@folijs/core 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. package/CHANGELOG.md +45 -0
  2. package/README.md +1 -1
  3. package/dist/document.d.ts +10 -16
  4. package/dist/document.d.ts.map +1 -1
  5. package/dist/document.js +22 -22
  6. package/dist/document.js.map +1 -1
  7. package/dist/index.d.ts +3 -2
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +3 -2
  10. package/dist/index.js.map +1 -1
  11. package/dist/model/fragment.d.ts +5 -2
  12. package/dist/model/fragment.d.ts.map +1 -1
  13. package/dist/model/fragment.js +5 -2
  14. package/dist/model/fragment.js.map +1 -1
  15. package/dist/model/node.d.ts +1 -1
  16. package/dist/model/node.d.ts.map +1 -1
  17. package/dist/model/node.js.map +1 -1
  18. package/dist/schema.d.ts +10 -1
  19. package/dist/schema.d.ts.map +1 -1
  20. package/dist/schema.js +11 -1
  21. package/dist/schema.js.map +1 -1
  22. package/dist/state/commands.d.ts +7 -6
  23. package/dist/state/commands.d.ts.map +1 -1
  24. package/dist/state/commands.js +162 -97
  25. package/dist/state/commands.js.map +1 -1
  26. package/dist/state/comments.d.ts +47 -0
  27. package/dist/state/comments.d.ts.map +1 -0
  28. package/dist/state/comments.js +304 -0
  29. package/dist/state/comments.js.map +1 -0
  30. package/dist/state/editor.d.ts +67 -16
  31. package/dist/state/editor.d.ts.map +1 -1
  32. package/dist/state/editor.js +74 -18
  33. package/dist/state/editor.js.map +1 -1
  34. package/dist/state/format.d.ts +8 -22
  35. package/dist/state/format.d.ts.map +1 -1
  36. package/dist/state/format.js +16 -140
  37. package/dist/state/format.js.map +1 -1
  38. package/dist/state/history.d.ts +1 -1
  39. package/dist/state/history.d.ts.map +1 -1
  40. package/dist/state/history.js +3 -2
  41. package/dist/state/history.js.map +1 -1
  42. package/dist/state/index.d.ts +11 -4
  43. package/dist/state/index.d.ts.map +1 -1
  44. package/dist/state/index.js +11 -4
  45. package/dist/state/index.js.map +1 -1
  46. package/dist/state/links.d.ts.map +1 -1
  47. package/dist/state/links.js +10 -2
  48. package/dist/state/links.js.map +1 -1
  49. package/dist/state/lists.d.ts +19 -32
  50. package/dist/state/lists.d.ts.map +1 -1
  51. package/dist/state/lists.js +23 -372
  52. package/dist/state/lists.js.map +1 -1
  53. package/dist/state/mark.d.ts +28 -0
  54. package/dist/state/mark.d.ts.map +1 -0
  55. package/dist/state/mark.js +57 -0
  56. package/dist/state/mark.js.map +1 -0
  57. package/dist/state/review.d.ts +43 -0
  58. package/dist/state/review.d.ts.map +1 -0
  59. package/dist/state/review.js +282 -0
  60. package/dist/state/review.js.map +1 -0
  61. package/dist/state/state.d.ts +2 -0
  62. package/dist/state/state.d.ts.map +1 -1
  63. package/dist/state/state.js +18 -0
  64. package/dist/state/state.js.map +1 -1
  65. package/dist/state/track.d.ts +47 -0
  66. package/dist/state/track.d.ts.map +1 -0
  67. package/dist/state/track.js +534 -0
  68. package/dist/state/track.js.map +1 -0
  69. package/dist/state/word.d.ts +100 -0
  70. package/dist/state/word.d.ts.map +1 -0
  71. package/dist/state/word.js +253 -0
  72. package/dist/state/word.js.map +1 -0
  73. package/dist/state/wordclip.d.ts +43 -0
  74. package/dist/state/wordclip.d.ts.map +1 -0
  75. package/dist/state/wordclip.js +412 -0
  76. package/dist/state/wordclip.js.map +1 -0
  77. package/dist/state/wordformat.d.ts +136 -0
  78. package/dist/state/wordformat.d.ts.map +1 -0
  79. package/dist/state/wordformat.js +846 -0
  80. package/dist/state/wordformat.js.map +1 -0
  81. package/dist/v3/check.d.ts +33 -0
  82. package/dist/v3/check.d.ts.map +1 -0
  83. package/dist/v3/check.js +90 -0
  84. package/dist/v3/check.js.map +1 -0
  85. package/dist/v3/document.d.ts +217 -0
  86. package/dist/v3/document.d.ts.map +1 -0
  87. package/dist/v3/document.js +225 -0
  88. package/dist/v3/document.js.map +1 -0
  89. package/dist/v3/index.d.ts +6 -0
  90. package/dist/v3/index.d.ts.map +1 -0
  91. package/dist/v3/index.js +6 -0
  92. package/dist/v3/index.js.map +1 -0
  93. package/dist/v3/migrate.d.ts +19 -0
  94. package/dist/v3/migrate.d.ts.map +1 -0
  95. package/dist/v3/migrate.js +227 -0
  96. package/dist/v3/migrate.js.map +1 -0
  97. package/dist/v3/resolve.d.ts +129 -0
  98. package/dist/v3/resolve.d.ts.map +1 -0
  99. package/dist/v3/resolve.js +534 -0
  100. package/dist/v3/resolve.js.map +1 -0
  101. package/dist/v3/schema.d.ts +29 -0
  102. package/dist/v3/schema.d.ts.map +1 -0
  103. package/dist/v3/schema.js +431 -0
  104. package/dist/v3/schema.js.map +1 -0
  105. package/dist/v3/units.d.ts +16 -0
  106. package/dist/v3/units.d.ts.map +1 -0
  107. package/dist/v3/units.js +28 -0
  108. package/dist/v3/units.js.map +1 -0
  109. package/package.json +1 -1
  110. package/src/document.ts +26 -26
  111. package/src/index.ts +3 -6
  112. package/src/model/fragment.ts +11 -3
  113. package/src/model/node.ts +6 -1
  114. package/src/schema.ts +13 -1
  115. package/src/state/commands.ts +173 -91
  116. package/src/state/comments.ts +342 -0
  117. package/src/state/editor.ts +176 -36
  118. package/src/state/format.ts +24 -122
  119. package/src/state/history.ts +3 -2
  120. package/src/state/index.ts +73 -3
  121. package/src/state/links.ts +10 -2
  122. package/src/state/lists.ts +31 -408
  123. package/src/state/mark.ts +64 -0
  124. package/src/state/review.ts +330 -0
  125. package/src/state/state.ts +18 -0
  126. package/src/state/track.ts +602 -0
  127. package/src/state/word.ts +330 -0
  128. package/src/state/wordclip.ts +445 -0
  129. package/src/state/wordformat.ts +982 -0
  130. package/src/v3/check.ts +119 -0
  131. package/src/v3/document.ts +456 -0
  132. package/src/v3/index.ts +33 -0
  133. package/src/v3/migrate.ts +267 -0
  134. package/src/v3/resolve.ts +646 -0
  135. package/src/v3/schema.ts +482 -0
  136. package/src/v3/units.ts +34 -0
