这是一个系列博客,最终目的是要做一个基于 HTML Canvas 的、类似于微软 Office 的 Web Office 套件(包括:文档、表格、幻灯片……等等)。 博客园:《从零开始, 开发一个 Web Office 套件》系列博客目录 富文本编辑器 Github repo 地址:https://github.com/zhaokang555/canvas-text-editor 富文本编辑器 在线 Demo:https://zhaokang555.github.io/canvas-text-editor/
compositionstart和compositionend事件。并记录用户状态:是否正在使用输入法keydown事件。当事件触发: Backspace键:删除光标前的一个字符参考文档:
KeyboardEvent.key: https://developer.mozilla.org/zh-CN/docs/Web/API/KeyboardEvent/keycompositionstart:https://developer.mozilla.org/zh-CN/docs/Web/API/Element/compositionstart_eventcompositionend: https://developer.mozilla.org/en-US/docs/Web/API/Element/compositionend_event实现算法的同时,我们对代码进行一些重构:
store.charUnderCursor(光标所处的字符) 替代store.cursorIdxInChars, store.curParaIdx, store.cursorIdxInCurPara这3个状态所起到的作用store.getPrevCharInSoftLine(char: Char)替代shar.prev指针所起到的作用new Char('\n', ...)进行分段调用算法:

实现算法&重构:

删除:

删除with中文输入法:

监听keydown事件,当用户按下'Enter'键时:
直接调用我们上一小节重构好的store.insertChar(char)就可以:


(未完待续)