@portone/docx-editor 0.6.0 → 0.6.2
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.
- package/CHANGELOG.md +40 -0
- package/dist/DocxEditor.js +84 -26
- package/dist/core.d.ts +4 -3
- package/dist/docx/exportDocx.d.ts +3 -2
- package/dist/docx/exportDocx.js +6 -10
- package/dist/docx/headersFooters.js +35 -23
- package/dist/docx/identities.d.ts +30 -0
- package/dist/docx/identities.js +43 -10
- package/dist/docx/importDocx.js +25 -26
- package/dist/docx/importParagraph.d.ts +1 -4
- package/dist/docx/importParagraph.js +4 -18
- package/dist/docx/invariants.d.ts +13 -10
- package/dist/docx/invariants.js +300 -116
- package/dist/docx/newLists.js +12 -3
- package/dist/docx/notes/newNote.d.ts +39 -0
- package/dist/docx/notes/newNote.js +122 -0
- package/dist/docx/notes/numbering.d.ts +27 -0
- package/dist/docx/notes/numbering.js +113 -0
- package/dist/docx/notes/reading.d.ts +58 -0
- package/dist/docx/notes/reading.js +98 -0
- package/dist/docx/notes/references.d.ts +33 -0
- package/dist/docx/notes/references.js +37 -0
- package/dist/docx/notes/writing.d.ts +19 -0
- package/dist/docx/notes/writing.js +95 -0
- package/dist/docx/partPlan.d.ts +3 -0
- package/dist/docx/partPlanners.d.ts +13 -0
- package/dist/docx/partPlanners.js +48 -0
- package/dist/docx/sections.d.ts +5 -0
- package/dist/docx/sections.js +7 -2
- package/dist/docx/serializeParagraph.js +7 -2
- package/dist/docx/serializePreserved.d.ts +5 -1
- package/dist/docx/serializePreserved.js +24 -7
- package/dist/docx/session.d.ts +5 -0
- package/dist/docx/session.js +6 -0
- package/dist/docx/storyParts.d.ts +133 -0
- package/dist/docx/storyParts.js +314 -0
- package/dist/editor/clipboard/htmlReader.d.ts +24 -4
- package/dist/editor/clipboard/htmlReader.js +39 -4
- package/dist/editor/clipboard/internalChannel.d.ts +30 -3
- package/dist/editor/clipboard/internalChannel.js +43 -3
- package/dist/editor/clipboard/normalizers.d.ts +35 -8
- package/dist/editor/clipboard/normalizers.js +60 -11
- package/dist/editor/clipboard/plugin.js +42 -22
- package/dist/editor/commands/exportQueries.d.ts +4 -3
- package/dist/editor/commands/index.d.ts +2 -1
- package/dist/editor/commands/index.js +18 -0
- package/dist/editor/commands/noteCommands.d.ts +47 -0
- package/dist/editor/commands/noteCommands.js +95 -0
- package/dist/editor/commands/noteQueries.d.ts +22 -3
- package/dist/editor/commands/noteQueries.js +21 -13
- package/dist/editor/createEditor.js +21 -3
- package/dist/editor/documentStyles.d.ts +3 -0
- package/dist/editor/documentStyles.js +2 -1
- package/dist/editor/editorDocument.d.ts +24 -0
- package/dist/editor/editorDocument.js +34 -4
- package/dist/editor/notes/noteSurface.d.ts +53 -0
- package/dist/editor/notes/noteSurface.js +246 -0
- package/dist/editor/plugins/compositionSelection.d.ts +20 -0
- package/dist/editor/plugins/compositionSelection.js +19 -0
- package/dist/editor/plugins/keymap.d.ts +14 -0
- package/dist/editor/plugins/keymap.js +13 -1
- package/dist/editor/plugins/noteLifecycle.d.ts +18 -0
- package/dist/editor/plugins/noteLifecycle.js +117 -0
- package/dist/editor/plugins/noteNavigation.d.ts +45 -0
- package/dist/editor/plugins/noteNavigation.js +86 -0
- package/dist/editor/plugins/noteNumbering.d.ts +13 -0
- package/dist/editor/plugins/noteNumbering.js +58 -0
- package/dist/editor/plugins/rowResize.js +2 -7
- package/dist/editor/plugins/tabLayout.js +2 -7
- package/dist/editor/stories/storyMarkup.d.ts +24 -0
- package/dist/editor/stories/storyMarkup.js +35 -0
- package/dist/editor/stories/storyState.d.ts +34 -0
- package/dist/editor/stories/storyState.js +74 -0
- package/dist/editor/stories/storyView.d.ts +115 -0
- package/dist/editor/stories/storyView.js +144 -0
- package/dist/index.d.ts +2 -2
- package/dist/numbering/spellers.d.ts +1 -1
- package/dist/numbering/spellers.js +6 -1
- package/dist/ooxml/errors.d.ts +135 -2
- package/dist/ooxml/errors.js +6 -0
- package/dist/page/blockKinds.d.ts +23 -4
- package/dist/page/demands/footnoteDemands.d.ts +18 -0
- package/dist/page/demands/footnoteDemands.js +51 -0
- package/dist/page/demands/index.d.ts +43 -0
- package/dist/page/demands/index.js +8 -0
- package/dist/page/kinds/paragraphKind.js +7 -1
- package/dist/page/kinds/tableKind.js +9 -1
- package/dist/page/measureBlocks.d.ts +2 -0
- package/dist/page/measureBlocks.js +31 -10
- package/dist/page/pageDecorations.d.ts +9 -2
- package/dist/page/pageDecorations.js +18 -5
- package/dist/page/pageLayout.d.ts +77 -1
- package/dist/page/pageLayout.js +186 -9
- package/dist/page/usePageLayout.d.ts +57 -2
- package/dist/page/usePageLayout.js +50 -5
- package/dist/schema/attrRoles.js +3 -2
- package/dist/schema/docxSchema.js +5 -2
- package/dist/schema/editGuard.d.ts +1 -1
- package/dist/schema/guards.js +1 -2
- package/dist/schema/preservedGuards.d.ts +4 -6
- package/dist/schema/preservedGuards.js +0 -9
- package/dist/schema/stories.d.ts +42 -2
- package/dist/schema/stories.js +28 -5
- package/dist/styles/classNames.d.ts +21 -6
- package/dist/styles/classNames.js +21 -6
- package/dist/styles/inlineStyle.d.ts +7 -0
- package/dist/styles/inlineStyle.js +11 -5
- package/dist/styles/visualScale.d.ts +19 -0
- package/dist/styles/visualScale.js +26 -0
- package/dist/styles.css +89 -32
- package/dist/ui/InsertImageButton.d.ts +3 -1
- package/dist/ui/InsertImageButton.js +3 -2
- package/dist/ui/TableMenu.d.ts +5 -2
- package/dist/ui/TableMenu.js +18 -1
- package/dist/ui/TextMenu.d.ts +5 -2
- package/dist/ui/TextMenu.js +21 -6
- package/dist/ui/Toolbar.d.ts +17 -2
- package/dist/ui/Toolbar.js +23 -10
- package/dist/ui/comments/useCommentRailLayout.js +2 -4
- package/dist/ui/noteItems.d.ts +21 -0
- package/dist/ui/noteItems.js +30 -0
- package/dist/ui/notes/NoteAreas.d.ts +73 -0
- package/dist/ui/notes/NoteAreas.js +131 -0
- package/dist/ui/notes/NoteList.d.ts +25 -0
- package/dist/ui/notes/NoteList.js +58 -0
- package/dist/ui/notes/StoryRow.d.ts +75 -0
- package/dist/ui/notes/StoryRow.js +122 -0
- package/dist/ui/notes/noteBands.d.ts +100 -0
- package/dist/ui/notes/noteBands.js +142 -0
- package/dist/ui/notes/noteHeights.d.ts +10 -0
- package/dist/ui/notes/noteHeights.js +24 -0
- package/dist/ui/notes/noteSeparator.d.ts +12 -0
- package/dist/ui/notes/noteSeparator.js +9 -0
- package/dist/ui/notes/useStorySurface.d.ts +68 -0
- package/dist/ui/notes/useStorySurface.js +95 -0
- package/dist/ui/runCommand.d.ts +8 -1
- package/dist/ui/runCommand.js +2 -2
- package/dist/ui/shortcutLabels.d.ts +16 -0
- package/dist/ui/shortcutLabels.js +9 -0
- package/dist/ui/usePageRoom.d.ts +13 -0
- package/dist/ui/usePageRoom.js +31 -0
- package/package.json +1 -1
- package/dist/docx/notes.d.ts +0 -27
- package/dist/docx/notes.js +0 -55
- package/dist/ui/NotesPanel.d.ts +0 -6
- package/dist/ui/NotesPanel.js +0 -33
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
// src/editor/notes/noteSurface.ts
|
|
2
|
+
import { keymap } from "prosemirror-keymap";
|
|
3
|
+
import {
|
|
4
|
+
DOMSerializer,
|
|
5
|
+
Mark
|
|
6
|
+
} from "prosemirror-model";
|
|
7
|
+
import { Plugin, TextSelection } from "prosemirror-state";
|
|
8
|
+
import { NOTE_NUMBER_ELEMENTS } from "../../docx/notes/newNote.js";
|
|
9
|
+
import { docxSchema } from "../../schema/index.js";
|
|
10
|
+
import { editShut, transactionAllowed } from "../../schema/guards.js";
|
|
11
|
+
import { editsShut } from "../../schema/protectionState.js";
|
|
12
|
+
import { noteKeyOf } from "../../schema/stories.js";
|
|
13
|
+
import { editorClassNames } from "../../styles/classNames.js";
|
|
14
|
+
import {
|
|
15
|
+
detachAnchors,
|
|
16
|
+
dropSourceIdentity,
|
|
17
|
+
mapSliceNodes,
|
|
18
|
+
rederiveDisplay,
|
|
19
|
+
rekeyNumbering
|
|
20
|
+
} from "../clipboard/normalizers.js";
|
|
21
|
+
import { redo, undo } from "../commands/historyCommands.js";
|
|
22
|
+
import { noteBodyCommand } from "../commands/noteCommands.js";
|
|
23
|
+
import { onMac } from "../plugins/keymap.js";
|
|
24
|
+
import { noteReferenceAt } from "../plugins/noteNavigation.js";
|
|
25
|
+
import {
|
|
26
|
+
NO_CAPABILITY
|
|
27
|
+
} from "../stories/storyView.js";
|
|
28
|
+
function isOwnMark(node) {
|
|
29
|
+
const element = node.attrs.element;
|
|
30
|
+
return node.type === docxSchema.nodes.rawRunContent && typeof element === "string" && NOTE_NUMBER_ELEMENTS.includes(element);
|
|
31
|
+
}
|
|
32
|
+
function drawnBySchema(node) {
|
|
33
|
+
return docxSchema.nodes.rawRunContent.spec.toDOM?.(node) ?? ["span"];
|
|
34
|
+
}
|
|
35
|
+
function markSpec(node, labelOf) {
|
|
36
|
+
return isOwnMark(node) ? ["sup", { class: editorClassNames.noteMark }, labelOf()] : drawnBySchema(node);
|
|
37
|
+
}
|
|
38
|
+
function noteNodeSpecs(labelOf) {
|
|
39
|
+
return { rawRunContent: (node) => markSpec(node, labelOf) };
|
|
40
|
+
}
|
|
41
|
+
function noteMarkView(labelOf, back) {
|
|
42
|
+
return (node) => {
|
|
43
|
+
const { dom } = DOMSerializer.renderSpec(document, markSpec(node, labelOf));
|
|
44
|
+
if (isOwnMark(node) && dom instanceof HTMLElement) {
|
|
45
|
+
dom.addEventListener("mousedown", (event) => {
|
|
46
|
+
event.preventDefault();
|
|
47
|
+
back();
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
return { dom };
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
var dropUnwritableContent = (slice) => mapSliceNodes(slice, (node) => {
|
|
54
|
+
if (node.type === docxSchema.nodes.image) return null;
|
|
55
|
+
const marks = node.marks.filter(
|
|
56
|
+
(mark) => mark.type !== docxSchema.marks.link
|
|
57
|
+
);
|
|
58
|
+
return Mark.sameSet(marks, node.marks) ? node : node.mark(marks);
|
|
59
|
+
});
|
|
60
|
+
var dropNoteReferences = (slice, { move }) => move ? slice : mapSliceNodes(
|
|
61
|
+
slice,
|
|
62
|
+
(node) => node.type === docxSchema.nodes.noteReference ? null : node
|
|
63
|
+
);
|
|
64
|
+
var NOTE_TAKES = NO_CAPABILITY;
|
|
65
|
+
var NOTE_NORMALIZERS = [
|
|
66
|
+
dropSourceIdentity,
|
|
67
|
+
detachAnchors,
|
|
68
|
+
dropNoteReferences,
|
|
69
|
+
dropUnwritableContent,
|
|
70
|
+
rekeyNumbering,
|
|
71
|
+
rederiveDisplay
|
|
72
|
+
];
|
|
73
|
+
function runOn(main, command) {
|
|
74
|
+
return command(main.state, (tr) => main.dispatch(tr), main);
|
|
75
|
+
}
|
|
76
|
+
function referenceOf(main, key) {
|
|
77
|
+
const note = noteKeyOf(key);
|
|
78
|
+
return note === null ? null : noteReferenceAt(main.state.doc, note.kind, note.id);
|
|
79
|
+
}
|
|
80
|
+
function returnToReference(main, key) {
|
|
81
|
+
const found = referenceOf(main, key);
|
|
82
|
+
if (found !== null) {
|
|
83
|
+
const after = found.pos + found.node.nodeSize;
|
|
84
|
+
main.dispatch(
|
|
85
|
+
main.state.tr.setSelection(TextSelection.near(main.state.doc.resolve(after))).scrollIntoView()
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
main.focus();
|
|
89
|
+
}
|
|
90
|
+
function leadingChipSize(story) {
|
|
91
|
+
const first = story.firstChild?.firstChild ?? null;
|
|
92
|
+
return first !== null && isOwnMark(first) ? first.nodeSize : 0;
|
|
93
|
+
}
|
|
94
|
+
function ownMarkIn(story) {
|
|
95
|
+
let found = null;
|
|
96
|
+
story.descendants((node, pos) => {
|
|
97
|
+
if (found === null && isOwnMark(node)) found = { pos, node };
|
|
98
|
+
return found === null;
|
|
99
|
+
});
|
|
100
|
+
return found;
|
|
101
|
+
}
|
|
102
|
+
function caretFloor(story) {
|
|
103
|
+
const held = ownMarkIn(story);
|
|
104
|
+
return held === null || held.pos !== markHome(story) ? null : held.pos + held.node.nodeSize;
|
|
105
|
+
}
|
|
106
|
+
function markHome(story) {
|
|
107
|
+
const type = docxSchema.nodes.rawRunContent;
|
|
108
|
+
let at = null;
|
|
109
|
+
story.descendants((node, pos) => {
|
|
110
|
+
if (at !== null) return false;
|
|
111
|
+
if (!node.isTextblock) return true;
|
|
112
|
+
if (node.type.contentMatch.matchType(type) !== null) at = pos + 1;
|
|
113
|
+
return false;
|
|
114
|
+
});
|
|
115
|
+
return at;
|
|
116
|
+
}
|
|
117
|
+
function pressesTowardsTheNumber() {
|
|
118
|
+
return [
|
|
119
|
+
"ArrowLeft",
|
|
120
|
+
"ArrowUp",
|
|
121
|
+
"Home",
|
|
122
|
+
"PageUp",
|
|
123
|
+
"Mod-ArrowLeft",
|
|
124
|
+
"Mod-ArrowUp",
|
|
125
|
+
"Mod-Home",
|
|
126
|
+
...onMac() ? ["Ctrl-a"] : []
|
|
127
|
+
];
|
|
128
|
+
}
|
|
129
|
+
function holdAtTheNumber(extend) {
|
|
130
|
+
return (state, dispatch) => {
|
|
131
|
+
const floor = caretFloor(state.doc);
|
|
132
|
+
const home = markHome(state.doc);
|
|
133
|
+
if (floor === null || home === null) return false;
|
|
134
|
+
const { selection } = state;
|
|
135
|
+
if (extend ? selection.head > home : selection.from > floor) return false;
|
|
136
|
+
if (dispatch && !extend) {
|
|
137
|
+
dispatch(state.tr.setSelection(TextSelection.create(state.doc, floor)));
|
|
138
|
+
}
|
|
139
|
+
return true;
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
function keysOffTheNumber() {
|
|
143
|
+
return Object.fromEntries(
|
|
144
|
+
pressesTowardsTheNumber().flatMap((press) => [
|
|
145
|
+
[press, holdAtTheNumber(false)],
|
|
146
|
+
[`Shift-${press}`, holdAtTheNumber(true)]
|
|
147
|
+
])
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
function ownMarkFirst() {
|
|
151
|
+
return new Plugin({
|
|
152
|
+
appendTransaction(transactions, oldState, newState) {
|
|
153
|
+
const tr = newState.tr;
|
|
154
|
+
const home = markHome(newState.doc);
|
|
155
|
+
if (transactions.some((changed) => changed.docChanged) && home !== null) {
|
|
156
|
+
const held = ownMarkIn(newState.doc);
|
|
157
|
+
const lost = ownMarkIn(oldState.doc);
|
|
158
|
+
if (held === null) {
|
|
159
|
+
if (lost !== null) tr.insert(home, lost.node);
|
|
160
|
+
} else if (held.pos !== home) {
|
|
161
|
+
tr.delete(held.pos, held.pos + held.node.nodeSize).insert(
|
|
162
|
+
home,
|
|
163
|
+
held.node
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const floor = caretFloor(tr.doc);
|
|
168
|
+
if (floor !== null && tr.selection.empty && tr.selection.from < floor) {
|
|
169
|
+
tr.setSelection(
|
|
170
|
+
TextSelection.create(tr.doc, Math.min(floor, tr.doc.content.size))
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
return tr.docChanged || tr.selectionSet ? tr : null;
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
function holdsNothing(story) {
|
|
178
|
+
const only = story.childCount === 1 ? story.firstChild : null;
|
|
179
|
+
return only !== null && only.type === docxSchema.nodes.paragraph && only.content.size === leadingChipSize(story);
|
|
180
|
+
}
|
|
181
|
+
function deleteEmptyNote(main, key) {
|
|
182
|
+
return (state, dispatch) => {
|
|
183
|
+
if (!state.selection.empty) return false;
|
|
184
|
+
if (!holdsNothing(state.doc)) return false;
|
|
185
|
+
const $at = state.selection.$from;
|
|
186
|
+
if ($at.index(0) !== 0 || $at.parentOffset > leadingChipSize(state.doc)) {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
const found = referenceOf(main, key);
|
|
190
|
+
if (found === null) return false;
|
|
191
|
+
const tr = main.state.tr.delete(found.pos, found.pos + found.node.nodeSize);
|
|
192
|
+
if (!transactionAllowed(tr, main.state)) return false;
|
|
193
|
+
if (dispatch) {
|
|
194
|
+
main.dispatch(
|
|
195
|
+
tr.setSelection(TextSelection.near(tr.doc.resolve(found.pos))).scrollIntoView()
|
|
196
|
+
);
|
|
197
|
+
main.focus();
|
|
198
|
+
}
|
|
199
|
+
return true;
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
function noteHost(main, activate) {
|
|
203
|
+
return {
|
|
204
|
+
state: () => main.state,
|
|
205
|
+
write(key, story) {
|
|
206
|
+
const note = noteKeyOf(key);
|
|
207
|
+
return note !== null && runOn(main, noteBodyCommand(note.kind, note.id, story));
|
|
208
|
+
},
|
|
209
|
+
undo: () => runOn(main, undo),
|
|
210
|
+
redo: () => runOn(main, redo),
|
|
211
|
+
leave: (key) => returnToReference(main, key),
|
|
212
|
+
shut(key) {
|
|
213
|
+
if (editsShut(main.state)) return true;
|
|
214
|
+
const found = referenceOf(main, key);
|
|
215
|
+
if (found === null) return true;
|
|
216
|
+
return editShut(main.state, {
|
|
217
|
+
kind: "replace",
|
|
218
|
+
from: found.pos,
|
|
219
|
+
to: found.pos + found.node.nodeSize
|
|
220
|
+
});
|
|
221
|
+
},
|
|
222
|
+
activate
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
function noteExtensions(main, key, labelOf) {
|
|
226
|
+
return {
|
|
227
|
+
plugins: [
|
|
228
|
+
ownMarkFirst(),
|
|
229
|
+
keymap(keysOffTheNumber()),
|
|
230
|
+
keymap({ Backspace: deleteEmptyNote(main, key) })
|
|
231
|
+
],
|
|
232
|
+
nodeViews: {
|
|
233
|
+
rawRunContent: noteMarkView(labelOf, () => returnToReference(main, key))
|
|
234
|
+
},
|
|
235
|
+
normalizers: NOTE_NORMALIZERS,
|
|
236
|
+
takes: NOTE_TAKES
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
export {
|
|
240
|
+
dropNoteReferences,
|
|
241
|
+
dropUnwritableContent,
|
|
242
|
+
noteExtensions,
|
|
243
|
+
noteHost,
|
|
244
|
+
noteNodeSpecs,
|
|
245
|
+
returnToReference
|
|
246
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Takes the selected text away before a composition opens over it.
|
|
3
|
+
*
|
|
4
|
+
* A composition normally replaces what is selected because the browser does the replacing and
|
|
5
|
+
* ProseMirror reads the result back. Chrome will not touch a selection that begins at an element
|
|
6
|
+
* it may not edit - the number a note is drawn by, a note reference - and does not say so: the
|
|
7
|
+
* composition lands beside the words it was meant to replace, or nothing happens at all. A note
|
|
8
|
+
* meets it every time, since its first paragraph opens with its number and selecting the whole of
|
|
9
|
+
* a note to write it again is an ordinary thing to do.
|
|
10
|
+
*
|
|
11
|
+
* So the selection is deleted here, in the frame the composition opens in and before ProseMirror
|
|
12
|
+
* marks the view as composing, which leaves the composition starting from a caret with nothing
|
|
13
|
+
* for the browser to refuse. ProseMirror does the same for a selection spanning whole blocks
|
|
14
|
+
* (`endComposition`).
|
|
15
|
+
*
|
|
16
|
+
* The deletion is an edit like any other, so the guards judge it: across a bookmark marker it is
|
|
17
|
+
* refused, and the composition is left to the browser where it stood.
|
|
18
|
+
*/
|
|
19
|
+
import { Plugin } from "prosemirror-state";
|
|
20
|
+
export declare function compositionSelection(): Plugin;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// src/editor/plugins/compositionSelection.ts
|
|
2
|
+
import { deleteSelection } from "prosemirror-commands";
|
|
3
|
+
import { Plugin } from "prosemirror-state";
|
|
4
|
+
function compositionSelection() {
|
|
5
|
+
return new Plugin({
|
|
6
|
+
props: {
|
|
7
|
+
handleDOMEvents: {
|
|
8
|
+
compositionstart(view) {
|
|
9
|
+
if (view.state.selection.empty) return false;
|
|
10
|
+
deleteSelection(view.state, (tr) => view.dispatch(tr));
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
export {
|
|
18
|
+
compositionSelection
|
|
19
|
+
};
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* Keys not handled here are taken by ProseMirror's base keymap.
|
|
3
3
|
*/
|
|
4
4
|
import { type Command, Plugin } from "prosemirror-state";
|
|
5
|
+
import type { NoteKind } from "../../schema/stories";
|
|
5
6
|
/**
|
|
6
7
|
* Keeps undo and redo on their keys where the keymap above cannot reach them.
|
|
7
8
|
*
|
|
@@ -12,4 +13,17 @@ import { type Command, Plugin } from "prosemirror-state";
|
|
|
12
13
|
* second time.
|
|
13
14
|
*/
|
|
14
15
|
export declare function historyKeys(): Plugin;
|
|
16
|
+
/**
|
|
17
|
+
* Whether the editor is running on a Mac, which decides the Mod key and what the system takes
|
|
18
|
+
* before a page sees it.
|
|
19
|
+
*/
|
|
20
|
+
export declare function onMac(): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* The key each kind of note is put in on.
|
|
23
|
+
*
|
|
24
|
+
* They are Word's own, except that Word for Windows' endnote key is the system's on a Mac:
|
|
25
|
+
* Cmd+Option+D hides the Dock, so the editor is never asked about it, and Word for Mac puts an
|
|
26
|
+
* endnote on Cmd+Option+E. The footnote key is the same on both, which is Google Docs' as well.
|
|
27
|
+
*/
|
|
28
|
+
export declare const NOTE_KEYS: Readonly<Record<NoteKind, string>>;
|
|
15
29
|
export declare const docxKeymap: Record<string, Command>;
|
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
increaseListLevel,
|
|
23
23
|
leaveEmptyListItem
|
|
24
24
|
} from "../commands/listCommands.js";
|
|
25
|
+
import { insertEndnote, insertFootnote } from "../commands/noteCommands.js";
|
|
25
26
|
import { insertTab, moveAcrossTab } from "../commands/tabCommands.js";
|
|
26
27
|
import { openLinkPanel } from "./linkPanel.js";
|
|
27
28
|
var insertTabFromKeyboard = (state, dispatch, view) => {
|
|
@@ -91,6 +92,13 @@ function historyKeys() {
|
|
|
91
92
|
}
|
|
92
93
|
});
|
|
93
94
|
}
|
|
95
|
+
function onMac() {
|
|
96
|
+
return typeof navigator !== "undefined" && navigator.platform.includes("Mac");
|
|
97
|
+
}
|
|
98
|
+
var NOTE_KEYS = {
|
|
99
|
+
footnote: "Mod-Alt-f",
|
|
100
|
+
endnote: onMac() ? "Mod-Alt-e" : "Mod-Alt-d"
|
|
101
|
+
};
|
|
94
102
|
var docxKeymap = {
|
|
95
103
|
Enter: chainCommands(leaveEmptyListItem, splitParagraph),
|
|
96
104
|
"Shift-Enter": insertLineBreak,
|
|
@@ -107,6 +115,8 @@ var docxKeymap = {
|
|
|
107
115
|
// The link key Word and Google Docs share. It opens the panel over the selection, and with
|
|
108
116
|
// nothing there to link it reports that it did nothing, so the browser keeps its own Cmd+K
|
|
109
117
|
"Mod-k": openLinkPanel,
|
|
118
|
+
[NOTE_KEYS.footnote]: insertFootnote,
|
|
119
|
+
[NOTE_KEYS.endnote]: insertEndnote,
|
|
110
120
|
// Inside a table, moving between cells comes first (Word does the same).
|
|
111
121
|
// In a list paragraph outside a table it shifts the level; an ordinary paragraph gets a document tab.
|
|
112
122
|
Tab: chainCommands(
|
|
@@ -124,6 +134,8 @@ var docxKeymap = {
|
|
|
124
134
|
"Shift-ArrowRight": moveAcrossTab("right", true)
|
|
125
135
|
};
|
|
126
136
|
export {
|
|
137
|
+
NOTE_KEYS,
|
|
127
138
|
docxKeymap,
|
|
128
|
-
historyKeys
|
|
139
|
+
historyKeys,
|
|
140
|
+
onMac
|
|
129
141
|
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps each note of an editable kind in step with the references that call it.
|
|
3
|
+
*
|
|
4
|
+
* A note is written once and called from the text by its id, so what an edit does to the text
|
|
5
|
+
* decides what becomes of the note. An edit that takes away the last reference to a note takes the
|
|
6
|
+
* note away with it. An edit that copies a reference gives the copy a new id and a copy of the
|
|
7
|
+
* note, since two references naming one id are one note drawn with one number where the reader
|
|
8
|
+
* made two. A reference moved whole keeps its id and its note. Either change goes in a transaction
|
|
9
|
+
* appended to the edit, so it lands in the edit's history event and one undo takes both back.
|
|
10
|
+
*
|
|
11
|
+
* Only what the edit did is read. An entry the file arrived with and no reference to is never
|
|
12
|
+
* reached, a reference naming no note is left naming none, and the separators a notes part lays
|
|
13
|
+
* the page out with are left as they are. The notes part writer relies on the first of these: a
|
|
14
|
+
* note disappears only together with its last reference (`docx/storyParts`).
|
|
15
|
+
*/
|
|
16
|
+
import { Plugin } from "prosemirror-state";
|
|
17
|
+
import { type NoteKind } from "../../schema/stories";
|
|
18
|
+
export declare function noteLifecycle(kinds?: readonly NoteKind[]): Plugin;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// src/editor/plugins/noteLifecycle.ts
|
|
2
|
+
import { Plugin } from "prosemirror-state";
|
|
3
|
+
import { DocAttrStep, Mapping } from "prosemirror-transform";
|
|
4
|
+
import {
|
|
5
|
+
copiedNoteStory,
|
|
6
|
+
nextNoteId,
|
|
7
|
+
takenNoteIds
|
|
8
|
+
} from "../../docx/notes/newNote.js";
|
|
9
|
+
import { setStory, withoutStories } from "../../docx/story.js";
|
|
10
|
+
import { displayOnly } from "../../schema/displayDerivation.js";
|
|
11
|
+
import { stepReaches } from "../../schema/guards.js";
|
|
12
|
+
import {
|
|
13
|
+
EDITABLE_NOTE_KINDS,
|
|
14
|
+
storyKey,
|
|
15
|
+
storyNodeOf
|
|
16
|
+
} from "../../schema/stories.js";
|
|
17
|
+
import { documentOf } from "../editorDocument.js";
|
|
18
|
+
function kindOf(node, kinds) {
|
|
19
|
+
if (node.type.name !== "noteReference") return null;
|
|
20
|
+
return kinds.find((kind) => kind === node.attrs.kind) ?? null;
|
|
21
|
+
}
|
|
22
|
+
function referencesIn(doc, kinds) {
|
|
23
|
+
const found = [];
|
|
24
|
+
doc.descendants((node, pos) => {
|
|
25
|
+
const kind = kindOf(node, kinds);
|
|
26
|
+
const id = node.attrs.id;
|
|
27
|
+
if (kind !== null && typeof id === "string") {
|
|
28
|
+
found.push({ kind, key: storyKey(kind, id), pos });
|
|
29
|
+
}
|
|
30
|
+
return true;
|
|
31
|
+
});
|
|
32
|
+
return found;
|
|
33
|
+
}
|
|
34
|
+
function countsOf(references) {
|
|
35
|
+
return references.reduce(
|
|
36
|
+
(counts, { key }) => counts.set(key, (counts.get(key) ?? 0) + 1),
|
|
37
|
+
/* @__PURE__ */ new Map()
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
function reachesReferences(tr, kinds) {
|
|
41
|
+
if (!tr.docChanged || tr.getMeta(displayOnly) === true) return false;
|
|
42
|
+
return tr.steps.some(
|
|
43
|
+
(step, index) => !(step instanceof DocAttrStep) && stepReaches(
|
|
44
|
+
step,
|
|
45
|
+
tr.docs[index],
|
|
46
|
+
tr.docs[index + 1] ?? tr.doc,
|
|
47
|
+
(node) => kindOf(node, kinds) !== null
|
|
48
|
+
)
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
function copiedReferences(transactions, before, after) {
|
|
52
|
+
const found = countsOf(before);
|
|
53
|
+
const left = countsOf(after);
|
|
54
|
+
const mapping = new Mapping(transactions.flatMap((tr) => tr.mapping.maps));
|
|
55
|
+
const carried = new Set(
|
|
56
|
+
before.flatMap(({ key, pos }) => {
|
|
57
|
+
const mapped = mapping.mapResult(pos, 1);
|
|
58
|
+
return mapped.deletedAfter ? [] : [`${key}@${mapped.pos}`];
|
|
59
|
+
})
|
|
60
|
+
);
|
|
61
|
+
const wasCarried = ({ key, pos }) => carried.has(`${key}@${pos}`);
|
|
62
|
+
return [...left].flatMap(([key, count]) => {
|
|
63
|
+
const kept = found.get(key) ?? 0;
|
|
64
|
+
if (kept === 0 || count <= kept) return [];
|
|
65
|
+
const standing = after.filter((reference) => reference.key === key);
|
|
66
|
+
return [
|
|
67
|
+
...standing.filter(wasCarried),
|
|
68
|
+
...standing.filter((reference) => !wasCarried(reference))
|
|
69
|
+
].slice(kept);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
function settled(transactions, oldState, newState, kinds) {
|
|
73
|
+
const document = documentOf(newState);
|
|
74
|
+
const isNote = (key) => !document.specialNotes.has(key) && storyNodeOf(newState.doc, key) !== null;
|
|
75
|
+
const before = referencesIn(oldState.doc, kinds);
|
|
76
|
+
const after = referencesIn(newState.doc, kinds);
|
|
77
|
+
const left = countsOf(after);
|
|
78
|
+
const gone = [...countsOf(before).keys()].filter(
|
|
79
|
+
(key) => !left.has(key) && isNote(key)
|
|
80
|
+
);
|
|
81
|
+
const copies = copiedReferences(transactions, before, after).filter(({ key }) => isNote(key)).sort((a, b) => a.pos - b.pos);
|
|
82
|
+
if (gone.length === 0 && copies.length === 0) return null;
|
|
83
|
+
const tr = withoutStories(newState.tr, gone);
|
|
84
|
+
const taken = /* @__PURE__ */ new Map();
|
|
85
|
+
for (const copy of copies) {
|
|
86
|
+
const story = storyNodeOf(newState.doc, copy.key);
|
|
87
|
+
if (story === null) continue;
|
|
88
|
+
const ids = taken.get(copy.kind) ?? takenNoteIds(newState.doc, copy.kind, document.reservedNoteKeys);
|
|
89
|
+
taken.set(copy.kind, ids);
|
|
90
|
+
const id = nextNoteId(ids);
|
|
91
|
+
ids.add(id);
|
|
92
|
+
tr.setNodeAttribute(copy.pos, "id", id).setNodeAttribute(
|
|
93
|
+
copy.pos,
|
|
94
|
+
"referenceXml",
|
|
95
|
+
null
|
|
96
|
+
);
|
|
97
|
+
setStory(
|
|
98
|
+
tr,
|
|
99
|
+
storyKey(copy.kind, id),
|
|
100
|
+
copiedNoteStory(story, document.session)
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
return tr;
|
|
104
|
+
}
|
|
105
|
+
function noteLifecycle(kinds = EDITABLE_NOTE_KINDS) {
|
|
106
|
+
return new Plugin({
|
|
107
|
+
appendTransaction(transactions, oldState, newState) {
|
|
108
|
+
if (!transactions.some((tr) => reachesReferences(tr, kinds))) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
return settled(transactions, oldState, newState, kinds);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
export {
|
|
116
|
+
noteLifecycle
|
|
117
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Getting into a note and asking for one to be opened.
|
|
3
|
+
*
|
|
4
|
+
* A press on a note's number or a command asks for a note, and neither can mount anything itself,
|
|
5
|
+
* since what draws the notes is the surface around the editor; so the request is held in the state
|
|
6
|
+
* and the surface reads it back (`requestedNote`). The nonce is what makes a second request for
|
|
7
|
+
* the note already open reach the surface at all.
|
|
8
|
+
*
|
|
9
|
+
* The request moves the caret to just after the reference as it goes in, which is where Escape
|
|
10
|
+
* hands it back to (`editor/notes/noteSurface`), so leaving a note needs nothing remembered.
|
|
11
|
+
*/
|
|
12
|
+
import type { Node as PMNode } from "prosemirror-model";
|
|
13
|
+
import { type Command, type EditorState, Plugin, type Transaction } from "prosemirror-state";
|
|
14
|
+
import { type NoteKind, type StoryKey } from "../../schema/stories";
|
|
15
|
+
/** Which note the reader asked for, and which asking it was */
|
|
16
|
+
export interface RequestedNote {
|
|
17
|
+
readonly key: StoryKey;
|
|
18
|
+
/** Rises with every request, so asking twice for one note is two requests */
|
|
19
|
+
readonly nonce: number;
|
|
20
|
+
}
|
|
21
|
+
/** One reference to a note, as the document holds it */
|
|
22
|
+
export interface PlacedNoteReference {
|
|
23
|
+
readonly pos: number;
|
|
24
|
+
readonly node: PMNode;
|
|
25
|
+
}
|
|
26
|
+
/** Where the first reference to this note stands, and null where the document holds none */
|
|
27
|
+
export declare function noteReferenceAt(doc: PMNode, kind: NoteKind, id: string): PlacedNoteReference | null;
|
|
28
|
+
/** The transaction with the note it opens recorded on it, which `insertFootnote` also carries */
|
|
29
|
+
export declare function requestNote<T extends Transaction>(tr: T, key: StoryKey): T;
|
|
30
|
+
/** The note last asked for, and null where none was. Null for a state built without the plugin */
|
|
31
|
+
export declare function requestedNote(state: EditorState): RequestedNote | null;
|
|
32
|
+
/**
|
|
33
|
+
* Puts the caret just after the note's reference and asks for the note to be opened. It applies
|
|
34
|
+
* under every mode, since opening a note is reading it; what a reader may do once it stands open
|
|
35
|
+
* is the story view's question (`editor/stories`).
|
|
36
|
+
*/
|
|
37
|
+
export declare function openNoteCommand(kind: NoteKind, id: string): Command;
|
|
38
|
+
/**
|
|
39
|
+
* Holds the note last asked for, and turns a press on a note's number into that asking.
|
|
40
|
+
*
|
|
41
|
+
* One press is what opens a note, which is what Google Docs does: the number is a small
|
|
42
|
+
* superscript, and a press on one is far more often a reader going to the note than a caret being
|
|
43
|
+
* placed on the digit. A reference to a kind no writer puts back opens nothing.
|
|
44
|
+
*/
|
|
45
|
+
export declare function noteNavigation(kinds?: readonly NoteKind[]): Plugin<RequestedNote | null>;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// src/editor/plugins/noteNavigation.ts
|
|
2
|
+
import {
|
|
3
|
+
Plugin,
|
|
4
|
+
PluginKey,
|
|
5
|
+
TextSelection
|
|
6
|
+
} from "prosemirror-state";
|
|
7
|
+
import { docxSchema } from "../../schema/index.js";
|
|
8
|
+
import {
|
|
9
|
+
EDITABLE_NOTE_KINDS,
|
|
10
|
+
storyKey,
|
|
11
|
+
storyNodeOf
|
|
12
|
+
} from "../../schema/stories.js";
|
|
13
|
+
import { documentOf } from "../editorDocument.js";
|
|
14
|
+
var navigationKey = new PluginKey(
|
|
15
|
+
"docxEditorNoteNavigation"
|
|
16
|
+
);
|
|
17
|
+
function noteReferenceAt(doc, kind, id) {
|
|
18
|
+
let found = null;
|
|
19
|
+
doc.descendants((node, pos) => {
|
|
20
|
+
if (found !== null) return false;
|
|
21
|
+
if (node.type === docxSchema.nodes.noteReference && node.attrs.kind === kind && node.attrs.id === id) {
|
|
22
|
+
found = { pos, node };
|
|
23
|
+
}
|
|
24
|
+
return found === null;
|
|
25
|
+
});
|
|
26
|
+
return found;
|
|
27
|
+
}
|
|
28
|
+
function isRequest(value) {
|
|
29
|
+
if (typeof value !== "object" || value === null) return false;
|
|
30
|
+
return typeof Reflect.get(value, "key") === "string" && typeof Reflect.get(value, "nonce") === "number";
|
|
31
|
+
}
|
|
32
|
+
var asked = 0;
|
|
33
|
+
function requestNote(tr, key) {
|
|
34
|
+
asked += 1;
|
|
35
|
+
return tr.setMeta(navigationKey, { key, nonce: asked });
|
|
36
|
+
}
|
|
37
|
+
function requestedNote(state) {
|
|
38
|
+
return navigationKey.getState(state) ?? null;
|
|
39
|
+
}
|
|
40
|
+
function openNoteCommand(kind, id) {
|
|
41
|
+
return (state, dispatch) => {
|
|
42
|
+
const key = storyKey(kind, id);
|
|
43
|
+
if (documentOf(state).specialNotes.has(key)) return false;
|
|
44
|
+
if (storyNodeOf(state.doc, key) === null) return false;
|
|
45
|
+
const found = noteReferenceAt(state.doc, kind, id);
|
|
46
|
+
if (found === null) return false;
|
|
47
|
+
if (dispatch) {
|
|
48
|
+
const after = found.pos + found.node.nodeSize;
|
|
49
|
+
const tr = state.tr.setSelection(
|
|
50
|
+
TextSelection.near(state.doc.resolve(after))
|
|
51
|
+
);
|
|
52
|
+
dispatch(requestNote(tr, key).scrollIntoView());
|
|
53
|
+
}
|
|
54
|
+
return true;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function noteNavigation(kinds = EDITABLE_NOTE_KINDS) {
|
|
58
|
+
return new Plugin({
|
|
59
|
+
key: navigationKey,
|
|
60
|
+
state: {
|
|
61
|
+
init: () => null,
|
|
62
|
+
apply: (tr, held) => {
|
|
63
|
+
const next = tr.getMeta(navigationKey);
|
|
64
|
+
return isRequest(next) ? next : held;
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
props: {
|
|
68
|
+
handleClickOn(view, _pos, node, _nodePos, _event, direct) {
|
|
69
|
+
if (!direct || node.type !== docxSchema.nodes.noteReference) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
const kind = kinds.find((named) => named === node.attrs.kind);
|
|
73
|
+
const id = node.attrs.id;
|
|
74
|
+
if (kind === void 0 || typeof id !== "string") return false;
|
|
75
|
+
return openNoteCommand(kind, id)(view.state, view.dispatch);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
export {
|
|
81
|
+
noteNavigation,
|
|
82
|
+
noteReferenceAt,
|
|
83
|
+
openNoteCommand,
|
|
84
|
+
requestNote,
|
|
85
|
+
requestedNote
|
|
86
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Plugin } from "prosemirror-state";
|
|
2
|
+
import { noteLabelsIn } from "../../docx/notes/numbering";
|
|
3
|
+
export type NoteLabeller = typeof noteLabelsIn;
|
|
4
|
+
/**
|
|
5
|
+
* Labels the note references again after a change that moves one, moves a section break, or
|
|
6
|
+
* replaces the numbering the snapshot holds.
|
|
7
|
+
*
|
|
8
|
+
* A label is a display value of an inline node, which the display derivation walk does not reach,
|
|
9
|
+
* so it is written here under the same pass: a relabel goes through a lock around the reference,
|
|
10
|
+
* and one undo takes it back with the edit that caused it. The relabel after a snapshot change
|
|
11
|
+
* follows no edit and goes to the history not at all.
|
|
12
|
+
*/
|
|
13
|
+
export declare function noteNumbering(labelsIn?: NoteLabeller): Plugin;
|