-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathdocument-section.js
More file actions
413 lines (353 loc) · 13.4 KB
/
document-section.js
File metadata and controls
413 lines (353 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// @ts-check
/**
* Document section attributes
* @typedef {Object} SectionAttributes
* @property {number} [id] - Unique section identifier
* @property {string} [title] - Display label (becomes w:alias in Word)
* @property {string} [description] - Additional metadata stored in w:tag
* @property {string} [sectionType] - Business type for filtering/logic (e.g., 'legal', 'pricing')
* @property {boolean} [isLocked] - Lock state (maps to w:lock="sdtContentLocked")
*/
/**
* Create a new document section
* @typedef {Object} SectionCreate
* @property {number} [id] - Unique ID. Auto-increments from existing sections if omitted
* @property {string} [title="Document section"] - Label shown in section header
* @property {string} [description] - Metadata for tracking (stored in Word's w:tag)
* @property {string} [sectionType] - Business classification
* @property {boolean} [isLocked=false] - Prevent editing when true
* @property {string} [html] - HTML content to insert
* @property {Object} [json] - ProseMirror JSON (overrides html if both provided)
*/
/**
* Update an existing section
* @typedef {Object} SectionUpdate
* @property {number} id - Target section ID (required)
* @property {string} [html] - Replace content with HTML
* @property {Object} [json] - Replace content with ProseMirror JSON (overrides html)
* @property {Partial<SectionAttributes>} [attrs] - Update attributes only (preserves content)
*/
import { Node, Attribute } from '@core/index.js';
import { DocumentSectionView } from './document-section/DocumentSectionView.js';
import { htmlHandler } from '@core/InputRule.js';
import { Selection } from 'prosemirror-state';
import { DOMParser as PMDOMParser } from 'prosemirror-model';
import { findParentNode } from '@helpers/index.js';
import { SectionHelpers } from './document-section/helpers.js';
/**
* @module DocumentSection
* @sidebarTitle Document Section
* @snippetPath /snippets/extensions/document-section.mdx
*/
export const DocumentSection = Node.create({
name: 'documentSection',
group: 'block',
content: 'block*',
atom: true,
isolating: true,
addOptions() {
return {
htmlAttributes: {
class: 'sd-document-section-block',
'aria-label': 'Structured content block',
},
};
},
parseDOM() {
return [
{
tag: 'div.sd-document-section-block',
priority: 60,
},
];
},
renderDOM({ htmlAttributes }) {
return ['div', Attribute.mergeAttributes(this.options.htmlAttributes, htmlAttributes), 0];
},
addAttributes() {
return {
/**
* @category Attribute
* @param {number} [id] - Unique section identifier
*/
id: {},
/**
* @private
* @category Attribute
* @param {string} [sdBlockId] - Internal block tracking
*/
sdBlockId: {
default: null,
keepOnSplit: false,
parseDOM: (elem) => elem.getAttribute('data-sd-block-id'),
renderDOM: (attrs) => {
return attrs.sdBlockId ? { 'data-sd-block-id': attrs.sdBlockId } : {};
},
},
/**
* @category Attribute
* @param {string} [title] - Section display label
*/
title: {},
/**
* @category Attribute
* @param {string} [description] - Section metadata
*/
description: {},
/**
* @category Attribute
* @param {string} [sectionType] - Business classification (e.g., 'legal', 'pricing')
*/
sectionType: {},
/**
* @category Attribute
* @param {boolean} [isLocked=false] - Lock state preventing edits
*/
isLocked: { default: false },
};
},
addNodeView() {
return ({ node, editor, getPos, decorations }) => {
return new DocumentSectionView(node, getPos, decorations, editor);
};
},
addCommands() {
return {
/**
* Create a lockable content section
* @category Command
* @param {SectionCreate} [options={}] - Section configuration
* @returns {Function} Command - true if created, false if position invalid
* @example
* createDocumentSection({
* id: 'legal-1',
* title: 'Terms & Conditions',
* isLocked: true,
* html: '<p>Legal content...</p>'
* })
*/
createDocumentSection:
(options = {}) =>
({ tr, state, dispatch, editor }) => {
const { selection } = state;
let { from, to } = selection;
let content = selection.content().content;
const { html: optionsHTML, json: optionsJSON } = options;
// If HTML is provided, parse it and convert to ProseMirror nodes
if (optionsHTML) {
const html = htmlHandler(optionsHTML, this.editor);
const doc = PMDOMParser.fromSchema(this.editor.schema).parse(html);
content = doc.content;
}
// JSON takes priority over HTML
if (optionsJSON) {
content = this.editor.schema.nodeFromJSON(optionsJSON);
}
if (!content?.content?.length) {
content = this.editor.schema.nodeFromJSON({ type: 'paragraph', content: [] });
}
// We assign IDs as positive integers starting from 0.
if (!options.id) {
const allSections = SectionHelpers.getAllSections(editor);
options.id = allSections.length + 1;
}
if (!options.title) {
options.title = 'Document section';
}
const node = this.type.createAndFill(options, content);
if (!node) return false;
const isAlreadyInSdtBlock = findParentNode((node) => node.type.name === 'documentSection')(selection);
if (isAlreadyInSdtBlock && isAlreadyInSdtBlock.node) {
const insertPos = isAlreadyInSdtBlock.pos + isAlreadyInSdtBlock.node.nodeSize;
from = insertPos;
to = insertPos;
}
// Replace the selection with the new node
tr.replaceRangeWith(from, to, node);
// Calculate where the node ends after insertion
const nodeEnd = from + node.nodeSize;
// Only insert paragraph if we're not at the document boundary and there's space
let shouldInsertParagraph = true;
let insertPos = nodeEnd;
// Check if we can safely insert at this position
if (nodeEnd >= tr.doc.content.size) {
// We're at or beyond the document end
insertPos = tr.doc.content.size;
// Check if there's already content at the end
if (insertPos > 0) {
const $endPos = tr.doc.resolve(insertPos);
if ($endPos.nodeBefore && $endPos.nodeBefore.type.name === 'paragraph') {
shouldInsertParagraph = false; // There's already a paragraph
}
}
}
if (shouldInsertParagraph) {
const emptyParagraph = tr.doc.type.schema.nodes.paragraph.create();
tr.insert(insertPos, emptyParagraph);
}
if (dispatch) {
tr.setMeta('documentSection', { action: 'create' });
dispatch(tr);
// Set selection after the DOM has updated
setTimeout(() => {
try {
const currentState = editor.state;
const docSize = currentState.doc.content.size;
// Calculate target position more safely
let targetPos = from + node.nodeSize;
// If we inserted a paragraph, position inside it
if (shouldInsertParagraph) {
targetPos += 1; // +1 to get inside the paragraph
}
// Ensure we don't go beyond document bounds
targetPos = Math.min(targetPos, docSize);
// Ensure we have a valid position (at least 1 if document has content)
if (targetPos < docSize && targetPos > 0) {
const newSelection = Selection.near(currentState.doc.resolve(targetPos));
const newTr = currentState.tr.setSelection(newSelection);
editor.view.dispatch(newTr);
}
} catch (e) {
console.warn('Could not set delayed selection:', e);
}
}, 0);
}
return true;
},
/**
* Remove section wrapper at cursor, preserving its content
* @category Command
* @returns {Function} Command - true if removed, false if no section at position
* @example
* removeSectionAtSelection()
* @note Content stays in document, only section wrapper is removed
*/
removeSectionAtSelection:
() =>
({ tr, dispatch }) => {
const sdtNode = findParentNode((node) => node.type.name === 'documentSection')(tr.selection);
if (!sdtNode) return false;
const { node, pos } = sdtNode;
// Calculate positions before making changes
const nodeStart = pos;
const nodeEnd = nodeStart + node.nodeSize;
// Extract the content we want to preserve
const contentToPreserve = node.content;
// Delete the entire structured content block
tr.delete(nodeStart, nodeEnd);
// Insert the preserved content at the same position
if (contentToPreserve.size > 0) {
tr.insert(nodeStart, contentToPreserve);
}
// Set selection to a safe position after the operation
const newPos = Math.min(nodeStart, tr.doc.content.size);
tr.setSelection(Selection.near(tr.doc.resolve(newPos)));
if (dispatch) {
tr.setMeta('documentSection', { action: 'delete' });
dispatch(tr);
}
return true;
},
/**
* Delete section and all its content
* @category Command
* @param {number} id - Section to delete
* @returns {Function} Command - true if deleted, false if ID doesn't exist
* @example
* removeSectionById(123)
*/
removeSectionById:
(id) =>
({ tr, dispatch }) => {
const sections = SectionHelpers.getAllSections(this.editor);
const sectionToRemove = sections.find(({ node }) => node.attrs.id === id);
if (!sectionToRemove) return false;
const { pos, node } = sectionToRemove;
const nodeStart = pos;
const nodeEnd = nodeStart + node.nodeSize;
// Delete the entire structured content block
tr.delete(nodeStart, nodeEnd);
if (dispatch) {
tr.setMeta('documentSection', { action: 'delete', id });
dispatch(tr);
}
return true;
},
/**
* Lock section against edits
* @category Command
* @param {number} id - Section to lock
* @returns {Function} Command - true if locked, false if ID doesn't exist
* @example
* lockSectionById(123)
*/
lockSectionById:
(id) =>
({ tr, dispatch }) => {
const sections = SectionHelpers.getAllSections(this.editor);
const sectionToLock = sections.find(({ node }) => node.attrs.id === id);
if (!sectionToLock) return false;
tr.setNodeMarkup(sectionToLock.pos, null, { ...sectionToLock.node.attrs, isLocked: true });
if (dispatch) {
tr.setMeta('documentSection', { action: 'lock', id });
dispatch(tr);
}
return true;
},
/**
* Modify section attributes or content
* @category Command
* @param {SectionUpdate} options - Changes to apply
* @returns {Function} Command - true if updated, false if ID doesn't exist
* @example
* // Toggle lock
* updateSectionById({ id: 123, attrs: { isLocked: false } })
*
* // Replace content
* updateSectionById({ id: 123, html: '<p>New content</p>' })
*
* // Both
* updateSectionById({
* id: 123,
* html: '<p>Updated</p>',
* attrs: { title: 'New Title' }
* })
*/
updateSectionById:
({ id, html, json, attrs }) =>
({ tr, dispatch, editor }) => {
const sections = SectionHelpers.getAllSections(editor || this.editor);
const sectionToUpdate = sections.find(({ node }) => node.attrs.id === id);
if (!sectionToUpdate) return false;
const { pos, node } = sectionToUpdate;
let newContent = null;
// If HTML is provided, parse it and convert to ProseMirror nodes
if (html) {
const htmlDoc = htmlHandler(html, editor || this.editor);
const doc = PMDOMParser.fromSchema((editor || this.editor).schema).parse(htmlDoc);
newContent = doc.content;
}
// JSON takes priority over HTML
if (json) {
newContent = (editor || this.editor).schema.nodeFromJSON(json);
}
// If no new content, keep the old content
if (!newContent) {
newContent = node.content;
}
const updatedNode = node.type.create({ ...node.attrs, ...attrs }, newContent, node.marks);
tr.replaceWith(pos, pos + node.nodeSize, updatedNode);
if (dispatch) {
tr.setMeta('documentSection', { action: 'update', id, attrs });
dispatch(tr);
}
return true;
},
};
},
addHelpers() {
return {
...SectionHelpers,
};
},
});