-
-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathToolbar.vue
More file actions
453 lines (418 loc) · 10 KB
/
Copy pathToolbar.vue
File metadata and controls
453 lines (418 loc) · 10 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<template>
<k-toolbar
v-if="isOpen || !inline"
ref="toolbar"
:buttons="buttons"
:data-inline="inline"
:theme="theme"
:style="positions"
class="k-writer-toolbar"
/>
</template>
<script>
/**
* Toolbar for `k-writer`
* @displayName WriterToolbar
* @unstable
*/
export default {
props: {
/**
* ProseMirror editor instance
*/
editor: {
required: true,
type: Object
},
/**
* Whether the toolbar is displayed inline or as
* a floating toolbar near the selection
*/
inline: {
default: true,
type: Boolean
},
/**
* Which marks to show in the toolbar
*/
marks: {
default: () => [
"bold",
"italic",
"underline",
"strike",
"code",
"|",
"link",
"email",
"|",
"clear"
],
type: [Array, Boolean]
},
/**
* Which nodes to show in the toolbar
*/
nodes: {
default: true,
type: [Array, Boolean]
}
},
emits: ["command"],
data() {
return {
isOpen: false,
position: { x: 0, y: 0 }
};
},
computed: {
/**
* Currently active dropdown entry, if any
* @returns {Object|undefined}
*/
activeDropdownEntry() {
return Object.values(this.dropdownEntries).findLast(this.isNodeActive);
},
/**
* Button objects for k-toolbar
* @returns {Array}
*/
buttons() {
const buttons = [];
// button for nodes dropdown
if (this.hasDropdownEntries) {
buttons.push(this.dropdownInlineButton);
}
// divider between dropdown and inline buttons
if (this.hasDropdownEntries && this.hasInlineEntries) {
buttons.push("|");
}
// inline buttons (all marks and inline nodes)
for (const [type, entry] of Object.entries(this.inlineEntries)) {
buttons.push(this.inlineButton(entry, type));
}
return buttons;
},
/**
* All dropdown buttons
* @returns {Array}
*/
dropdown() {
const buttons = [];
const entries = Object.entries(this.dropdownEntries);
let index = 0;
for (const [type, entry] of entries) {
// add dropdown button for each entry
buttons.push(this.dropdownButton(entry, type));
// add separator between dropdown entries
// unless it's the last entry
if (entry.separator === true && index !== entries.length - 1) {
buttons.push("-");
}
index++;
}
return buttons;
},
/**
* Dropdown inline button for the toolbar
* @returns {Object}
*/
dropdownInlineButton() {
return {
current: Boolean(this.activeDropdownEntry),
dropdown: this.dropdown,
icon: this.activeDropdownEntry?.icon ?? "title"
};
},
/**
* All dropdown entries that are available and requested
* based on the `nodes` prop
* @returns {Object}
*/
dropdownEntries() {
if (this.nodes === false) {
return {};
}
// get all non-inline nodes
const available = this.nodesForBlock;
// remove the paragraph when certain nodes are requested to be loaded
if (this.editor.nodes.doc.content !== "block+" && available.paragraph) {
delete available.paragraph;
}
if (this.nodes === true) {
return available;
}
// get requested nodes from available entries
// if they are available
return Object.fromEntries(
this.nodes
.filter((node) => available[node])
.map((node) => [node, available[node]])
);
},
/**
* Whether there are any block buttons to show in the toolbar dropdown
* @returns {Boolean}
*/
hasDropdownEntries() {
return this.$helper.object.length(this.dropdownEntries) > 1;
},
/**
* Whether there are any inline buttons to show in the toolbar
* @returns {Boolean}
*/
hasInlineEntries() {
return this.$helper.object.length(this.inlineEntries) > 0;
},
/**
* All inline entries that are available and requested
* as based on the `marks` and `nodes` props
* @returns {Object}
*/
inlineEntries() {
let entries = {};
// inline nodes
if (this.nodes === true) {
// add all inline nodes
entries = this.nodesForInline;
} else if (this.nodes !== false) {
// add requested inline nodes
for (const node of this.nodes) {
if (this.nodesForInline[node]) {
entries[node] = this.nodesForInline[node];
}
}
}
// add divider between inline nodes and marks
if (this.$helper.object.length(entries) > 0) {
entries["divider-inline-nodes"] = "|";
}
// marks
const marks = this.editor.buttons("mark");
if (this.marks === true) {
// add all marks to existing entries
return { ...entries, ...marks };
}
if (this.marks !== false) {
// add only requested marks to existing entries
for (const [index, mark] of this.marks.entries()) {
if (mark === "|") {
entries["divider" + index] = "|";
} else if (marks[mark]) {
entries[mark] = marks[mark];
}
}
}
return entries;
},
/**
* All block nodes
* @returns {Object}
*/
nodesForBlock() {
return this.$helper.object.filter(
this.editor.buttons("node"),
(button) => button.inline !== true
);
},
/**
* All inline nodes
* @returns {Object}
*/
nodesForInline() {
return this.$helper.object.filter(
this.editor.buttons("node"),
(button) => button.inline === true
);
},
/**
* @returns {Object|null}
*/
positions() {
// only set position when toolbar is inline,
// otherwise the top value is overwriting the top offset
// for the sticky non-inline toolbar
if (this.inline === false) {
return null;
}
return {
top: this.position.y + "px",
left: this.position.x + "px"
};
},
/**
* @returns {String}
*/
theme() {
return this.inline ? "dark" : "light";
}
},
methods: {
/**
* Closes the inline toolbar
* @public
* @param {FocusEvent} event
*/
close(event) {
if (!event || this.$el.contains(event.relatedTarget) === false) {
this.isOpen = false;
}
},
command(command, ...args) {
this.$emit("command", command, ...args);
},
/**
* Creates a dropdown button object
* @param {Object} entry
* @param {String} type
* @returns {Object}
*/
dropdownButton(entry, type) {
return {
current: this.activeDropdownEntry?.id === entry.id,
disabled:
this.activeDropdownEntry?.when?.includes(entry.name) === false,
icon: entry.icon,
label: entry.label,
click: () => this.command(entry.command ?? type)
};
},
/**
* Creates an inline button object
* @param {Object} entry
* @param {String} type
* @returns {Object}
*/
inlineButton(entry, type) {
if (entry === "|") {
return "|";
}
return {
current: this.isMarkActive({ ...entry, name: type }),
icon: entry.icon,
label: entry.label,
click: (e) => this.command(entry.command ?? type, e)
};
},
/**
* Checks if the given mark is active
* @param {Object} mark
* @returns {Boolean}
*/
isMarkActive(mark) {
return this.editor.activeMarks.includes(mark.name);
},
/**
* Checks if the given node is active
* @param {Object} node
* @returns {Boolean}
*/
isNodeActive(node) {
if (this.editor.activeNodes.includes(node.name) === false) {
return false;
}
// We might have multiple node buttons for the same node
// (e.g. headings). To know which one is active, we need
// to compare the active attributes with the
// attributes of the node button
if (node.attrs) {
const activeAttrs = Object.values(this.editor.activeNodeAttrs);
const activeNode = activeAttrs.find(
(attrs) => JSON.stringify(attrs) === JSON.stringify(node.attrs)
);
if (activeNode === undefined) {
return false;
}
}
return true;
},
/**
* Opens the toolbar
* @public
*/
open() {
if (this.buttons.length === 0) {
return;
}
this.isOpen = true;
if (this.inline) {
this.$nextTick(this.setPosition);
}
},
/**
* Calculates the position of the inline toolbar
* based on the current selection in the editor
*/
setPosition() {
// Get sizes for the toolbar itself but also the editor box
const toolbar = this.$el.getBoundingClientRect();
const editor = this.editor.element.getBoundingClientRect();
// Create pseudo rectangle for the selection
const { from, to } = this.editor.selection;
const start = this.editor.view.coordsAtPos(from);
const end = this.editor.view.coordsAtPos(to, true);
const selection = new DOMRect(
start.left,
start.top,
end.right - start.left,
end.bottom - start.top
);
// Calculate the position of the toolbar: centered above the selection
let x = selection.x - editor.x + selection.width / 2 - toolbar.width / 2;
let y = selection.y - editor.y - toolbar.height - 5;
// Contain in editor (if possible)
if (toolbar.width < editor.width) {
if (x < 0) {
x = 0;
} else if (x + toolbar.width > editor.width) {
x = editor.width - toolbar.width;
}
} else {
// Contain in viewport
const menu = document
.querySelector(".k-panel-menu")
?.getBoundingClientRect();
const left = editor.x + x;
const right = left + toolbar.width;
const safeSpaceLeft = menu?.width + 20;
const safeSpaceRight = 20;
if (left < safeSpaceLeft) {
x += safeSpaceLeft - left;
} else if (right > window.innerWidth - safeSpaceRight) {
x -= right - (window.innerWidth - safeSpaceRight);
}
}
this.position = { x, y };
}
}
};
</script>
<style>
.k-writer-input:has(
.k-toolbar:not([data-inline="true"], [data-disabled="true"])
) {
grid-template-areas: "topbar" "content";
grid-template-rows: var(--toolbar-size) 1fr;
gap: 0;
}
.k-writer-toolbar:not(:has(~ :focus-within)) {
--toolbar-current: currentColor;
}
.k-writer-toolbar[data-inline="true"] {
position: absolute;
z-index: calc(var(--z-dropdown) + 1);
max-width: none;
box-shadow: var(--shadow-toolbar);
}
.k-writer-toolbar:not([data-inline="true"]) {
border-end-start-radius: 0;
border-end-end-radius: 0;
border-bottom: 1px solid var(--toolbar-border);
}
.k-writer-toolbar:not([data-inline="true"]) > .k-button:first-child {
border-end-start-radius: 0;
}
.k-writer-toolbar:not([data-inline="true"]) > .k-button:last-child {
border-end-end-radius: 0;
}
</style>