Skip to content

Commit f99ded3

Browse files
committed
docs(structured-content): add live SDT fields to interactive demo
Use onReady callback to auto-insert inline and block sdtLocked fields into the example document on load, replacing placeholder text with real structured content fields. Reorganize buttons into logical groups: update actions, lock mode actions, and delete.
1 parent a29cca6 commit f99ded3

1 file changed

Lines changed: 49 additions & 52 deletions

File tree

apps/docs/snippets/extensions/structured-content.mdx

Lines changed: 49 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,68 +10,69 @@ Native Word SDT (w:sdt) fields for documents. Supports inline and block structur
1010
import { SuperDocEditor } from '/snippets/components/superdoc-editor.jsx'
1111

1212
<SuperDocEditor
13-
html={`<p><b>Service Agreement</b></p><p>This agreement is entered into by and between <i>Acme Corp</i> ("Company") and the client named below. The Company agrees to provide consulting services as outlined in the attached statement of work.</p><p><b>Client Information</b></p><p>Name: ________&nbsp;&nbsp;&nbsp;&nbsp;Date: January 1, 2026</p><p><b>Terms</b></p><p>Payment is due within 30 days of invoice date. All work products remain property of the Company until final payment is received.</p>`}
14-
height="300px"
13+
html={`<p><b>Service Agreement</b></p><p>This agreement is entered into by and between <i>Acme Corp</i> ("Company") and the client identified below.</p><p>Client name: ________</p><p>The Company agrees to provide consulting services under the following terms:</p><p>________</p>`}
14+
height="350px"
15+
onReady={(superdoc) => {
16+
const editor = superdoc?.activeEditor || superdoc?.editor
17+
if (!editor?.commands) return
18+
19+
// Find "________" in "Client name:" paragraph and replace with inline sdtLocked field
20+
let inlinePos = null
21+
editor.state.doc.descendants((node, pos) => {
22+
if (node.isText && node.text.includes('________') && inlinePos === null) {
23+
inlinePos = pos + node.text.indexOf('________')
24+
}
25+
})
26+
if (inlinePos !== null) {
27+
editor.commands.setTextSelection({ from: inlinePos, to: inlinePos + 8 })
28+
editor.commands.insertStructuredContentInline({
29+
attrs: { id: '1', alias: 'Client Name', lockMode: 'sdtLocked' },
30+
text: 'John Doe',
31+
})
32+
}
33+
34+
// Find the second "________" paragraph and replace with block sdtLocked field
35+
let blockPos = null
36+
editor.state.doc.descendants((node, pos) => {
37+
if (node.isText && node.text.includes('________')) {
38+
blockPos = pos + node.text.indexOf('________')
39+
}
40+
})
41+
if (blockPos !== null) {
42+
editor.commands.setTextSelection({ from: blockPos, to: blockPos + 8 })
43+
editor.commands.insertStructuredContentBlock({
44+
attrs: { id: '2', alias: 'Payment Terms', lockMode: 'sdtLocked' },
45+
json: { type: 'paragraph', content: [{ type: 'text', text: 'Payment is due within 30 days of invoice date. Late payments incur a 1.5% monthly fee.' }] },
46+
})
47+
}
48+
}}
1549
customButtons={[
1650
[
1751
{
18-
label: 'Insert inline (sdtLocked)',
52+
label: 'Update client name',
1953
onClick: (superdoc) => {
2054
const editor = superdoc?.activeEditor || superdoc?.editor
2155
if (!editor?.commands) return
22-
23-
editor.commands.insertStructuredContentInline({
24-
attrs: {
25-
id: '1',
26-
alias: 'Client Name',
27-
lockMode: 'sdtLocked',
28-
},
29-
text: 'John Doe',
30-
});
31-
}
32-
},
33-
{
34-
label: 'Insert block (sdtLocked)',
35-
onClick: (superdoc) => {
36-
const editor = superdoc?.activeEditor || superdoc?.editor
37-
if (!editor?.commands) return
38-
39-
editor.commands.insertStructuredContentBlock({
40-
attrs: {
41-
id: '2',
42-
alias: 'Payment Terms',
43-
lockMode: 'sdtLocked',
44-
},
45-
json: { type: 'paragraph', content: [{ type: 'text', text: 'Payment is due within 30 days of invoice date. Late payments incur a 1.5% monthly fee. All disputes must be submitted in writing within 10 business days.' }] }
46-
});
47-
},
48-
},
49-
{
50-
label: 'Update inline field',
51-
onClick: (superdoc) => {
52-
const editor = superdoc?.activeEditor || superdoc?.editor
53-
if (!editor?.commands) return
54-
5556
editor.commands.updateStructuredContentById('1', { text: 'Jane Smith' });
5657
}
5758
},
5859
{
59-
label: 'Update block field',
60+
label: 'Update payment terms',
6061
onClick: (superdoc) => {
6162
const editor = superdoc?.activeEditor || superdoc?.editor
6263
if (!editor?.commands) return
63-
6464
editor.commands.updateStructuredContentById('2', {
6565
json: { type: 'paragraph', content: [{ type: 'text', text: 'Net-15 payment terms apply. A 2% early payment discount is available for invoices settled within 7 days.' }] }
6666
});
6767
}
6868
},
69+
],
70+
[
6971
{
7072
label: 'Lock inline (sdtContentLocked)',
7173
onClick: (superdoc) => {
7274
const editor = superdoc?.activeEditor || superdoc?.editor
7375
if (!editor?.commands) return
74-
7576
editor.commands.updateStructuredContentById('1', {
7677
attrs: { lockMode: 'sdtContentLocked' },
7778
});
@@ -82,32 +83,27 @@ import { SuperDocEditor } from '/snippets/components/superdoc-editor.jsx'
8283
onClick: (superdoc) => {
8384
const editor = superdoc?.activeEditor || superdoc?.editor
8485
if (!editor?.commands) return
85-
8686
editor.commands.updateStructuredContentById('2', {
8787
attrs: { lockMode: 'sdtContentLocked' },
8888
});
8989
}
9090
},
9191
{
92-
label: 'Unlock all fields',
92+
label: 'Unlock all',
9393
onClick: (superdoc) => {
9494
const editor = superdoc?.activeEditor || superdoc?.editor
9595
if (!editor?.commands) return
96-
97-
editor.commands.updateStructuredContentById('1', {
98-
attrs: { lockMode: 'unlocked' },
99-
});
100-
editor.commands.updateStructuredContentById('2', {
101-
attrs: { lockMode: 'unlocked' },
102-
});
96+
editor.commands.updateStructuredContentById('1', { attrs: { lockMode: 'unlocked' } });
97+
editor.commands.updateStructuredContentById('2', { attrs: { lockMode: 'unlocked' } });
10398
}
10499
},
100+
],
101+
[
105102
{
106103
label: 'Delete all fields',
107104
onClick: (superdoc) => {
108105
const editor = superdoc?.activeEditor || superdoc?.editor
109106
if (!editor?.commands) return
110-
111107
const fields = editor.helpers.structuredContentCommands.getStructuredContentTags(editor.state);
112108
editor.commands.deleteStructuredContent(fields);
113109
}
@@ -119,12 +115,12 @@ import { SuperDocEditor } from '/snippets/components/superdoc-editor.jsx'
119115
## Quick start
120116

121117
```javascript
122-
// Insert inline field for customer name
118+
// Insert inline field with sdtLocked — users can edit content but not delete the field
123119
editor.commands.insertStructuredContentInline({
124120
attrs: {
125121
id: '1',
126122
alias: 'Customer Name',
127-
lockMode: 'sdtLocked', // users can edit the value but not delete the field
123+
lockMode: 'sdtLocked',
128124
},
129125
text: 'John Doe'
130126
});
@@ -139,11 +135,12 @@ editor.commands.insertStructuredContentInline({
139135
text: 'ACC-00042'
140136
});
141137

142-
// Insert block field for terms section
138+
// Insert block field with sdtLocked — content is editable, wrapper is protected
143139
editor.commands.insertStructuredContentBlock({
144140
attrs: {
145141
id: '2',
146142
alias: 'Terms & Conditions',
143+
lockMode: 'sdtLocked',
147144
},
148145
html: '<p>Please review the terms...</p>'
149146
});

0 commit comments

Comments
 (0)