-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlocal-testing.html
More file actions
187 lines (167 loc) · 8.2 KB
/
Copy pathlocal-testing.html
File metadata and controls
187 lines (167 loc) · 8.2 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Test indent</title>
<script>
if (!globalThis.Cypress) {
window.editorVersion = '2.30.8'
document.writeln(
`<script onload="window.dispatchEvent(new CustomEvent('loadded-editorjs-script'));" src="https://cdn.jsdelivr.net/npm/@editorjs/editorjs@${window.editorVersion}"><\/script>`,
)
} else {
}
</script>
<script src="
https://cdn.jsdelivr.net/npm/@editorjs/header@2.8.7/dist/header.umd.min.js
"></script>
<script src="
https://cdn.jsdelivr.net/npm/@editorjs/list@1.10.0/dist/list.umd.min.js
"></script>
<script src="https://cdn.jsdelivr.net/npm/@editorjs/underline@1.1.0/dist/bundle.min.js"></script>
<script src="./dist/dev-build.js"></script>
<style>
.ce-block__content::before {
content: '';
width: 20px;
display: block;
height: 1.6em;
right: 100%;
top: 50%;
position: absolute;
transform: translate(0px, -50%);
}
.toolbarIsOpen [data-item-name='convert-to'] {
display: none;
}
</style>
</head>
<body>
<div id="editor" style="background-color: antiquewhite" spellcheck="false"></div>
<script>
if (!globalThis.Cypress) {
queueMicrotask(() => {
window.dispatchEvent(new CustomEvent('loadded-editorjs-script'))
})
}
window.addEventListener('loadded-editorjs-script', () => {
const version = window.editorVersion
const MultiBlockSelectionPlugin = self.MultiBlockSelectionPlugin
let selectedBlocks = []
window.addEventListener(MultiBlockSelectionPlugin.SELECTION_CHANGE_EVENT, (ev) => {
selectedBlocks = ev.detail.selectedBlocks.slice()
})
class ExtendedUnderline extends Underline {
elementTagName = 'u'
surround(range) {
if (!selectedBlocks.length) {
super.surround(range)
return
}
let isAppliedOnAllSelectedBlocks = true
// verify if it is applied to all selected blocks
selectedBlocks.forEach(({ blockId, index }) => {
const el = document.querySelector(`.codex-editor__redactor .ce-block:nth-child(${index + 1})`)
if (!(el instanceof HTMLElement)) return
const textEl = el.querySelector('[contenteditable=true]')
if (!(textEl instanceof HTMLElement)) return
const allText = el.textContent
const firstBoldEl = textEl.querySelector(this.elementTagName)
const isAppliedOnCurrentBlock = firstBoldEl && firstBoldEl.textContent == allText
if (isAppliedOnCurrentBlock) return
isAppliedOnAllSelectedBlocks = false
})
selectedBlocks.forEach(({ blockId, index }) => {
// depending on your editor version you might have to use getByIndex();
const block = this.api.blocks.getById(blockId)
if (!block) return
const el = block.holder
if (!(el instanceof HTMLElement)) return
const textEl = el.querySelector('[contenteditable=true]')
if (!(textEl instanceof HTMLElement)) return
const allText = el.textContent
const firstUnderlineEl = textEl.querySelector(this.elementTagName)
const isAppliedOnCurrentBlock = firstUnderlineEl && firstUnderlineEl.textContent == allText
const shouldRemove = isAppliedOnAllSelectedBlocks
const shouldAdd = !isAppliedOnAllSelectedBlocks && !isAppliedOnCurrentBlock
if (shouldRemove) {
textEl.querySelectorAll(this.elementTagName).forEach((b) => b.replaceWith(...b.childNodes))
block.dispatchChange()
return
}
if (!shouldAdd) return
textEl.querySelectorAll(this.elementTagName).forEach((b) => b.replaceWith(...b.childNodes))
const newUnderline = document.createElement(this.elementTagName)
newUnderline.append(...textEl.childNodes)
textEl.replaceChildren(newUnderline)
block.dispatchChange()
})
}
}
const dataLocation = 'editorjs-data-testing'
let data = {}
try {
data = JSON.parse(localStorage.getItem(dataLocation))
// console.log('🚀 ~ data:', data)
} catch {}
var editor = new EditorJS({
holder: 'editor',
tools: {
underline: {
class: ExtendedUnderline,
},
list: {
class: self.List,
},
header: {
class: self.Header,
},
},
// readOnly: true,
// onChange(api) {
// api.saver.save().then((data) => {
// // localStorage.setItem(dataLocation, JSON.stringify(data))
// })
// },
data,
})
// console.log('🚀 ~ editor:', editor.configuration?.tools)
const blockSelection = new MultiBlockSelectionPlugin({
editor,
version: EditorJS.version,
onBeforeToolbarOpen(toolbar) {
if (!(toolbar instanceof HTMLElement)) return
const dropdown = toolbar.querySelector('.ce-inline-toolbar__dropdown')
if (dropdown instanceof HTMLElement) {
dropdown.style.display = 'none'
}
toolbar.querySelectorAll('[data-tool]:not([data-tool=underline])').forEach((el) => {
if (!(el instanceof HTMLElement)) return
el.style.display = 'none'
})
toolbar.classList.add('toolbarIsOpen')
},
onAfterToolbarClose(toolbar) {
if (!(toolbar instanceof HTMLElement)) return
const dropdown = toolbar.querySelector('.ce-inline-toolbar__dropdown')
if (dropdown instanceof HTMLElement) {
dropdown.style.display = ''
}
toolbar.querySelectorAll('[data-tool]:not([data-tool=underline])').forEach((el) => {
if (!(el instanceof HTMLElement)) return
el.style.display = ''
})
setTimeout(() => {
toolbar.classList.remove('toolbarIsOpen')
}, 250)
},
})
window.isEditorReady = editor.isReady.then(() => {
// console.log('🚀 ~ editor.isReady.then ~ editor:', editor)
blockSelection.listen()
})
})
</script>
</body>
</html>