-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock.js
More file actions
390 lines (342 loc) · 11.7 KB
/
Copy pathBlock.js
File metadata and controls
390 lines (342 loc) · 11.7 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
import { uid } from './uid.js'
import { cloneEditorData } from '../shared/cloneEditorData.js'
import { el } from './dom.js'
import {
BLOCK_CLASS,
READ_ONLY_INTERACTIVE_ATTRIBUTE,
TEXT_ALIGN_TUNE_ATTRIBUTE,
} from './constants.js'
import { normalizeTextAlign } from '../shared/textFormat.js'
/** @typedef {import('./types').IBlock} IBlockContract */
/** @implements {IBlockContract} */
export class Block {
/** @type {string} */
#id
/** @type {string} */
#type
/** @type {import('./types').BlockPlugin} */
#plugin
/** @type {HTMLElement} wrapper div.oe-block */
#element
/** @type {HTMLElement} inner element from plugin.render() */
#contentElement
/** @type {boolean} */
#focused = false
/** @type {boolean} */
#selected = false
/** @type {Record<string, unknown> | null} */
#cachedData = null
/** @type {boolean} */
#dirty = true
/** @type {number} */
#version = 0
#readOnly
/** @type {Record<string, unknown> | undefined} */
#tunes
/** @type {string | number | undefined} */
#revision
/** @type {Record<string, import('../renderer/types').InlineWidget> | undefined} */
#preservedInline
/** @type {import('./CommandDispatcher').CommandDispatcher} */
#commands
/** @type {boolean} */
#destroyed = false
/** @type {(() => void) | null} */
#splitBlock = null
/** @type {(() => boolean) | null} */
#exitEmptyBlock = null
/**
* @param {import('./types').BlockPlugin} plugin
* @param {import('./CommandDispatcher').CommandDispatcher} commands
* @param {Record<string, unknown>} [data]
* @param {string} [id]
* @param {boolean} [readOnly]
* @param {{ tunes?: Record<string, unknown>, revision?: string | number, inline?: Record<string, import('../renderer/types').InlineWidget>, preserveInline?: boolean }} [metadata]
*/
constructor(plugin, commands, data, id, readOnly = false, metadata = {}) {
this.#id = id || uid()
this.#type = plugin.type
this.#plugin = plugin
this.#readOnly = readOnly
this.#commands = commands
this.#tunes = metadata.tunes === undefined ? undefined : cloneEditorData(metadata.tunes)
this.#revision = metadata.revision
this.#preservedInline = metadata.preserveInline && metadata.inline
? cloneEditorData(metadata.inline)
: undefined
this.#element = el('div', BLOCK_CLASS, {
'data-block-id': this.#id,
'data-block-type': this.#type,
})
const contentElement = plugin.render(data || {}, {
mutate: (operation) => this.#runMutation(operation),
splitBlock: () => this.#splitBlock?.(),
exitEmptyBlock: () => this.#exitEmptyBlock?.() ?? false,
readOnly: this.#readOnly,
})
if (!(contentElement instanceof HTMLElement)) {
throw new TypeError(`Block plugin "${this.#type}" render() must return an HTMLElement`)
}
this.#contentElement = contentElement
this.#applyTextAlignTune()
this.#applyReadOnly()
this.#element.appendChild(this.#contentElement)
}
/**
* Core-owned command boundary for interactive block-plugin controls.
* Plugins mutate their own DOM, while the Block remains responsible for
* cache invalidation, change events, and the final history commit.
* @template T
* @param {() => T} operation
* @returns {T | undefined}
*/
#runMutation(operation) {
if (this.#destroyed || this.#readOnly) return undefined
return this.#commands.runForBlock(this, operation)
}
/**
* Connect core structural commands used by interactive plugin controls.
* The callbacks are cleared while the editor is in read-only mode.
* @param {{ splitBlock: (() => void) | null, exitEmptyBlock: (() => boolean) | null }} commands
* @returns {void}
*/
setStructuralCommands(commands) {
this.#splitBlock = commands.splitBlock
this.#exitEmptyBlock = commands.exitEmptyBlock
}
get id() {
return this.#id
}
get type() {
return this.#type
}
get plugin() {
return this.#plugin
}
get element() {
return this.#element
}
get contentElement() {
return this.#contentElement
}
get focused() {
return this.#focused
}
set focused(value) {
this.#focused = value
this.#element.classList.toggle('oe-block--focused', value)
}
get selected() {
return this.#selected
}
set selected(value) {
this.#selected = value
this.#element.classList.toggle('oe-block--selected', value)
}
/**
* Extract block data from DOM.
* @returns {import('./types').BlockData}
*/
save() {
if (this.#dirty || !this.#cachedData) {
const saved = this.#plugin.save(this.#contentElement)
if (!saved || typeof saved !== 'object' || Array.isArray(saved)) {
throw new TypeError(`Block plugin "${this.#type}" save() must return a data object`)
}
this.#cachedData = cloneEditorData(saved)
this.#syncTextAlignTune()
this.#dirty = false
}
const data = cloneEditorData(this.#cachedData)
const result = { id: this.#id, type: this.#type, data }
if (this.#revision !== undefined) result.revision = this.#revision
if (this.#tunes !== undefined) result.tunes = cloneEditorData(this.#tunes)
if (this.#preservedInline !== undefined) result.inline = cloneEditorData(this.#preservedInline)
return result
}
/** Mark cached serialization stale after a block-local mutation. */
markDirty() {
this.#dirty = true
// A producer-owned revision only describes the input block. Once the
// editor changes that block it can no longer claim the old revision.
this.#revision = undefined
this.#version++
}
/** Monotonic version used by incremental consumers. */
get version() {
return this.#version
}
/**
* Merge another block's data into this one.
* @param {Record<string, unknown>} data
*/
merge(data) {
if (this.#plugin.merge) {
this.#plugin.merge(this.#contentElement, data)
}
this.markDirty()
}
/**
* Whether this block's plugin supports merge.
* @returns {boolean}
*/
get canMerge() {
return typeof this.#plugin.merge === 'function'
}
/**
* Check if the block content is empty.
* Delegates to plugin.isEmpty() if defined, else checks textContent.
* @returns {boolean}
*/
isEmpty() {
if (typeof this.#plugin.isEmpty === 'function') {
return this.#plugin.isEmpty(this.#contentElement)
}
const text = (this.#contentElement.textContent || '').trim()
return text.length === 0
}
/**
* Check if the plugin supports inline tools.
* @returns {boolean}
*/
get hasInlineTools() {
const setting = this.#plugin.inlineTools
return setting !== false && (!Array.isArray(setting) || setting.length > 0)
}
/**
* Tool types allowed by this block, or null when the editor-wide set applies.
* @returns {readonly string[] | null}
*/
get inlineToolTypes() {
return Array.isArray(this.#plugin.inlineTools)
? this.#plugin.inlineTools
: null
}
/**
* Get the block's settings UI (if plugin provides one).
* @returns {HTMLElement | HTMLElement[] | null}
*/
renderSettings() {
return this.#plugin.renderSettings?.(this.#contentElement) ?? null
}
/**
* Adopt a new content element as this block's content.
*
* Two paths, automatically chosen:
* 1. The plugin already swapped the DOM itself (`oldEl.replaceWith(newEl)`),
* so `newEl` is already a child of `#element` — we only need to update
* the internal reference. This is the case for Heading.changeLevel.
* 2. The plugin returned a fresh detached element — we swap it in via
* `replaceChild`.
*
* Without the first branch, calling this after a DOM swap performed by the plugin
* throws `NotFoundError: replaceChild — node is not a child of this node`,
* because the old contentElement is already detached.
*
* @param {HTMLElement} newEl
*/
replaceContentElement(newEl) {
if (newEl === this.#contentElement) return
if (newEl.parentNode === this.#element) {
this.markDirty()
// Plugin already performed the DOM swap; just track the new ref.
this.#contentElement = newEl
this.#applyTextAlignTune()
return
}
this.#element.replaceChild(newEl, this.#contentElement)
this.markDirty()
this.#contentElement = newEl
this.#applyTextAlignTune()
}
/** Apply a valid serialized block alignment to the plugin root. */
#applyTextAlignTune() {
const align = normalizeTextAlign(this.#tunes?.textAlign)
if (!align) return
this.#contentElement.style.textAlign = align
this.#contentElement.setAttribute(TEXT_ALIGN_TUNE_ATTRIBUTE, align)
}
/**
* Copy an alignment explicitly changed by the toolbar into block tunes.
* Plugin-owned `data.align` remains readable and writable for Paragraph and
* Heading, while the tune gives every other text block one stable contract.
*/
#syncTextAlignTune() {
if (!this.#contentElement.hasAttribute(TEXT_ALIGN_TUNE_ATTRIBUTE)) return
const align = normalizeTextAlign(this.#contentElement.getAttribute(TEXT_ALIGN_TUNE_ATTRIBUTE))
const nextTunes = this.#tunes ? cloneEditorData(this.#tunes) : {}
if (align) nextTunes.textAlign = align
else delete nextTunes.textAlign
this.#tunes = Object.keys(nextTunes).length > 0 ? nextTunes : undefined
}
/**
* Enforce read-only as a DOM invariant instead of relying on pointer events.
*/
#applyReadOnly() {
if (!this.#readOnly) return
this.#contentElement.setAttribute('aria-readonly', 'true')
/** @type {HTMLElement[]} */
const editableElements = []
if (this.#contentElement.matches('[contenteditable]')) {
editableElements.push(this.#contentElement)
}
for (const editableElement of this.#contentElement.querySelectorAll('[contenteditable]')) {
if (editableElement instanceof HTMLElement) editableElements.push(editableElement)
}
for (const editableElement of editableElements) {
editableElement.contentEditable = 'false'
}
for (const field of this.#contentElement.querySelectorAll('input, textarea')) {
if (field instanceof HTMLInputElement) field.readOnly = true
else if (field instanceof HTMLTextAreaElement) field.readOnly = true
}
for (const control of this.#contentElement.querySelectorAll('button, select')) {
if (control.hasAttribute(READ_ONLY_INTERACTIVE_ATTRIBUTE)) continue
if (control instanceof HTMLButtonElement) control.disabled = true
else if (control instanceof HTMLSelectElement) control.disabled = true
}
}
/**
* Clean up the plugin without removing the block element from DOM.
* Used by BlockManager.convert() which handles element removal separately.
*/
disposePlugin() {
if (this.#destroyed) return
this.#destroyed = true
if (this.#plugin.destroy) {
try {
this.#plugin.destroy(this.#contentElement)
} catch (err) {
console.warn(`[Block] Plugin destroy failed for ${this.#id} (${this.#type}):`, err)
}
}
}
/**
* Focus the first editable or focusable element within the block.
*/
focus() {
if (this.#contentElement.contentEditable === 'true' || this.#contentElement.tabIndex >= 0) {
this.#contentElement.focus()
return
}
const editable = this.#contentElement.querySelector('[contenteditable="true"]')
if (editable instanceof HTMLElement) {
editable.focus()
return
}
const focusable = this.#contentElement.querySelector('input, textarea, [tabindex]')
if (focusable instanceof HTMLElement) {
focusable.focus()
return
}
this.#contentElement.setAttribute('tabindex', '-1')
this.#contentElement.focus()
}
/**
* Clean up plugin resources. Does NOT remove the element from DOM —
* BlockAnimator handles animated removal separately.
*/
destroy() {
this.disposePlugin()
}
}