-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
2349077
commit 2b6700c
Showing
4 changed files
with
48 additions
and
27 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 |
---|---|---|
@@ -1,42 +1,63 @@ | ||
import { Editor, Range, Point } from 'slate'; | ||
import { Editor, Range, Point, Text, Node, Path } from 'slate'; | ||
import { Cursor, Selection } from '../model/cursor.ts'; | ||
|
||
function emptySelection(): Selection { | ||
return { | ||
start: { line: 0, column: 0 }, | ||
end: { line: 0, column: 0 }, | ||
}; | ||
} | ||
|
||
export function isSelected(editor: Editor) { | ||
if (!editor.selection) return false; | ||
const { anchor, focus } = editor.selection; | ||
return anchor.path !== focus.path && anchor.offset !== focus.offset; | ||
} | ||
|
||
// export function getSelection(editor: Editor): Selection { | ||
// if (!editor.selection) return { start: { line: 0, column: 0 }, end: { line: 0, column: 0 } }; | ||
// const { anchor, focus } = editor.selection; | ||
// const { path: startLine, offset: startColumn } = focus; | ||
// const { path: endLine, offset: endColumn } = anchor; | ||
// const start = { line: startLine[0], column: startColumn }; | ||
// const end = { line: endLine[0], column: endColumn }; | ||
// const isRightToLeft = start.line > end.line || (start.line === end.line && start.column > end.column); | ||
// return isRightToLeft ? { start: end, end: start } : { start, end }; | ||
// } | ||
|
||
// does the same as the function above | ||
export function getSelection(editor: Editor): Selection { | ||
const { selection } = editor; | ||
if (!selection) { | ||
return { | ||
start: { line: 0, column: 0 }, | ||
end: { line: 0, column: 0 }, | ||
}; | ||
return emptySelection(); | ||
} | ||
const [start, end] = Range.edges(selection); | ||
return { | ||
start: pointToCursor(start), | ||
end: pointToCursor(end), | ||
start: pointToCursor(editor, start), | ||
end: pointToCursor(editor, end), | ||
}; | ||
} | ||
|
||
function pointToCursor(point: Point): Cursor { | ||
function pointToCursor(editor: Editor, point: Point): Cursor { | ||
return { | ||
line: point.path[0], | ||
column: point.offset, | ||
column: getAbsoluteOffset(editor, point), | ||
}; | ||
} | ||
|
||
function getAbsoluteOffset(editor: Editor, point: Point): number { | ||
let offset = 0; | ||
let line = 0; | ||
|
||
// Traverse the nodes in the document | ||
for (const [node, path] of Node.nodes(editor)) { | ||
if (Text.isText(node)) { | ||
if (Path.isBefore(path, point.path)) { | ||
// If the current text node is before the point, add its length to the offset | ||
offset += node.text.length; | ||
// If the text node contains a line break, increment the line count and reset the offset | ||
if (node.text.includes('\n')) { | ||
line++; | ||
offset = 0; | ||
} | ||
} else if (Path.equals(path, point.path)) { | ||
// If the current text node contains the point, add the point's offset to the total offset | ||
offset += point.offset; | ||
break; | ||
} | ||
} | ||
if (line !== point.path[0]) { | ||
offset = 0; | ||
} | ||
} | ||
|
||
return offset; | ||
} |