text.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { Editor, Element, Text, Location } from 'slate';
  2. import { SelectionPoint, TextDelta, TextSelection } from '$app/interfaces/document';
  3. export function getDelta(editor: Editor, at: Location): TextDelta[] {
  4. const baseElement = Editor.fragment(editor, at)[0] as Element;
  5. return baseElement.children.map((item) => {
  6. const { text, ...attributes } = item as Text;
  7. return {
  8. insert: text,
  9. attributes,
  10. };
  11. });
  12. }
  13. /**
  14. * get the selection between the beginning of the editor and the point
  15. * form 0 to point
  16. * @param editor
  17. * @param at
  18. */
  19. export function getBeforeRangeAt(editor: Editor, at: Location) {
  20. const start = Editor.start(editor, at);
  21. return {
  22. anchor: { path: [0, 0], offset: 0 },
  23. focus: start,
  24. };
  25. }
  26. /**
  27. * get the selection between the point and the end of the editor
  28. * from point to end
  29. * @param editor
  30. * @param at
  31. */
  32. export function getAfterRangeAt(editor: Editor, at: Location) {
  33. const end = Editor.end(editor, at);
  34. const fragment = (editor.children[0] as Element).children;
  35. const lastIndex = fragment.length - 1;
  36. const lastNode = fragment[lastIndex] as Text;
  37. return {
  38. anchor: end,
  39. focus: { path: [0, lastIndex], offset: lastNode.text.length },
  40. };
  41. }
  42. /**
  43. * check if the point is in the beginning of the editor
  44. * @param editor
  45. * @param at
  46. */
  47. export function pointInBegin(editor: Editor, at: Location) {
  48. const start = Editor.start(editor, at);
  49. return Editor.before(editor, start) === undefined;
  50. }
  51. /**
  52. * check if the point is in the end of the editor
  53. * @param editor
  54. * @param at
  55. */
  56. export function pointInEnd(editor: Editor, at: Location) {
  57. const end = Editor.end(editor, at);
  58. return Editor.after(editor, end) === undefined;
  59. }
  60. /**
  61. * get the selection of the beginning of the node
  62. */
  63. export function getNodeBeginSelection(): TextSelection {
  64. const point: SelectionPoint = {
  65. path: [0, 0],
  66. offset: 0,
  67. };
  68. const selection: TextSelection = {
  69. anchor: clonePoint(point),
  70. focus: clonePoint(point),
  71. };
  72. return selection;
  73. }
  74. /**
  75. * get the selection of the end of the node
  76. * @param delta
  77. */
  78. export function getNodeEndSelection(delta: TextDelta[]) {
  79. const len = delta.length;
  80. const offset = len > 0 ? delta[len - 1].insert.length : 0;
  81. const cursorPoint: SelectionPoint = {
  82. path: [0, Math.max(len - 1, 0)],
  83. offset,
  84. };
  85. const selection: TextSelection = {
  86. anchor: clonePoint(cursorPoint),
  87. focus: clonePoint(cursorPoint),
  88. };
  89. return selection;
  90. }
  91. /**
  92. * get lines by delta
  93. * @param delta
  94. */
  95. export function getLinesByDelta(delta: TextDelta[]): string[] {
  96. const text = delta.map((item) => item.insert).join('');
  97. return text.split('\n');
  98. }
  99. /**
  100. * get the offset of the last line
  101. * @param delta
  102. */
  103. export function getLastLineOffsetByDelta(delta: TextDelta[]): number {
  104. const text = delta.map((item) => item.insert).join('');
  105. const index = text.lastIndexOf('\n');
  106. return index === -1 ? 0 : index + 1;
  107. }
  108. /**
  109. * get the selection of the end line by offset
  110. * @param delta
  111. * @param offset relative offset of the end line
  112. */
  113. export function getEndLineSelectionByOffset(delta: TextDelta[], offset: number) {
  114. const lines = getLinesByDelta(delta);
  115. const endLine = lines[lines.length - 1];
  116. // if the offset is greater than the length of the end line, set cursor to the end of prev line
  117. if (offset >= endLine.length) {
  118. return getNodeEndSelection(delta);
  119. }
  120. const textOffset = getLastLineOffsetByDelta(delta) + offset;
  121. return getSelectionByTextOffset(delta, textOffset);
  122. }
  123. /**
  124. * get the selection of the start line by offset
  125. * @param delta
  126. * @param offset relative offset of the start line
  127. */
  128. export function getStartLineSelectionByOffset(delta: TextDelta[], offset: number) {
  129. const lines = getLinesByDelta(delta);
  130. if (lines.length === 0) {
  131. return getNodeBeginSelection();
  132. }
  133. const startLine = lines[0];
  134. // if the offset is greater than the length of the end line, set cursor to the end of prev line
  135. if (offset >= startLine.length) {
  136. return getSelectionByTextOffset(delta, startLine.length);
  137. }
  138. return getSelectionByTextOffset(delta, offset);
  139. }
  140. /**
  141. * get the selection by text offset
  142. * @param delta
  143. * @param offset absolute offset
  144. */
  145. export function getSelectionByTextOffset(delta: TextDelta[], offset: number) {
  146. const point = getPointByTextOffset(delta, offset);
  147. const selection: TextSelection = {
  148. anchor: clonePoint(point),
  149. focus: clonePoint(point),
  150. };
  151. return selection;
  152. }
  153. /**
  154. * get the point by text offset
  155. * @param delta
  156. * @param offset absolute offset
  157. */
  158. export function getPointByTextOffset(delta: TextDelta[], offset: number): SelectionPoint {
  159. let textOffset = 0;
  160. let path: [number, number] = [0, 0];
  161. let textLength = 0;
  162. for (let i = 0; i < delta.length; i++) {
  163. const item = delta[i];
  164. if (textOffset + item.insert.length >= offset) {
  165. path = [0, i];
  166. textLength = offset - textOffset;
  167. break;
  168. }
  169. textOffset += item.insert.length;
  170. }
  171. return {
  172. path,
  173. offset: textLength,
  174. };
  175. }
  176. export function clonePoint(point: SelectionPoint): SelectionPoint {
  177. return {
  178. path: [...point.path],
  179. offset: point.offset,
  180. };
  181. }