Skip to content

Commit 37a4a6a

Browse files
authored
refactor: enhance documentation for section attributes and commands in DocumentSection (#805)
1 parent 793c9f0 commit 37a4a6a

1 file changed

Lines changed: 62 additions & 35 deletions

File tree

packages/super-editor/src/extensions/structured-content/document-section.js

Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
// @ts-check
22

33
/**
4-
* Base section attributes
4+
* Document section attributes
55
* @typedef {Object} SectionAttributes
6-
* @property {number} [id] - Section identifier
7-
* @property {string} [title] - Section title
8-
* @property {string} [description] - Section description
9-
* @property {string} [sectionType] - Type of section
10-
* @property {boolean} [isLocked] - Whether section is locked
6+
* @property {number} [id] - Unique section identifier
7+
* @property {string} [title] - Display label (becomes w:alias in Word)
8+
* @property {string} [description] - Additional metadata stored in w:tag
9+
* @property {string} [sectionType] - Business type for filtering/logic (e.g., 'legal', 'pricing')
10+
* @property {boolean} [isLocked] - Lock state (maps to w:lock="sdtContentLocked")
1111
*/
1212

1313
/**
14-
* Options for creating a section
14+
* Create a new document section
1515
* @typedef {Object} SectionCreate
16-
* @property {number} [id] - Section identifier (auto-generated if not provided)
17-
* @property {string} [title] - Section title (defaults to "Document section")
18-
* @property {string} [description] - Section description
19-
* @property {string} [sectionType] - Type of section
20-
* @property {boolean} [isLocked] - Whether section is locked
21-
* @property {string} [html] - HTML content to parse
22-
* @property {Object} [json] - ProseMirror JSON content (takes precedence over html)
16+
* @property {number} [id] - Unique ID. Auto-increments from existing sections if omitted
17+
* @property {string} [title="Document section"] - Label shown in section header
18+
* @property {string} [description] - Metadata for tracking (stored in Word's w:tag)
19+
* @property {string} [sectionType] - Business classification
20+
* @property {boolean} [isLocked=false] - Prevent editing when true
21+
* @property {string} [html] - HTML content to insert
22+
* @property {Object} [json] - ProseMirror JSON (overrides html if both provided)
2323
*/
2424

2525
/**
26-
* Options for updating a section
26+
* Update an existing section
2727
* @typedef {Object} SectionUpdate
28-
* @property {number} id - Section ID to update (required)
29-
* @property {string} [html] - HTML content to parse
30-
* @property {Object} [json] - ProseMirror JSON content (takes precedence over html)
31-
* @property {Partial<SectionAttributes>} [attrs] - Attributes to update
28+
* @property {number} id - Target section ID (required)
29+
* @property {string} [html] - Replace content with HTML
30+
* @property {Object} [json] - Replace content with ProseMirror JSON (overrides html)
31+
* @property {Partial<SectionAttributes>} [attrs] - Update attributes only (preserves content)
3232
*/
3333

3434
import { Node, Attribute } from '@core/index.js';
@@ -39,8 +39,9 @@ import { DOMParser as PMDOMParser } from 'prosemirror-model';
3939
import { findParentNode, SectionHelpers } from '@helpers/index.js';
4040

4141
/**
42-
* Document Section - Structured content blocks
4342
* @module DocumentSection
43+
* @sidebarTitle Document Section
44+
* @snippetPath /snippets/extensions/document-section.mdx
4445
*/
4546
export const DocumentSection = Node.create({
4647
name: 'documentSection',
@@ -98,10 +99,17 @@ export const DocumentSection = Node.create({
9899
addCommands() {
99100
return {
100101
/**
101-
* Create a new document section
102+
* Create a lockable content section
102103
* @category Command
103-
* @param {SectionCreate} [options={}]
104-
* @returns {Function} Command function - returns true if section was created successfully
104+
* @param {SectionCreate} [options={}] - Section configuration
105+
* @returns {Function} Command - true if created, false if position invalid
106+
* @example
107+
* createDocumentSection({
108+
* id: 'legal-1',
109+
* title: 'Terms & Conditions',
110+
* isLocked: true,
111+
* html: '<p>Legal content...</p>'
112+
* })
105113
*/
106114
createDocumentSection:
107115
(options = {}) =>
@@ -214,9 +222,12 @@ export const DocumentSection = Node.create({
214222
},
215223

216224
/**
217-
* Remove section at current selection, keeping content
225+
* Remove section wrapper at cursor, preserving its content
218226
* @category Command
219-
* @returns {Function} Command function - returns true if section was removed, false if none found
227+
* @returns {Function} Command - true if removed, false if no section at position
228+
* @example
229+
* removeSectionAtSelection()
230+
* @note Content stays in document, only section wrapper is removed
220231
*/
221232
removeSectionAtSelection:
222233
() =>
@@ -254,10 +265,12 @@ export const DocumentSection = Node.create({
254265
},
255266

256267
/**
257-
* Remove section by ID
268+
* Delete section and all its content
258269
* @category Command
259-
* @param {number} id - Section ID to remove
260-
* @returns {Function} Command function - returns true if section was removed, false if not found
270+
* @param {number} id - Section to delete
271+
* @returns {Function} Command - true if deleted, false if ID doesn't exist
272+
* @example
273+
* removeSectionById(123)
261274
*/
262275
removeSectionById:
263276
(id) =>
@@ -282,11 +295,12 @@ export const DocumentSection = Node.create({
282295
},
283296

284297
/**
285-
* Lock section by ID
298+
* Lock section against edits
286299
* @category Command
287-
* @param {number} id - Section ID to lock
288-
* @returns {Function} Command function - returns true if section was locked, false if not found
289-
* @private
300+
* @param {number} id - Section to lock
301+
* @returns {Function} Command - true if locked, false if ID doesn't exist
302+
* @example
303+
* lockSectionById(123)
290304
*/
291305
lockSectionById:
292306
(id) =>
@@ -306,10 +320,23 @@ export const DocumentSection = Node.create({
306320
},
307321

308322
/**
309-
* Update section by ID
323+
* Modify section attributes or content
310324
* @category Command
311-
* @param {SectionUpdate} options
312-
* @returns {Function} Command function - returns true if section was updated, false if not found
325+
* @param {SectionUpdate} options - Changes to apply
326+
* @returns {Function} Command - true if updated, false if ID doesn't exist
327+
* @example
328+
* // Toggle lock
329+
* updateSectionById({ id: 123, attrs: { isLocked: false } })
330+
*
331+
* // Replace content
332+
* updateSectionById({ id: 123, html: '<p>New content</p>' })
333+
*
334+
* // Both
335+
* updateSectionById({
336+
* id: 123,
337+
* html: '<p>Updated</p>',
338+
* attrs: { title: 'New Title' }
339+
* })
313340
*/
314341
updateSectionById:
315342
({ id, html, json, attrs }) =>

0 commit comments

Comments
 (0)