-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreservedBlockPlugin.js
More file actions
42 lines (40 loc) · 1.26 KB
/
Copy pathPreservedBlockPlugin.js
File metadata and controls
42 lines (40 loc) · 1.26 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
import { cloneEditorData } from '../shared/cloneEditorData.js'
/**
* Create an inert plugin for a document block whose implementation is not
* registered in this editor instance. The block remains removable and
* reorderable, but its plugin-owned payload is preserved byte-for-byte at the
* JSON value level instead of being handed to an unrelated default plugin.
*
* @param {string} type
* @param {(type: string) => string} label
* @returns {import('./types').BlockPlugin}
*/
export function createPreservedBlockPlugin(type, label) {
/** @type {WeakMap<HTMLElement, Record<string, unknown>>} */
const dataByElement = new WeakMap()
return {
type,
title: type,
icon: '',
inlineTools: false,
render(data) {
const element = document.createElement('div')
element.className = 'oe-unsupported-block'
element.setAttribute('role', 'note')
const message = label(type)
element.setAttribute('aria-label', message)
element.textContent = message
dataByElement.set(element, cloneEditorData(data))
return element
},
save(element) {
return cloneEditorData(dataByElement.get(element) ?? {})
},
validate() {
return true
},
destroy(element) {
dataByElement.delete(element)
},
}
}