@@ -1,133 +1,35 @@
1
- import { Fragment, type MarkType, type Node, Slice } from "../model/index.js";
2
1
  import { type Alignment, isColor } from "../schema.js";
3
- import { TransformError } from "../transform/transform.js";
4
- import { attempt } from "./attempt.js";
5
- import { marksAt, NodeSelection, TextSelection } from "./selection.js";
6
- import { type Command, defaultTextblockType, type EditorState } from "./state.js";
2
+ import type { Command, EditorState } from "./state.js";
3
+ import {
4
+ setWordAlign,
5
+ setWordHighlight,
6
+ setWordTextColor,
7
+ wordAlignAt,
8
+ wordHighlightAt,
9
+ wordTextColorAt,
10
+ } from "./wordformat.js";
7
11
 
8
- /** The alignment all textblocks in the selection share, or null (mixed, or no textblock). */
9
- export function alignAt(state: EditorState): Alignment | null {
10
- let align: Alignment | null | undefined;
11
- const { from, to } = state.selection;
12
- state.doc.nodesBetween(from, to, (node) => {
13
- if (align === null) return false;
14
- if (!node.isTextblock) return undefined;
15
- const value = typeof node.attrs.align === "string" ? (node.attrs.align as Alignment) : null;
16
- align = align === undefined || align === value ? value : null;
17
- return false;
18
- });
19
- return align ?? null;
20
- }
12
+ /** The alignment all selected paragraphs show, or null (mixed, or no paragraph). */
13
+ export const alignAt: (state: EditorState) => Alignment | null = wordAlignAt;
21
14
 
