-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
159 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { ParentBlot } from 'parchment'; | ||
import Module from '../core/module'; | ||
import Quill from '../core/quill'; | ||
|
||
const isMac = /Mac/i.test(navigator.platform); | ||
|
||
const canMoveCaretBeforeUINode = (event: KeyboardEvent) => { | ||
if ( | ||
event.key === 'ArrowLeft' || | ||
event.key === 'ArrowRight' || | ||
event.key === 'ArrowUp' || | ||
event.key === 'ArrowDown' || | ||
event.key === 'Home' | ||
) { | ||
return true; | ||
} | ||
|
||
if (isMac && event.key === 'a' && event.ctrlKey === true) { | ||
return true; | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
class UINodeSelection extends Module { | ||
isListening = false; | ||
selectionChangeDeadline = 0; | ||
|
||
constructor(quill: Quill, options: Record<string, never>) { | ||
super(quill, options); | ||
|
||
this.quill.keyboard.addBinding({ | ||
key: 'ArrowLeft', | ||
shiftKey: null, | ||
handler(range, { line, offset, event }) { | ||
if (offset === 0 && line instanceof ParentBlot && line.uiNode) { | ||
quill.setSelection( | ||
range.index - 1, | ||
range.length + (event.shiftKey ? 1 : 0), | ||
Quill.sources.USER, | ||
); | ||
return false; | ||
} | ||
return true; | ||
}, | ||
}); | ||
this.startMonitoringSelectionChange(); | ||
} | ||
|
||
private startMonitoringSelectionChange() { | ||
this.quill.root.addEventListener('keydown', (event) => { | ||
if (!event.defaultPrevented && canMoveCaretBeforeUINode(event)) { | ||
this.ensureListeningToSelectionChange(); | ||
} | ||
}); | ||
} | ||
|
||
private ensureListeningToSelectionChange() { | ||
if (this.isListening) return; | ||
|
||
this.isListening = true; | ||
this.selectionChangeDeadline = Date.now() + 100; | ||
document.addEventListener('selectionchange', this.handleSelectionChange, { | ||
once: true, | ||
}); | ||
} | ||
|
||
private handleSelectionChange = () => { | ||
this.isListening = false; | ||
if (Date.now() > this.selectionChangeDeadline) return; | ||
|
||
const selection = document.getSelection(); | ||
if (!selection) return; | ||
const range = selection.getRangeAt(0); | ||
if (range.collapsed !== true || range.startOffset !== 0) return; | ||
|
||
const line = this.quill.scroll.find(range.startContainer); | ||
if (!(line instanceof ParentBlot) || !line.uiNode) return; | ||
|
||
const newRange = document.createRange(); | ||
newRange.setStartAfter(line.uiNode); | ||
newRange.setEndAfter(line.uiNode); | ||
selection.removeAllRanges(); | ||
selection.addRange(newRange); | ||
}; | ||
} | ||
|
||
export default UINodeSelection; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters