Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly save selection state when the selection root is the editor #82

Merged
merged 1 commit into from
Jul 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions codejar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ export function CodeJar(editor: HTMLElement, highlight: (e: HTMLElement, pos?: P
let {anchorNode, anchorOffset, focusNode, focusOffset} = s
if (!anchorNode || !focusNode) throw 'error1'

// If the anchor and focus are the editor element, return either a full
// highlight or a start/end cursor position depending on the selection
if (anchorNode === editor && focusNode === editor) {
pos.start = (anchorOffset > 0 && editor.textContent) ? editor.textContent.length : 0
pos.end = (focusOffset > 0 && editor.textContent) ? editor.textContent.length : 0
pos.dir = (focusOffset >= anchorOffset) ? '->' : '<-'
return pos
}

// Selection anchor and focus are expected to be text nodes,
// so normalize them.
if (anchorNode.nodeType === Node.ELEMENT_NODE) {
Expand Down