22
- /**
23
- * Aligns the textblocks in the selection (`start` is the default: it removes the alignment).
24
- * An unknown value does not apply (the schema rejects it).
25
- */
26
- export function setAlign(align: Alignment): Command {
27
- return (state, dispatch) => {
28
- const { from, to } = state.selection;
29
- return attempt(state, dispatch, (tr) => tr.setBlockAttrs(from, to, { align }));
30
- };
31
- }
15
+ /** Aligns the selected paragraphs (`start` is the default). */
16
+ export const setAlign: (align: Alignment) => Command = setWordAlign;
32
17
 
33
- /**
34
- * Colors the selected text with a color mark (`textColor`, `highlight`), replacing the one it
35
- * has; `null` removes it. At a caret, sets the mark for the text typed next. Colors are
36
- * lowercase `#rrggbb` (an uppercase one is lowered); others do not apply.
37
- */
38
- function setColorMark(name: string, color: string | null): Command {
39
- return (state, dispatch) => {
40
- const type: MarkType | undefined = state.schema.marks[name];
18
+ /** Colors are lowercase `#rrggbb` (an uppercase one is lowered); others do not apply. */
19
+ const colored =
20
+ (set: (color: string | null) => Command) =>
21
+ (color: string | null): Command => {
41
22
  const value = color?.toLowerCase() ?? null;
42
- if (!type || (value !== null && !isColor(value))) return false;
43
- const mark = value === null ? null : type.create({ color: value });
44
- const { selection } = state;
45
- if (selection.empty && selection instanceof TextSelection) {
46
- const { $from } = selection;
47
- if (!$from.parent.type.allowsMarkType(type)) return false;
48
- const marks = state.storedMarks ?? marksAt($from);
49
- const next = mark ? mark.addToSet(marks) : type.removeFromSet(marks);
50
- if (next === marks) return false;
51
- dispatch?.(state.tr.setStoredMarks(next));
52
- return true;
53
- }
54
- const { from, to } = selection;
55
- return attempt(state, dispatch, (tr) => {
56
- if (mark) tr.addMark(from, to, mark);
57
- else tr.removeMark(from, to, type);
58
- });
23
+ return value !== null && !isColor(value) ? () => false : set(value);
59
24
  };
60
- }
61
25
 
62
- /** Text color (`#rrggbb`) for the selected or next typed text; `null` removes it. */
63
- export const setTextColor = (color: string | null): Command => setColorMark("textColor", color);
26
+ /** Text color (`#rrggbb`) for the selected or next typed text; `null` is Word's Automatic. */
27
+ export const setTextColor = colored(setWordTextColor);
64
28
 
65
- /** Highlight (background color, `#rrggbb`) for the selected or next typed text; `null` removes it. */
66
- export const setHighlight = (color: string | null): Command => setColorMark("highlight", color);
29
+ /** Highlight (`#rrggbb`) for the selected or next typed text; `null` removes it. */
30
+ export const setHighlight = colored(setWordHighlight);
67
31
 
68
- /**
69
- * The color of `name` (`textColor`, `highlight`) at the selection: the one all selected text
70
- * has (at a caret, the one typing gets), or null (none, or mixed).
71
- */
32
+ /** The text color or highlight all selected text shows (at a caret, typing's), or null. */
72
33
  export function colorAt(state: EditorState, name: "textColor" | "highlight"): string | null {
73
- const type = state.schema.marks[name];
74
- if (!type) return null;
75
- const read = (marks: Parameters<MarkType["isInSet"]>[0]) =>
76
- (type.isInSet(marks)?.attrs.color as string | undefined) ?? null;
77
- const { selection } = state;
78
- if (selection.empty) return read(state.storedMarks ?? marksAt(selection.$from));
79
- let color: string | null | undefined;
80
- state.doc.nodesBetween(selection.from, selection.to, (node, _pos, parent) => {
81
- if (color === null) return false;
82
- if (!node.isInline || !parent?.type.allowsMarkType(type)) return undefined;
83
- const value = read(node.marks);
84
- color = color === undefined || color === value ? value : null;
85
- return false;
86
- });
87
- return color ?? null;
34
+ return name === "highlight" ? wordHighlightAt(state) : wordTextColorAt(state);
88
35
  }
89
-
90
- /**
91
- * Inserts a horizontal rule at the selection: at the start of a textblock, before it; at its
92
- * end, after it, with a new empty paragraph for the caret; inside it, splitting it. Where a
93
- * rule cannot go before the textblock (a list item's first paragraph), an empty one is treated
94
- * as its end and a non-empty one is split at its start. With a block selected (a rule), after
95
- * that block. The caret goes to the text after the rule, keeping the marks typing had.
96
- */
97
- export const insertHorizontalRule: Command = (state, dispatch) => {
98
- const rule = state.schema.nodes.horizontalRule;
99
- if (!rule) return false;
100
- const paragraph = defaultTextblockType(state.schema);
101
- const { selection } = state;
102
- return attempt(state, dispatch, (tr) => {
103
- const marks = state.storedMarks ?? marksAt(selection.$from);
104
- let at: number;
105
- let newParagraph: boolean;
106
- if (selection instanceof NodeSelection && selection.node.isBlock) {
107
- at = selection.to;
108
- newParagraph = !tr.doc.resolve(at).nodeAfter?.isTextblock;
109
- } else {
110
- tr.deleteSelection();
111
- const $pos = tr.selection.$from;
112
- if (!$pos.parent.isTextblock) throw new TransformError("No rule here");
113
- const index = $pos.index(-1);
114
- const before = $pos.node(-1).canReplaceWith(index, index, rule);
115
- if ($pos.parentOffset === 0 && before) {
116
- at = $pos.before();
117
- newParagraph = false;
118
- } else if ($pos.parentOffset === $pos.parent.content.size) {
119
- at = $pos.after();
120
- newParagraph = true;
121
- } else {
122
- tr.split($pos.pos);
123
- at = $pos.pos + 1;
124
- newParagraph = false;
125
- }
126
- }
127
- const content: Node[] = [rule.create()];
128
- if (newParagraph) content.push(paragraph.create());
129
- tr.replace(at, at, new Slice(Fragment.from(content), 0, 0));
130
- tr.setSelection(TextSelection.create(tr.doc, at + 2));
131
- if (marks.length) tr.setStoredMarks(marks);
132
- });
133
- };
@@ -33,7 +33,7 @@ const historyKey = new PluginKey<HistoryState>("history");
33
33
  /** Meta set on undo/redo transactions: which operation produced them. */
34
34
  const historyOp = "historyOp";
35
35
 
36
- /** Set this meta on a transaction to stop it merging into the previous undo event. */
36
+ /** Set this meta on a transaction to make it an undo step of its own (no merging either side). */
37
37
  export const closeHistory = "closeHistory";
38
38
 
39
39
  const EMPTY: HistoryState = { done: [], undone: [], prevTime: 0, prevRanges: null };
@@ -99,7 +99,8 @@ export function history({ newGroupDelay = 500, depth = 100 }: HistoryOptions = {
99
99
  return {
100
100
  done: done.slice(-depth),
101
101
  undone: [],
102
- prevTime: root ? hist.prevTime : tr.time,
102
+ // A closed change is a step of its own on both sides: what follows does not join it.
103
+ prevTime: root ? hist.prevTime : tr.getMeta(closeHistory) ? 0 : tr.time,
103
104
  prevRanges: root ? mapRanges(hist.prevRanges, tr.mapping) : changedRanges(tr),
104
105
  };
105
106
  },
@@ -7,6 +7,7 @@ export {
7
7
  deleteSelection,
8
8
  insertHardBreak,
9
9
  insertPageBreak,
10
+ insertTab,
10
11
  insertText,
11
12
  isMarkActive,
12
13
  joinBackward,
@@ -16,11 +17,28 @@ export {
16
17
  splitBlock,
17
18
  toggleMark,
18
19
  } from "./commands.js";
19
- export { createEditor, type Editor, type EditorListener, type EditorOptions } from "./editor.js";
20
+ export {
21
+ addComment,
22
+ type CommentContent,
23
+ type CommentOptions,
24
+ commentAnchors,
25
+ deleteAllComments,
26
+ deleteComment,
27
+ editComment,
28
+ replyToComment,
29
+ resolveComment,
30
+ } from "./comments.js";
31
+ export {
32
+ createEditor,
33
+ type Editor,
34
+ type EditorDocument,
35
+ type EditorListener,
36
+ type EditorOptions,
37
+ sectionSettings,
38
+ } from "./editor.js";
20
39
  export {
21
40
  alignAt,
22
41
  colorAt,
23
- insertHorizontalRule,
24
42
  setAlign,
25
43
  setHighlight,
26
44
  setTextColor,
@@ -49,12 +67,25 @@ export {
49
67
  liftListItem,
50
68
  liftListItemAtStart,
51
69
  listKeymap,
52
- listTypeAt,
70
+ listKindAt,
53
71
  sinkListItem,
54
72
  splitListItem,
55
73
  toggleList,
56
74
  wrapInList,
57
75
  } from "./lists.js";
76
+ export { typedMarks } from "./mark.js";
77
+ export {
78
+ acceptAllChanges,
79
+ acceptChange,
80
+ type Change,
81
+ type ChangeFilter,
82
+ type ChangeKind,
83
+ nextChange,
84
+ previousChange,
85
+ rejectAllChanges,
86
+ rejectChange,
87
+ trackedChanges,
88
+ } from "./review.js";
58
89
  export {
59
90
  AllSelection,
60
91
  marksAt,
@@ -73,3 +104,42 @@ export {
73
104
  type PluginSpec,
74
105
  Transaction,
75
106
  } from "./state.js";
107
+ export {
108
+ type ReviewView,
109
+ setReviewView,
110
+ setTrackChanges,
111
+ type TrackState,
112
+ trackState,
113
+ trackTransaction,
114
+ untracked,
115
+ } from "./track.js";
116
+ export {
117
+ CommentStep,
118
+ setComments,
119
+ type WordContext,
120
+ type WordEnvelope,
121
+ withEnvelope,
122
+ wordContext,
123
+ } from "./word.js";
124
+ export {
125
+ copiedDefinitions,
126
+ fitPaste,
127
+ fitPastedV2,
128
+ type WordDefinitions,
129
+ } from "./wordclip.js";
130
+ export {
131
+ canOutdent,
132
+ changeIndent,
133
+ fontFamilyAt,
134
+ fontSizeAt,
135
+ HIGHLIGHT_HEX,
136
+ type ListKind,
137
+ lineSpacingAt,
138
+ paragraphStyleAt,
139
+ paragraphStyles,
140
+ setFontFamily,
141
+ setFontSize,
142
+ setLineSpacing,
143
+ setParagraphSpacing,
144
+ setParagraphStyle,
145
+ } from "./wordformat.js";
@@ -4,6 +4,7 @@ import { TransformError } from "../transform/transform.js";
4
4
  import { attempt } from "./attempt.js";
5
5
  import { AllSelection, marksAt, TextSelection } from "./selection.js";
6
6
  import type { Command, EditorState, Transaction } from "./state.js";
7
+ import { styleLink, unstyleLink } from "./wordformat.js";
7
8
 
8
9
  const EMAIL = /^[^\s@/:]+@[^\s@/:]+\.\p{L}{2,}$/iu;
9
10
  /**
@@ -129,6 +130,7 @@ export function insertLink(href: string, text?: string): Command {
129
130
  }
130
131
  const allowed = marks.filter((mark) => parent.type.allowsMarkType(mark.type));
131
132
  tr.insertText(label, pos, pos, link.addToSet(allowed));
133
+ styleLink(tr, state, pos, pos + label.length);
132
134
  tr.setSelection(TextSelection.create(tr.doc, pos + label.length));
133
135
  });
134
136
  };
@@ -147,7 +149,10 @@ export function setLink(href: string): Command {
147
149
  const mark = type.create({ href: normalized });
148
150
  const { selection } = state;
149
151
  if (!selection.empty) {
150
- return attempt(state, dispatch, (tr) => tr.addMark(selection.from, selection.to, mark));
152
+ return attempt(state, dispatch, (tr) => {
153
+ tr.addMark(selection.from, selection.to, mark);
154
+ styleLink(tr, state, selection.from, selection.to);
155
+ });
151
156
  }
152
157
  const around = linkAround(selection.$from, type);
153
158
  if (!around) return insertLink(normalized)(state, dispatch);
@@ -162,7 +167,10 @@ export const removeLink: Command = (state, dispatch) => {
162
167
  const { selection } = state;
163
168
  const range = selection.empty ? linkAround(selection.$from, type) : selection;
164
169
  if (!range) return false;
165
- return attempt(state, dispatch, (tr) => tr.removeMark(range.from, range.to, type));
170
+ return attempt(state, dispatch, (tr) => {
171
+ tr.removeMark(range.from, range.to, type);
172
+ unstyleLink(tr, state, range.from, range.to);
173
+ });
166
174
  };
167
175
 
168
176
  /** Transaction metadata set by `openLinkEditor`; a link editing UI listens for it. */