@astl/editor 0.0.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +107 -0
- package/LICENSE +202 -0
- package/README.md +43 -0
- package/dist/authoring.d.ts +284 -0
- package/dist/authoring.d.ts.map +1 -0
- package/dist/authoring.js +566 -0
- package/dist/authoring.js.map +1 -0
- package/dist/define.d.ts +8 -0
- package/dist/define.d.ts.map +1 -0
- package/dist/define.js +35 -0
- package/dist/define.js.map +1 -0
- package/dist/edit.d.ts +45 -0
- package/dist/edit.d.ts.map +1 -0
- package/dist/edit.js +150 -0
- package/dist/edit.js.map +1 -0
- package/dist/elements/base.d.ts +31 -0
- package/dist/elements/base.d.ts.map +1 -0
- package/dist/elements/base.js +75 -0
- package/dist/elements/base.js.map +1 -0
- package/dist/elements/color-control.d.ts +25 -0
- package/dist/elements/color-control.d.ts.map +1 -0
- package/dist/elements/color-control.js +139 -0
- package/dist/elements/color-control.js.map +1 -0
- package/dist/elements/help-form.d.ts +31 -0
- package/dist/elements/help-form.d.ts.map +1 -0
- package/dist/elements/help-form.js +371 -0
- package/dist/elements/help-form.js.map +1 -0
- package/dist/elements/insert-menu.d.ts +79 -0
- package/dist/elements/insert-menu.d.ts.map +1 -0
- package/dist/elements/insert-menu.js +362 -0
- package/dist/elements/insert-menu.js.map +1 -0
- package/dist/elements/inspector.d.ts +24 -0
- package/dist/elements/inspector.d.ts.map +1 -0
- package/dist/elements/inspector.js +846 -0
- package/dist/elements/inspector.js.map +1 -0
- package/dist/elements/issue-list.d.ts +18 -0
- package/dist/elements/issue-list.d.ts.map +1 -0
- package/dist/elements/issue-list.js +85 -0
- package/dist/elements/issue-list.js.map +1 -0
- package/dist/elements/issues.d.ts +24 -0
- package/dist/elements/issues.d.ts.map +1 -0
- package/dist/elements/issues.js +81 -0
- package/dist/elements/issues.js.map +1 -0
- package/dist/elements/picture-dialog.d.ts +40 -0
- package/dist/elements/picture-dialog.d.ts.map +1 -0
- package/dist/elements/picture-dialog.js +433 -0
- package/dist/elements/picture-dialog.js.map +1 -0
- package/dist/elements/picture-field.d.ts +33 -0
- package/dist/elements/picture-field.d.ts.map +1 -0
- package/dist/elements/picture-field.js +692 -0
- package/dist/elements/picture-field.js.map +1 -0
- package/dist/elements/preview.d.ts +34 -0
- package/dist/elements/preview.d.ts.map +1 -0
- package/dist/elements/preview.js +165 -0
- package/dist/elements/preview.js.map +1 -0
- package/dist/elements/style-form.d.ts +30 -0
- package/dist/elements/style-form.d.ts.map +1 -0
- package/dist/elements/style-form.js +640 -0
- package/dist/elements/style-form.js.map +1 -0
- package/dist/elements/style-load.d.ts +26 -0
- package/dist/elements/style-load.d.ts.map +1 -0
- package/dist/elements/style-load.js +50 -0
- package/dist/elements/style-load.js.map +1 -0
- package/dist/elements/style-popups.d.ts +39 -0
- package/dist/elements/style-popups.d.ts.map +1 -0
- package/dist/elements/style-popups.js +173 -0
- package/dist/elements/style-popups.js.map +1 -0
- package/dist/elements/style-rules.d.ts +21 -0
- package/dist/elements/style-rules.d.ts.map +1 -0
- package/dist/elements/style-rules.js +274 -0
- package/dist/elements/style-rules.js.map +1 -0
- package/dist/elements/styles.d.ts +9 -0
- package/dist/elements/styles.d.ts.map +1 -0
- package/dist/elements/styles.js +161 -0
- package/dist/elements/styles.js.map +1 -0
- package/dist/elements/toolbar.d.ts +65 -0
- package/dist/elements/toolbar.d.ts.map +1 -0
- package/dist/elements/toolbar.js +1270 -0
- package/dist/elements/toolbar.js.map +1 -0
- package/dist/help.d.ts +33 -0
- package/dist/help.d.ts.map +1 -0
- package/dist/help.js +111 -0
- package/dist/help.js.map +1 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/inspect.d.ts +251 -0
- package/dist/inspect.d.ts.map +1 -0
- package/dist/inspect.js +428 -0
- package/dist/inspect.js.map +1 -0
- package/dist/messages/en.json +252 -0
- package/dist/messages/help/en.json +18 -0
- package/dist/messages/help/pl.json +18 -0
- package/dist/messages/pl.json +252 -0
- package/dist/messages/style/en.json +67 -0
- package/dist/messages/style/pl.json +67 -0
- package/dist/messages.d.ts +5 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +9 -0
- package/dist/messages.js.map +1 -0
- package/dist/pages.d.ts +28 -0
- package/dist/pages.d.ts.map +1 -0
- package/dist/pages.js +147 -0
- package/dist/pages.js.map +1 -0
- package/dist/scan.d.ts +82 -0
- package/dist/scan.d.ts.map +1 -0
- package/dist/scan.js +329 -0
- package/dist/scan.js.map +1 -0
- package/dist/style-messages.d.ts +7 -0
- package/dist/style-messages.d.ts.map +1 -0
- package/dist/style-messages.js +12 -0
- package/dist/style-messages.js.map +1 -0
- package/dist/style-model.d.ts +50 -0
- package/dist/style-model.d.ts.map +1 -0
- package/dist/style-model.js +76 -0
- package/dist/style-model.js.map +1 -0
- package/dist/style-placing.d.ts +38 -0
- package/dist/style-placing.d.ts.map +1 -0
- package/dist/style-placing.js +98 -0
- package/dist/style-placing.js.map +1 -0
- package/dist/textarea.d.ts +17 -0
- package/dist/textarea.d.ts.map +1 -0
- package/dist/textarea.js +175 -0
- package/dist/textarea.js.map +1 -0
- package/package.json +54 -4
- package/src/authoring.ts +896 -0
- package/src/define.ts +34 -0
- package/src/edit.ts +195 -0
- package/src/elements/base.ts +93 -0
- package/src/elements/color-control.ts +159 -0
- package/src/elements/help-form.ts +404 -0
- package/src/elements/insert-menu.ts +481 -0
- package/src/elements/inspector.ts +941 -0
- package/src/elements/issue-list.ts +103 -0
- package/src/elements/issues.ts +89 -0
- package/src/elements/picture-dialog.ts +461 -0
- package/src/elements/picture-field.ts +770 -0
- package/src/elements/preview.ts +186 -0
- package/src/elements/style-form.ts +751 -0
- package/src/elements/style-load.ts +64 -0
- package/src/elements/style-popups.ts +257 -0
- package/src/elements/style-rules.ts +294 -0
- package/src/elements/styles.ts +162 -0
- package/src/elements/toolbar.ts +1386 -0
- package/src/help.ts +156 -0
- package/src/index.ts +81 -0
- package/src/inspect.ts +774 -0
- package/src/messages/en.json +252 -0
- package/src/messages/help/en.json +18 -0
- package/src/messages/help/pl.json +18 -0
- package/src/messages/pl.json +252 -0
- package/src/messages/style/en.json +67 -0
- package/src/messages/style/pl.json +67 -0
- package/src/messages.ts +11 -0
- package/src/pages.ts +158 -0
- package/src/scan.ts +382 -0
- package/src/style-messages.ts +13 -0
- package/src/style-model.ts +125 -0
- package/src/style-placing.ts +117 -0
- package/src/textarea.ts +182 -0
|
@@ -0,0 +1,1270 @@
|
|
|
1
|
+
import { RovingFocus } from '@astl/player/interaction';
|
|
2
|
+
import { css, html, nothing, svg, } from 'lit';
|
|
3
|
+
import { styleMapOf } from '../style-model.js';
|
|
4
|
+
import { AuthoringElement } from './base.js';
|
|
5
|
+
import { GLYPHS, TYPE_GLYPHS } from './insert-menu.js';
|
|
6
|
+
import { loadStyleElements, STYLE_OPEN } from './style-load.js';
|
|
7
|
+
import { inheritTokens, kitStyles } from './styles.js';
|
|
8
|
+
/** Kit items a host can feature besides component tags (`tag`) and starters (`tag:template`). */
|
|
9
|
+
export const FEATURED_ITEMS = ['text', 'picture', 'page-break'];
|
|
10
|
+
const ICONS = {
|
|
11
|
+
bold: svg `<path d="M6 4h5a3 3 0 0 1 0 6H6zm0 6h6a3 3 0 0 1 0 6H6z" stroke-width="2"/>`,
|
|
12
|
+
italic: svg `<path d="M9 4h7M4 16h7M12.5 4l-5 12"/>`,
|
|
13
|
+
underline: svg `<path d="M6 3v6a4 4 0 0 0 8 0V3M5 17h10"/>`,
|
|
14
|
+
link: svg `<path d="M8.5 11.5l3-3M7.5 9.5l-2 2a2.8 2.8 0 0 0 4 4l2-2M12.5 10.5l2-2a2.8 2.8 0 0 0-4-4l-2 2"/>`,
|
|
15
|
+
bulleted: svg `<path d="M8 5h9M8 10h9M8 15h9"/><circle cx="4" cy="5" r="1.3"/><circle cx="4" cy="10" r="1.3"/><circle cx="4" cy="15" r="1.3"/>`,
|
|
16
|
+
numbered: svg `<path d="M9 5h8M9 10h8M9 15h8M3 3.5h1.5V8M2.5 12.2c.4-.8 2.5-.9 2.5.4 0 1-2.5 2-2.5 3.4H5" stroke-width="1.5"/>`,
|
|
17
|
+
undo: svg `<path d="M7.5 5L4 8.5 7.5 12M4 8.5h8a4 4 0 0 1 0 8H9"/>`,
|
|
18
|
+
redo: svg `<path d="M12.5 5L16 8.5 12.5 12M16 8.5H8a4 4 0 0 0 0 8h3"/>`,
|
|
19
|
+
text: svg `<path d="M4 5V4h12v1M10 4v12M7.5 16h5"/>`,
|
|
20
|
+
add: svg `<path d="M10 4v12M4 10h12"/>`,
|
|
21
|
+
layouts: GLYPHS.columns,
|
|
22
|
+
templates: svg `<rect x="3" y="3" width="14" height="14" rx="1.5"/><path d="M3 8h14M8 8v9"/>`,
|
|
23
|
+
wrap: svg `<path d="M3 6h14M3 10h6M11 10h6M3 14h14" stroke-width="1.6"/><rect x="8" y="8" width="4" height="4" rx="1" stroke-width="1.4"/>`,
|
|
24
|
+
chevron: svg `<path d="M6 8l4 4 4-4"/>`,
|
|
25
|
+
color: svg `<path d="M5.5 13L10 3l4.5 10M7 10h6" stroke-width="1.8"/>`,
|
|
26
|
+
highlight: svg `<path d="M12.5 3.5l4 4-7.5 7.5H5v-4zM3.5 16.5h6" stroke-width="1.6"/>`,
|
|
27
|
+
'align-start': svg `<path d="M3.5 5h13M3.5 9h8M3.5 13h13M3.5 17h8"/>`,
|
|
28
|
+
'align-center': svg `<path d="M3.5 5h13M6 9h8M3.5 13h13M6 17h8"/>`,
|
|
29
|
+
'align-end': svg `<path d="M3.5 5h13M8.5 9h8M3.5 13h13M8.5 17h8"/>`,
|
|
30
|
+
'align-justify': svg `<path d="M3.5 5h13M3.5 9h13M3.5 13h13M3.5 17h13"/>`,
|
|
31
|
+
spacing: svg `<path d="M9 5h8M9 10h8M9 15h8M4.5 4v12M2.5 6l2-2 2 2M2.5 14l2 2 2-2"/>`,
|
|
32
|
+
clear: svg `<path d="M5 5V4h10v1M10 4l-1.5 7M4 16h7M13 11.5l4 4M17 11.5l-4 4"/>`,
|
|
33
|
+
};
|
|
34
|
+
const STYLES = ['paragraph', 'heading', 'subheading'];
|
|
35
|
+
const ELEMENT_GROUPS = ['components', 'content'];
|
|
36
|
+
const TEMPLATE_GROUPS = ['exercises'];
|
|
37
|
+
const LAYOUT_GROUPS = ['layouts'];
|
|
38
|
+
const MENU_GROUPS = {
|
|
39
|
+
elements: ELEMENT_GROUPS,
|
|
40
|
+
templates: TEMPLATE_GROUPS,
|
|
41
|
+
layouts: LAYOUT_GROUPS,
|
|
42
|
+
};
|
|
43
|
+
const INSERT_MENUS = new Set(Object.keys(MENU_GROUPS));
|
|
44
|
+
const MARKS = ['bold', 'italic', 'underline'];
|
|
45
|
+
const LISTS = ['bulleted', 'numbered'];
|
|
46
|
+
const STYLE_POPUPS = new Set(['font', 'size', 'color', 'highlight', 'align', 'spacing']);
|
|
47
|
+
const isMenu = (popup) => INSERT_MENUS.has(popup);
|
|
48
|
+
/** The popover a popup opens: the insert menus share one. */
|
|
49
|
+
const popoverOf = (popup) => (isMenu(popup) ? 'insert' : popup);
|
|
50
|
+
/** The button a popup hangs from: the panel, reached from the style menu, hangs from that. */
|
|
51
|
+
const openerOf = (popup) => (popup === 'panel' ? 'style' : popup);
|
|
52
|
+
/** The event the view switch fires when an author picks a view; `detail.view` is its name. */
|
|
53
|
+
export const VIEW_PICKED = 'astl-toolbar-view';
|
|
54
|
+
const NO_STATE = { active: [], enabled: [], href: null, undo: false, redo: false };
|
|
55
|
+
/**
|
|
56
|
+
* The bar: one row of groups with a thin rule between them (each group's `separator`, a part a host can redraw), undo
|
|
57
|
+
* and redo at its end. A group that wraps to the start of a row has its rule on the bar's own border, where it does not
|
|
58
|
+
* show. Wider than a phone, the groups wrap into rows
|
|
59
|
+
* when they do not fit, so every button is on show; on a phone the bar is one row that scrolls sideways, as a mobile
|
|
60
|
+
* editor's:
|
|
61
|
+
* wrapped, it would push the text below the fold. A shade at an edge says more buttons lie that way (the covers scroll
|
|
62
|
+
* with the buttons and hide it at either end), and the arrow keys scroll the focused button into view.
|
|
63
|
+
*/
|
|
64
|
+
const barStyles = css `
|
|
65
|
+
:host {
|
|
66
|
+
display: block;
|
|
67
|
+
container-type: inline-size;
|
|
68
|
+
}
|
|
69
|
+
.bar {
|
|
70
|
+
box-sizing: border-box;
|
|
71
|
+
display: flex;
|
|
72
|
+
flex-wrap: wrap;
|
|
73
|
+
align-items: center;
|
|
74
|
+
gap: var(--astl-space-1) calc(2 * var(--astl-space-2) + var(--astl-outline-width));
|
|
75
|
+
padding: var(--astl-space-2);
|
|
76
|
+
border: var(--astl-outline-width) solid var(--astl-line);
|
|
77
|
+
border-radius: var(--astl-radius-l);
|
|
78
|
+
background: var(--astl-surface);
|
|
79
|
+
}
|
|
80
|
+
.group {
|
|
81
|
+
display: flex;
|
|
82
|
+
flex-wrap: wrap;
|
|
83
|
+
align-items: center;
|
|
84
|
+
gap: var(--astl-space-1);
|
|
85
|
+
}
|
|
86
|
+
.group + .group {
|
|
87
|
+
position: relative;
|
|
88
|
+
margin-inline-start: calc(-1 * var(--astl-space-2) - var(--astl-outline-width));
|
|
89
|
+
padding-inline-start: calc(var(--astl-space-2) + var(--astl-outline-width));
|
|
90
|
+
}
|
|
91
|
+
.separator {
|
|
92
|
+
position: absolute;
|
|
93
|
+
inset-block: 0;
|
|
94
|
+
inset-inline-start: 0;
|
|
95
|
+
border-inline-start: var(--astl-outline-width) solid var(--astl-line);
|
|
96
|
+
}
|
|
97
|
+
.group.history {
|
|
98
|
+
margin-inline-start: auto;
|
|
99
|
+
}
|
|
100
|
+
@container (inline-size < 30rem) {
|
|
101
|
+
.bar {
|
|
102
|
+
flex-wrap: nowrap;
|
|
103
|
+
overflow-x: auto;
|
|
104
|
+
scrollbar-width: thin;
|
|
105
|
+
background:
|
|
106
|
+
linear-gradient(90deg, var(--astl-surface) 50%, #0000) left / 1.5rem no-repeat local,
|
|
107
|
+
linear-gradient(-90deg, var(--astl-surface) 50%, #0000) right / 1.5rem no-repeat local,
|
|
108
|
+
radial-gradient(farthest-side at 0, var(--astl-outline), #0000) left / 0.5rem no-repeat,
|
|
109
|
+
radial-gradient(farthest-side at 100%, var(--astl-outline), #0000) right / 0.5rem no-repeat,
|
|
110
|
+
var(--astl-surface);
|
|
111
|
+
}
|
|
112
|
+
.group {
|
|
113
|
+
flex: none;
|
|
114
|
+
flex-wrap: nowrap;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
`;
|
|
118
|
+
/** The buttons: hovered, pressed. */
|
|
119
|
+
const buttonStyles = css `
|
|
120
|
+
.bar button {
|
|
121
|
+
justify-content: center;
|
|
122
|
+
padding-inline: var(--astl-space-2);
|
|
123
|
+
border-color: transparent;
|
|
124
|
+
background: transparent;
|
|
125
|
+
white-space: nowrap;
|
|
126
|
+
}
|
|
127
|
+
.bar button.labelled {
|
|
128
|
+
padding-inline: var(--astl-space-2) var(--astl-space-3);
|
|
129
|
+
}
|
|
130
|
+
.bar button:hover {
|
|
131
|
+
border-color: var(--astl-line);
|
|
132
|
+
background: var(--astl-accent-soft);
|
|
133
|
+
}
|
|
134
|
+
.bar button[aria-pressed='true'] {
|
|
135
|
+
border-color: var(--astl-accent);
|
|
136
|
+
background: var(--astl-accent-soft);
|
|
137
|
+
color: var(--astl-ink);
|
|
138
|
+
}
|
|
139
|
+
.group.views {
|
|
140
|
+
gap: 0;
|
|
141
|
+
padding: calc(2 * var(--astl-outline-width));
|
|
142
|
+
border: var(--astl-outline-width) solid var(--astl-outline);
|
|
143
|
+
border-radius: var(--astl-radius-m);
|
|
144
|
+
}
|
|
145
|
+
.bar .views [aria-checked='true'] {
|
|
146
|
+
background: var(--astl-accent);
|
|
147
|
+
color: var(--astl-on-accent);
|
|
148
|
+
font-weight: 600;
|
|
149
|
+
}
|
|
150
|
+
`;
|
|
151
|
+
/**
|
|
152
|
+
* Unavailable here: dimmed and dashed (not by colour alone), still reachable by the arrow keys. Then the icons (shapes
|
|
153
|
+
* stroked here) and the popups: the text style menu, the link and the insert menus. Popups are compact: little
|
|
154
|
+
* padding, items without a frame (the hover's fill shows them; a disabled entry keeps its dashed one), the target kept
|
|
155
|
+
* (`--astl-target-min`). An insert menu has no visible title: the button that opened it names it, and the title is
|
|
156
|
+
* still the section's name.
|
|
157
|
+
*/
|
|
158
|
+
const popupStyles = css `
|
|
159
|
+
.bar button[aria-disabled='true'] {
|
|
160
|
+
border-color: var(--astl-line);
|
|
161
|
+
border-style: dashed;
|
|
162
|
+
background: transparent;
|
|
163
|
+
color: var(--astl-ink-muted);
|
|
164
|
+
cursor: not-allowed;
|
|
165
|
+
}
|
|
166
|
+
.bar button[aria-expanded='true'] {
|
|
167
|
+
border-color: var(--astl-outline);
|
|
168
|
+
}
|
|
169
|
+
.icon {
|
|
170
|
+
fill: none;
|
|
171
|
+
stroke: currentColor;
|
|
172
|
+
stroke-width: 1.8;
|
|
173
|
+
stroke-linecap: round;
|
|
174
|
+
stroke-linejoin: round;
|
|
175
|
+
}
|
|
176
|
+
.icon circle {
|
|
177
|
+
fill: currentColor;
|
|
178
|
+
stroke: none;
|
|
179
|
+
}
|
|
180
|
+
.chevron {
|
|
181
|
+
inline-size: 1rem;
|
|
182
|
+
block-size: 1rem;
|
|
183
|
+
}
|
|
184
|
+
[popover] {
|
|
185
|
+
box-sizing: border-box;
|
|
186
|
+
position: fixed;
|
|
187
|
+
inset: auto;
|
|
188
|
+
margin: 0;
|
|
189
|
+
padding: var(--astl-space-2);
|
|
190
|
+
border: var(--astl-outline-width) solid var(--astl-line);
|
|
191
|
+
border-radius: var(--astl-radius-m);
|
|
192
|
+
background: var(--astl-surface);
|
|
193
|
+
color: var(--astl-ink);
|
|
194
|
+
box-shadow: 0 0.5rem 1.5rem rgb(0 0 0 / 0.2);
|
|
195
|
+
overflow: auto;
|
|
196
|
+
}
|
|
197
|
+
[popover]:not(:popover-open) {
|
|
198
|
+
display: none;
|
|
199
|
+
}
|
|
200
|
+
.menu {
|
|
201
|
+
display: flex;
|
|
202
|
+
flex-direction: column;
|
|
203
|
+
gap: 2px;
|
|
204
|
+
min-inline-size: 12rem;
|
|
205
|
+
max-block-size: calc(100dvh - 2 * var(--astl-space-4));
|
|
206
|
+
}
|
|
207
|
+
.menu button {
|
|
208
|
+
justify-content: flex-start;
|
|
209
|
+
padding: var(--astl-space-1) var(--astl-space-2);
|
|
210
|
+
border-color: transparent;
|
|
211
|
+
}
|
|
212
|
+
.menu button[aria-checked='true'] {
|
|
213
|
+
border-color: var(--astl-accent);
|
|
214
|
+
background: var(--astl-accent-soft);
|
|
215
|
+
font-weight: 600;
|
|
216
|
+
}
|
|
217
|
+
.insert {
|
|
218
|
+
inline-size: min(26rem, calc(100vw - 2 * var(--astl-space-4)));
|
|
219
|
+
max-block-size: min(32rem, calc(100vh - 2 * var(--astl-space-4)));
|
|
220
|
+
padding: 0;
|
|
221
|
+
}
|
|
222
|
+
.insert .search {
|
|
223
|
+
position: sticky;
|
|
224
|
+
inset-block-start: 0;
|
|
225
|
+
z-index: 1;
|
|
226
|
+
padding: var(--astl-space-2);
|
|
227
|
+
border-block-end: var(--astl-outline-width) solid var(--astl-line);
|
|
228
|
+
background: var(--astl-surface);
|
|
229
|
+
}
|
|
230
|
+
.insert astl-insert-menu::part(root) {
|
|
231
|
+
padding: var(--astl-space-2);
|
|
232
|
+
border: 0;
|
|
233
|
+
border-radius: 0;
|
|
234
|
+
}
|
|
235
|
+
.insert astl-insert-menu::part(title) {
|
|
236
|
+
display: none;
|
|
237
|
+
}
|
|
238
|
+
.insert astl-insert-menu::part(button) {
|
|
239
|
+
padding-inline: var(--astl-space-2);
|
|
240
|
+
}
|
|
241
|
+
.insert astl-insert-menu::part(button):enabled {
|
|
242
|
+
border-color: transparent;
|
|
243
|
+
}
|
|
244
|
+
.link {
|
|
245
|
+
display: flex;
|
|
246
|
+
flex-direction: column;
|
|
247
|
+
inline-size: min(22rem, calc(100vw - 2 * var(--astl-space-4)));
|
|
248
|
+
}
|
|
249
|
+
.menu .menu-section {
|
|
250
|
+
display: contents;
|
|
251
|
+
}
|
|
252
|
+
.menu .menu-group {
|
|
253
|
+
margin-block: var(--astl-space-2) 0;
|
|
254
|
+
padding-inline: var(--astl-space-2);
|
|
255
|
+
color: var(--astl-ink-muted);
|
|
256
|
+
font-size: var(--astl-size-small);
|
|
257
|
+
font-weight: 600;
|
|
258
|
+
}
|
|
259
|
+
.menu hr {
|
|
260
|
+
inline-size: 100%;
|
|
261
|
+
margin-block: var(--astl-space-1);
|
|
262
|
+
border: 0;
|
|
263
|
+
border-block-start: var(--astl-outline-width) solid var(--astl-line);
|
|
264
|
+
}
|
|
265
|
+
.bar button .value {
|
|
266
|
+
max-inline-size: 8rem;
|
|
267
|
+
overflow: hidden;
|
|
268
|
+
text-overflow: ellipsis;
|
|
269
|
+
}
|
|
270
|
+
.color-mark {
|
|
271
|
+
position: absolute;
|
|
272
|
+
inset-inline: 25%;
|
|
273
|
+
inset-block-end: var(--astl-space-1);
|
|
274
|
+
block-size: 0.2rem;
|
|
275
|
+
border: var(--astl-outline-width) solid var(--astl-outline);
|
|
276
|
+
forced-color-adjust: none;
|
|
277
|
+
}
|
|
278
|
+
.bar button[data-popup='color'],
|
|
279
|
+
.bar button[data-popup='highlight'] {
|
|
280
|
+
position: relative;
|
|
281
|
+
}
|
|
282
|
+
.link > * + * {
|
|
283
|
+
margin-block-start: var(--astl-space-2);
|
|
284
|
+
}
|
|
285
|
+
.link > [role='alert']:empty {
|
|
286
|
+
margin-block-start: 0;
|
|
287
|
+
}
|
|
288
|
+
.link label {
|
|
289
|
+
font-weight: 600;
|
|
290
|
+
}
|
|
291
|
+
.link .buttons {
|
|
292
|
+
display: flex;
|
|
293
|
+
flex-wrap: wrap;
|
|
294
|
+
gap: var(--astl-space-2);
|
|
295
|
+
}
|
|
296
|
+
.link .error {
|
|
297
|
+
margin: 0;
|
|
298
|
+
padding: var(--astl-space-2);
|
|
299
|
+
border-inline-start: var(--astl-space-1) solid var(--astl-danger);
|
|
300
|
+
background: var(--astl-danger-soft);
|
|
301
|
+
}
|
|
302
|
+
@media (forced-colors: active) {
|
|
303
|
+
.bar button:is([aria-pressed='true'], [aria-checked='true']) {
|
|
304
|
+
border-color: Highlight;
|
|
305
|
+
}
|
|
306
|
+
.bar button[aria-disabled='true'] {
|
|
307
|
+
color: GrayText;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
`;
|
|
311
|
+
/**
|
|
312
|
+
* `<astl-toolbar>`: a word processor's toolbar for the editor in `adapter`, one row while it fits. In order: the text
|
|
313
|
+
* style (a menu: paragraph, heading, subheading), bold, italic, underline, link, the two lists; the host's `featured`
|
|
314
|
+
* inserts as buttons, "Elements" (every component and content insert) and "Templates" (the named starters), menus with
|
|
315
|
+
* a search over one `<astl-insert-menu>`; the types' commands on the selection (`EditorDescriptor.wrap`, "Mark
|
|
316
|
+
* word"); the view switch (`views`, a segmented control of radios); undo and redo at the end. Controls the adapter has
|
|
317
|
+
* no capability for are left out (`format`, `undo`, `wrap`). One tab stop: the arrow keys, Home and End move along it.
|
|
318
|
+
* Format buttons show the state at the cursor: pressed, or unavailable (`aria-disabled`, still focusable). A press
|
|
319
|
+
* never moves the editor's cursor: a pointer's leaves the focus in the editor, and a key's gives it back.
|
|
320
|
+
*/
|
|
321
|
+
export class AstlToolbar extends AuthoringElement {
|
|
322
|
+
static properties = {
|
|
323
|
+
...AuthoringElement.properties,
|
|
324
|
+
featured: { attribute: false },
|
|
325
|
+
views: { attribute: false },
|
|
326
|
+
view: {},
|
|
327
|
+
};
|
|
328
|
+
static styles = [
|
|
329
|
+
kitStyles,
|
|
330
|
+
inheritTokens('astl-insert-menu, astl-picture-dialog, astl-style-form, astl-style-rules'),
|
|
331
|
+
barStyles,
|
|
332
|
+
buttonStyles,
|
|
333
|
+
popupStyles,
|
|
334
|
+
];
|
|
335
|
+
#state = NO_STATE;
|
|
336
|
+
#styles = null;
|
|
337
|
+
#popup = null;
|
|
338
|
+
/** The popups of the style controls, once one was asked for. */
|
|
339
|
+
#stylePopupsModule = null;
|
|
340
|
+
/** What the panel shows. */
|
|
341
|
+
#panel = 'block';
|
|
342
|
+
/** A typed colour was refused, in the open colours popup. */
|
|
343
|
+
#colorError = false;
|
|
344
|
+
/** The insert menu shown last: its groups stay while closed. */
|
|
345
|
+
#menu = 'elements';
|
|
346
|
+
#query = '';
|
|
347
|
+
#linkError = false;
|
|
348
|
+
#off = null;
|
|
349
|
+
#items = [];
|
|
350
|
+
#roving = new RovingFocus(this, {
|
|
351
|
+
count: () => this.#items.length,
|
|
352
|
+
item: (index) => this.renderRoot.querySelector(`.bar [data-item="${index}"]`),
|
|
353
|
+
axis: 'inline',
|
|
354
|
+
});
|
|
355
|
+
constructor() {
|
|
356
|
+
super();
|
|
357
|
+
this.featured = [];
|
|
358
|
+
this.views = [];
|
|
359
|
+
this.view = '';
|
|
360
|
+
}
|
|
361
|
+
/** The insert menu of "Elements" and "Templates": insert from it as a host would from `<astl-insert-menu>`. */
|
|
362
|
+
get menu() {
|
|
363
|
+
return this.renderRoot.querySelector('astl-insert-menu');
|
|
364
|
+
}
|
|
365
|
+
/** Opens "Elements", "Templates" or "Layout" with its search, as its button does. */
|
|
366
|
+
async openMenu(menu) {
|
|
367
|
+
await this.#open(menu);
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Asks for the style form of a target at the cursor, or the rules, as the style menu does: fires `astl-style-open`,
|
|
371
|
+
* and unless a host cancels it, shows them in a popup (they load now).
|
|
372
|
+
*/
|
|
373
|
+
async openStyle(target) {
|
|
374
|
+
const detail = { target };
|
|
375
|
+
this.#close(false);
|
|
376
|
+
const event = new CustomEvent(STYLE_OPEN, { detail, bubbles: true, composed: true, cancelable: true });
|
|
377
|
+
if (!this.dispatchEvent(event))
|
|
378
|
+
return;
|
|
379
|
+
await loadStyleElements();
|
|
380
|
+
this.#panel = target;
|
|
381
|
+
await this.#open('panel');
|
|
382
|
+
}
|
|
383
|
+
/** Loads the popups of the style controls and their styles, once; false if that failed (it says so in the console). */
|
|
384
|
+
async #loadStylePopups() {
|
|
385
|
+
if (this.#stylePopupsModule !== null)
|
|
386
|
+
return true;
|
|
387
|
+
try {
|
|
388
|
+
const module = await import('./style-popups.js');
|
|
389
|
+
const root = this.renderRoot;
|
|
390
|
+
const sheet = module.stylePopupStyles.styleSheet;
|
|
391
|
+
if (this.#stylePopupsModule === null && sheet !== undefined)
|
|
392
|
+
root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet];
|
|
393
|
+
this.#stylePopupsModule = module;
|
|
394
|
+
return true;
|
|
395
|
+
}
|
|
396
|
+
catch (error) {
|
|
397
|
+
console.error(error);
|
|
398
|
+
return false;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
connectedCallback() {
|
|
402
|
+
super.connectedCallback();
|
|
403
|
+
this.#follow();
|
|
404
|
+
document.addEventListener('pointerdown', this.#outside, true);
|
|
405
|
+
document.addEventListener('keydown', this.#escape);
|
|
406
|
+
}
|
|
407
|
+
disconnectedCallback() {
|
|
408
|
+
super.disconnectedCallback();
|
|
409
|
+
this.#follow();
|
|
410
|
+
document.removeEventListener('pointerdown', this.#outside, true);
|
|
411
|
+
document.removeEventListener('keydown', this.#escape);
|
|
412
|
+
}
|
|
413
|
+
willUpdate(changed) {
|
|
414
|
+
super.willUpdate(changed);
|
|
415
|
+
if (changed.has('adapter'))
|
|
416
|
+
this.#follow();
|
|
417
|
+
this.#state = this.adapter?.formatState?.() ?? NO_STATE;
|
|
418
|
+
this.#styles = this.adapter?.style?.state() ?? null;
|
|
419
|
+
}
|
|
420
|
+
/** Follows the cursor of the adapter: every move may change the state at it. */
|
|
421
|
+
#follow() {
|
|
422
|
+
const adapter = this.isConnected ? this.adapter : null;
|
|
423
|
+
if ((this.#off?.adapter ?? null) === adapter)
|
|
424
|
+
return;
|
|
425
|
+
this.#off?.off();
|
|
426
|
+
this.#off = null;
|
|
427
|
+
const listen = adapter?.onCursorChange;
|
|
428
|
+
if (adapter === null || listen === undefined)
|
|
429
|
+
return;
|
|
430
|
+
this.#off = { adapter, off: listen.call(adapter, () => this.requestUpdate()) };
|
|
431
|
+
}
|
|
432
|
+
/** A press outside the open popup and its button closes it. */
|
|
433
|
+
#outside = (event) => {
|
|
434
|
+
if (this.#popup === null)
|
|
435
|
+
return;
|
|
436
|
+
const path = event.composedPath();
|
|
437
|
+
const popup = this.renderRoot.querySelector(`#${popoverOf(this.#popup)}`);
|
|
438
|
+
const opener = this.renderRoot.querySelector(`[data-popup="${openerOf(this.#popup)}"]`);
|
|
439
|
+
if ((popup !== null && path.includes(popup)) || (opener !== null && path.includes(opener)))
|
|
440
|
+
return;
|
|
441
|
+
// The picture dialog of an insert menu sits outside the popover in the top layer, above it.
|
|
442
|
+
if (path.some((node) => node instanceof Element && node.localName === 'astl-picture-dialog'))
|
|
443
|
+
return;
|
|
444
|
+
this.#close(false);
|
|
445
|
+
};
|
|
446
|
+
/**
|
|
447
|
+
* The style panel is not modal: the focus can leave it, by Tab or to the surface after an undo, and it stays open
|
|
448
|
+
* over what it covers. Escape closes it from there too; only from inside does the focus go back to its button.
|
|
449
|
+
*/
|
|
450
|
+
#escape = (event) => {
|
|
451
|
+
if (this.#popup !== 'panel' || event.key !== 'Escape')
|
|
452
|
+
return;
|
|
453
|
+
const panel = this.renderRoot.querySelector('#panel');
|
|
454
|
+
const inside = panel !== null && event.composedPath().includes(panel);
|
|
455
|
+
// Inside, a field may have its own use for the key (a rename). ProseMirror claims every Escape, with no use for it.
|
|
456
|
+
if (inside && event.defaultPrevented)
|
|
457
|
+
return;
|
|
458
|
+
event.preventDefault();
|
|
459
|
+
this.#close(inside);
|
|
460
|
+
};
|
|
461
|
+
async #open(popup) {
|
|
462
|
+
if ((popup === 'panel' || STYLE_POPUPS.has(popup)) && !(await this.#loadStylePopups()))
|
|
463
|
+
return;
|
|
464
|
+
if (this.#popup !== null && this.#popup !== popup)
|
|
465
|
+
this.#close(false);
|
|
466
|
+
this.#popup = popup;
|
|
467
|
+
this.#linkError = false;
|
|
468
|
+
this.#colorError = false;
|
|
469
|
+
if (isMenu(popup)) {
|
|
470
|
+
this.#query = '';
|
|
471
|
+
this.#menu = popup;
|
|
472
|
+
}
|
|
473
|
+
this.requestUpdate();
|
|
474
|
+
await this.updateComplete;
|
|
475
|
+
const element = this.renderRoot.querySelector(`#${popoverOf(popup)}`);
|
|
476
|
+
const opener = this.renderRoot.querySelector(`[data-popup="${openerOf(popup)}"]`);
|
|
477
|
+
if (element === null || opener === null)
|
|
478
|
+
return;
|
|
479
|
+
if (!element.matches(':popover-open'))
|
|
480
|
+
element.showPopover();
|
|
481
|
+
// Its descriptions load now (`menuMessages`), not with the editor.
|
|
482
|
+
this.menu?.requestUpdate();
|
|
483
|
+
this.#place(element, opener);
|
|
484
|
+
const first = popup === 'style' || STYLE_POPUPS.has(popup)
|
|
485
|
+
? (element.querySelector('[aria-checked="true"], [aria-pressed="true"]') ??
|
|
486
|
+
element.querySelector('button'))
|
|
487
|
+
: popup === 'panel'
|
|
488
|
+
? element
|
|
489
|
+
: element.querySelector('input');
|
|
490
|
+
first?.focus();
|
|
491
|
+
}
|
|
492
|
+
/** Closes the open popup; the focus goes back to its button with `back`. */
|
|
493
|
+
#close(back) {
|
|
494
|
+
const popup = this.#popup;
|
|
495
|
+
if (popup === null)
|
|
496
|
+
return;
|
|
497
|
+
this.#popup = null;
|
|
498
|
+
const element = this.renderRoot.querySelector(`#${popoverOf(popup)}`);
|
|
499
|
+
if (element?.matches(':popover-open') === true)
|
|
500
|
+
element.hidePopover();
|
|
501
|
+
if (back)
|
|
502
|
+
this.renderRoot.querySelector(`[data-popup="${openerOf(popup)}"]`)?.focus();
|
|
503
|
+
this.requestUpdate();
|
|
504
|
+
}
|
|
505
|
+
/** Puts a popup below its button, its start edge on the button's, inside the viewport. */
|
|
506
|
+
#place(popup, opener) {
|
|
507
|
+
const rect = opener.getBoundingClientRect();
|
|
508
|
+
const rtl = getComputedStyle(opener).direction === 'rtl';
|
|
509
|
+
const width = popup.offsetWidth;
|
|
510
|
+
const room = document.documentElement.clientWidth;
|
|
511
|
+
const start = rtl ? room - rect.right : rect.left;
|
|
512
|
+
const inset = Math.max(8, Math.min(start, room - width - 8));
|
|
513
|
+
popup.style.setProperty('inset-inline-start', `${inset}px`);
|
|
514
|
+
// Below the button, or as low as it fits: a tall popup (the style form) from a bar far down the page.
|
|
515
|
+
const below = document.documentElement.clientHeight - popup.offsetHeight - 8;
|
|
516
|
+
popup.style.setProperty('inset-block-start', `${Math.max(8, Math.min(rect.bottom + 4, below))}px`);
|
|
517
|
+
}
|
|
518
|
+
/** After a command: the editor gets the focus back when a key pressed the button (a pointer never took it). */
|
|
519
|
+
#done() {
|
|
520
|
+
this.adapter?.focus?.();
|
|
521
|
+
}
|
|
522
|
+
#format(format, href) {
|
|
523
|
+
if (!this.#state.enabled.includes(format))
|
|
524
|
+
return;
|
|
525
|
+
this.adapter?.format?.(format, href);
|
|
526
|
+
this.#done();
|
|
527
|
+
this.requestUpdate();
|
|
528
|
+
}
|
|
529
|
+
/** A style on the selected text (with only a cursor, on what is typed next), the popup closed. */
|
|
530
|
+
#setText(changes) {
|
|
531
|
+
this.adapter?.style?.setText(changes);
|
|
532
|
+
this.#close(false);
|
|
533
|
+
this.#done();
|
|
534
|
+
this.requestUpdate();
|
|
535
|
+
}
|
|
536
|
+
/** A style on each text block of the selection, the popup closed. */
|
|
537
|
+
#setBlock(changes) {
|
|
538
|
+
this.adapter?.style?.setTarget('block', changes);
|
|
539
|
+
this.#close(false);
|
|
540
|
+
this.#done();
|
|
541
|
+
this.requestUpdate();
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Where a named style goes: on the selected text, and with only a cursor on what the cursor is in. A style of boxes
|
|
545
|
+
* (`box`) goes on what the cursor is in, words selected or not, as a paragraph style does in a word processor.
|
|
546
|
+
*/
|
|
547
|
+
#namedTarget(box = false) {
|
|
548
|
+
const styles = this.#styles;
|
|
549
|
+
if (styles === null)
|
|
550
|
+
return null;
|
|
551
|
+
return styles.hasSelection && !box ? 'text' : (styles.targets[0]?.kind ?? null);
|
|
552
|
+
}
|
|
553
|
+
/** The named style on that target. */
|
|
554
|
+
#namedNow(box = false) {
|
|
555
|
+
const where = this.#namedTarget(box);
|
|
556
|
+
return where === 'text'
|
|
557
|
+
? (this.#styles?.textClass ?? null)
|
|
558
|
+
: (this.#styles?.targets.find((target) => target.kind === where)?.className ?? null);
|
|
559
|
+
}
|
|
560
|
+
/** Whether a named style is a ready style of boxes: its rule in the document keeps that way of going on. */
|
|
561
|
+
#isBox(name) {
|
|
562
|
+
return this.authoring?.styles.some((ready) => ready.name === name && ready.text !== true) ?? false;
|
|
563
|
+
}
|
|
564
|
+
/** Sets a named style, or takes it away when it is there; a ready style (`rule`) brings its rule in the same step. */
|
|
565
|
+
#setNamed(name, rule) {
|
|
566
|
+
const box = this.#isBox(name);
|
|
567
|
+
const where = this.#namedTarget(box);
|
|
568
|
+
if (where !== null) {
|
|
569
|
+
if (rule === undefined)
|
|
570
|
+
this.adapter?.style?.setClass(where, this.#namedNow(box) === name ? null : name);
|
|
571
|
+
else
|
|
572
|
+
this.adapter?.style?.setClass(where, name, rule);
|
|
573
|
+
}
|
|
574
|
+
this.#close(false);
|
|
575
|
+
this.#done();
|
|
576
|
+
this.requestUpdate();
|
|
577
|
+
}
|
|
578
|
+
/** The style at the cursor as a font's name: the host's, else the stack's first name; '' with none or a mix. */
|
|
579
|
+
#fontName() {
|
|
580
|
+
const stack = this.#styles?.text['font-family'];
|
|
581
|
+
if (stack === undefined)
|
|
582
|
+
return '';
|
|
583
|
+
const known = this.authoring?.fonts.find((font) => font.stack === stack);
|
|
584
|
+
return known?.name ?? (stack.split(',')[0] ?? '').replace(/"/g, '').trim();
|
|
585
|
+
}
|
|
586
|
+
/** The bar's style controls, by group (font and size; colours; alignment and spacing), and clear formatting. */
|
|
587
|
+
#styleGroups() {
|
|
588
|
+
const styles = this.#styles;
|
|
589
|
+
if (styles === null)
|
|
590
|
+
return { fonts: [], colors: [], blocks: [], clear: [] };
|
|
591
|
+
const { text, textEnabled, targets } = styles;
|
|
592
|
+
const hasBlock = targets.some((target) => target.kind === 'block');
|
|
593
|
+
const popupItem = (popup, label, enabled, icon, iconName) => ({
|
|
594
|
+
id: popup,
|
|
595
|
+
label,
|
|
596
|
+
text: null,
|
|
597
|
+
icon,
|
|
598
|
+
iconName,
|
|
599
|
+
disabled: !enabled,
|
|
600
|
+
popup,
|
|
601
|
+
press: () => {
|
|
602
|
+
if (this.#popup === popup)
|
|
603
|
+
this.#close(true);
|
|
604
|
+
else
|
|
605
|
+
void this.#open(popup);
|
|
606
|
+
},
|
|
607
|
+
});
|
|
608
|
+
const font = this.#fontName();
|
|
609
|
+
const size = /^(\d+(?:\.\d+)?)px$/.exec(text['font-size'] ?? '')?.[1] ?? text['font-size'] ?? '';
|
|
610
|
+
const valued = (popup, label, value) => ({
|
|
611
|
+
...popupItem(popup, this.t(label, { value: value === '' ? this.t('toolbar.default') : value }), textEnabled, ICONS.chevron, 'chevron'),
|
|
612
|
+
text: value === '' ? this.t(`toolbar.${popup}.name`) : value,
|
|
613
|
+
menu: true,
|
|
614
|
+
});
|
|
615
|
+
const blockStyle = targets.find((target) => target.kind === 'block')?.style ?? '';
|
|
616
|
+
const align = styleMapOf(blockStyle).get('text-align');
|
|
617
|
+
const alignIcon = align === 'right' || align === 'end'
|
|
618
|
+
? 'align-end'
|
|
619
|
+
: align === 'center'
|
|
620
|
+
? 'align-center'
|
|
621
|
+
: align === 'justify'
|
|
622
|
+
? 'align-justify'
|
|
623
|
+
: 'align-start';
|
|
624
|
+
return {
|
|
625
|
+
fonts: [valued('font', 'toolbar.font', font), valued('size', 'toolbar.size', size)],
|
|
626
|
+
colors: [
|
|
627
|
+
{
|
|
628
|
+
...popupItem('color', this.t('toolbar.color'), textEnabled, ICONS.color, 'color'),
|
|
629
|
+
bar: text.color ?? null,
|
|
630
|
+
},
|
|
631
|
+
{
|
|
632
|
+
...popupItem('highlight', this.t('toolbar.highlight'), textEnabled, ICONS.highlight, 'highlight'),
|
|
633
|
+
bar: text['background-color'] ?? null,
|
|
634
|
+
},
|
|
635
|
+
],
|
|
636
|
+
blocks: [
|
|
637
|
+
popupItem('align', this.t('toolbar.align'), hasBlock, ICONS[alignIcon], alignIcon),
|
|
638
|
+
popupItem('spacing', this.t('toolbar.spacing'), hasBlock, ICONS.spacing, 'spacing'),
|
|
639
|
+
],
|
|
640
|
+
clear: [
|
|
641
|
+
{
|
|
642
|
+
id: 'clear',
|
|
643
|
+
label: this.t('toolbar.clear'),
|
|
644
|
+
text: null,
|
|
645
|
+
icon: ICONS.clear,
|
|
646
|
+
iconName: 'clear',
|
|
647
|
+
disabled: !textEnabled && !hasBlock,
|
|
648
|
+
press: () => {
|
|
649
|
+
this.adapter?.style?.clear();
|
|
650
|
+
this.#done();
|
|
651
|
+
this.requestUpdate();
|
|
652
|
+
},
|
|
653
|
+
},
|
|
654
|
+
],
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
async #insert(name) {
|
|
658
|
+
const menu = this.menu;
|
|
659
|
+
const adapter = this.adapter;
|
|
660
|
+
if (menu === null || adapter === null)
|
|
661
|
+
return;
|
|
662
|
+
if (name === 'text') {
|
|
663
|
+
adapter.insert('<p></p>');
|
|
664
|
+
this.#done();
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
if (name === 'picture')
|
|
668
|
+
await menu.insertPicture();
|
|
669
|
+
else if (name === 'page-break')
|
|
670
|
+
menu.insertPageBreak();
|
|
671
|
+
else {
|
|
672
|
+
const [tag = '', template] = name.split(':');
|
|
673
|
+
await menu.add(tag, template);
|
|
674
|
+
}
|
|
675
|
+
this.#done();
|
|
676
|
+
}
|
|
677
|
+
/** The featured names this editor can insert, as buttons. */
|
|
678
|
+
#featuredItems() {
|
|
679
|
+
const authoring = this.authoring;
|
|
680
|
+
if (authoring === null)
|
|
681
|
+
return [];
|
|
682
|
+
const disabled = this.adapter === null;
|
|
683
|
+
const items = [];
|
|
684
|
+
for (const name of this.featured) {
|
|
685
|
+
if (name === 'text' || name === 'picture') {
|
|
686
|
+
const label = this.t(name === 'text' ? 'insert.text' : 'insert.picture');
|
|
687
|
+
items.push({
|
|
688
|
+
id: `insert-${name}`,
|
|
689
|
+
label,
|
|
690
|
+
text: label,
|
|
691
|
+
icon: name === 'text' ? ICONS.text : GLYPHS.picture,
|
|
692
|
+
iconName: name === 'text' ? 'text' : 'image',
|
|
693
|
+
disabled,
|
|
694
|
+
data: { content: name },
|
|
695
|
+
press: () => void this.#insert(name),
|
|
696
|
+
});
|
|
697
|
+
continue;
|
|
698
|
+
}
|
|
699
|
+
if (name === 'page-break') {
|
|
700
|
+
if (this.adapter?.splitPage === undefined)
|
|
701
|
+
continue;
|
|
702
|
+
const label = this.t('insert.pageBreak');
|
|
703
|
+
items.push({
|
|
704
|
+
id: 'insert-page-break',
|
|
705
|
+
label,
|
|
706
|
+
text: label,
|
|
707
|
+
icon: GLYPHS.page,
|
|
708
|
+
iconName: 'files',
|
|
709
|
+
data: { content: name },
|
|
710
|
+
press: () => void this.#insert(name),
|
|
711
|
+
});
|
|
712
|
+
continue;
|
|
713
|
+
}
|
|
714
|
+
const [tag = '', template] = name.split(':');
|
|
715
|
+
const descriptor = authoring.descriptor(tag);
|
|
716
|
+
if (descriptor === null)
|
|
717
|
+
continue;
|
|
718
|
+
const starter = template === undefined ? null : authoring.templates(tag).find((t) => t.id === template);
|
|
719
|
+
if (starter === undefined)
|
|
720
|
+
continue;
|
|
721
|
+
const label = authoring.t(descriptor.manifest.messages, starter?.label ?? descriptor.label);
|
|
722
|
+
const icon = starter?.icon ?? descriptor.icon;
|
|
723
|
+
items.push({
|
|
724
|
+
id: `insert-${name}`,
|
|
725
|
+
label,
|
|
726
|
+
text: label,
|
|
727
|
+
icon: TYPE_GLYPHS.get(icon) ?? GLYPHS[descriptor.manifest.display],
|
|
728
|
+
iconName: icon,
|
|
729
|
+
disabled,
|
|
730
|
+
data: template === undefined ? { tag } : { tag, template },
|
|
731
|
+
press: () => void this.#insert(name),
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
return items;
|
|
735
|
+
}
|
|
736
|
+
/** The bar's buttons in order, by group (named for its class). */
|
|
737
|
+
#groups() {
|
|
738
|
+
const adapter = this.adapter;
|
|
739
|
+
const authoring = this.authoring;
|
|
740
|
+
const state = this.#state;
|
|
741
|
+
const groups = [];
|
|
742
|
+
const formatItem = (format, popup) => ({
|
|
743
|
+
id: format,
|
|
744
|
+
label: this.t(`toolbar.${format}`),
|
|
745
|
+
text: null,
|
|
746
|
+
icon: ICONS[format],
|
|
747
|
+
iconName: format,
|
|
748
|
+
pressed: state.active.includes(format),
|
|
749
|
+
disabled: !state.enabled.includes(format),
|
|
750
|
+
...(popup === undefined ? {} : { popup }),
|
|
751
|
+
press: () => {
|
|
752
|
+
if (popup !== undefined)
|
|
753
|
+
void this.#open(popup);
|
|
754
|
+
else
|
|
755
|
+
this.#format(format);
|
|
756
|
+
},
|
|
757
|
+
});
|
|
758
|
+
if (adapter?.format !== undefined && adapter.formatState !== undefined) {
|
|
759
|
+
const current = STYLES.find((style) => state.active.includes(style));
|
|
760
|
+
// The named style on what the menu would change says more than the tag.
|
|
761
|
+
const named = this.#namedNow();
|
|
762
|
+
const rule = named === null ? undefined : adapter.style?.rules().find((one) => one.name === named);
|
|
763
|
+
const styleName = rule?.label ?? rule?.name ?? this.t(`toolbar.${current ?? 'paragraph'}`);
|
|
764
|
+
const style = this.#styleGroups();
|
|
765
|
+
groups.push([
|
|
766
|
+
'group',
|
|
767
|
+
[
|
|
768
|
+
{
|
|
769
|
+
id: 'style',
|
|
770
|
+
label: this.t('toolbar.style', { style: styleName }),
|
|
771
|
+
text: styleName,
|
|
772
|
+
icon: ICONS.chevron,
|
|
773
|
+
iconName: 'chevron',
|
|
774
|
+
menu: true,
|
|
775
|
+
// A selected picture or component has no text style, but its style form is in this menu.
|
|
776
|
+
disabled: !STYLES.some((one) => state.enabled.includes(one)) && (this.#styles?.targets.length ?? 0) === 0,
|
|
777
|
+
popup: 'style',
|
|
778
|
+
press: () => void this.#open('style'),
|
|
779
|
+
},
|
|
780
|
+
],
|
|
781
|
+
]);
|
|
782
|
+
if (style.fonts.length > 0)
|
|
783
|
+
groups.push(['group', style.fonts]);
|
|
784
|
+
groups.push(['group', [...MARKS.map((mark) => formatItem(mark)), formatItem('link', 'link')]]);
|
|
785
|
+
if (style.colors.length > 0)
|
|
786
|
+
groups.push(['group', style.colors]);
|
|
787
|
+
if (style.blocks.length > 0)
|
|
788
|
+
groups.push(['group', style.blocks]);
|
|
789
|
+
groups.push(['group', [...LISTS.map((list) => formatItem(list)), ...style.clear]]);
|
|
790
|
+
}
|
|
791
|
+
const menu = (popup, icon) => ({
|
|
792
|
+
id: popup,
|
|
793
|
+
label: this.t(`toolbar.${popup}`),
|
|
794
|
+
text: this.t(`toolbar.${popup}`),
|
|
795
|
+
icon: ICONS[icon],
|
|
796
|
+
iconName: icon,
|
|
797
|
+
disabled: adapter === null,
|
|
798
|
+
popup,
|
|
799
|
+
press: () => {
|
|
800
|
+
if (this.#popup === popup)
|
|
801
|
+
this.#close(true);
|
|
802
|
+
else
|
|
803
|
+
void this.#open(popup);
|
|
804
|
+
},
|
|
805
|
+
});
|
|
806
|
+
const starters = authoring?.descriptors.some((d) => (d.templates ?? []).length > 0) === true;
|
|
807
|
+
groups.push([
|
|
808
|
+
'group inserts',
|
|
809
|
+
[
|
|
810
|
+
...this.#featuredItems(),
|
|
811
|
+
menu('elements', 'add'),
|
|
812
|
+
...(starters ? [menu('templates', 'templates')] : []),
|
|
813
|
+
// Layouts are styles: a host without the style API has none to insert.
|
|
814
|
+
...(adapter?.style !== undefined ? [menu('layouts', 'layouts')] : []),
|
|
815
|
+
],
|
|
816
|
+
]);
|
|
817
|
+
if (adapter?.wrap !== undefined && authoring !== null) {
|
|
818
|
+
const wraps = authoring.descriptors.filter((d) => d.wrap !== undefined);
|
|
819
|
+
if (wraps.length > 0) {
|
|
820
|
+
groups.push([
|
|
821
|
+
'group',
|
|
822
|
+
wraps.map((d) => {
|
|
823
|
+
const label = authoring.t(d.manifest.messages, d.wrap?.label ?? '');
|
|
824
|
+
return {
|
|
825
|
+
id: `wrap-${d.manifest.tag}`,
|
|
826
|
+
label,
|
|
827
|
+
text: label,
|
|
828
|
+
icon: ICONS.wrap,
|
|
829
|
+
iconName: 'wrap',
|
|
830
|
+
data: { wrap: d.manifest.tag },
|
|
831
|
+
press: () => {
|
|
832
|
+
this.adapter?.wrap?.(d.manifest.tag);
|
|
833
|
+
this.#done();
|
|
834
|
+
},
|
|
835
|
+
};
|
|
836
|
+
}),
|
|
837
|
+
]);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
if (this.views.length > 0) {
|
|
841
|
+
groups.push([
|
|
842
|
+
'group views',
|
|
843
|
+
this.views.map((view) => ({
|
|
844
|
+
id: `view-${view.name}`,
|
|
845
|
+
label: view.label,
|
|
846
|
+
text: view.label,
|
|
847
|
+
icon: null,
|
|
848
|
+
iconName: '',
|
|
849
|
+
checked: view.name === this.view,
|
|
850
|
+
press: () => {
|
|
851
|
+
if (view.name === this.view)
|
|
852
|
+
return;
|
|
853
|
+
const detail = { view: view.name };
|
|
854
|
+
this.dispatchEvent(new CustomEvent(VIEW_PICKED, { detail, bubbles: true, composed: true }));
|
|
855
|
+
},
|
|
856
|
+
})),
|
|
857
|
+
]);
|
|
858
|
+
}
|
|
859
|
+
if (adapter?.undo !== undefined && adapter.redo !== undefined) {
|
|
860
|
+
const history = (name) => ({
|
|
861
|
+
id: name,
|
|
862
|
+
label: this.t(`toolbar.${name}`),
|
|
863
|
+
text: null,
|
|
864
|
+
icon: ICONS[name],
|
|
865
|
+
iconName: name,
|
|
866
|
+
disabled: !state[name],
|
|
867
|
+
press: () => {
|
|
868
|
+
if (!state[name])
|
|
869
|
+
return;
|
|
870
|
+
adapter[name]?.();
|
|
871
|
+
this.#done();
|
|
872
|
+
},
|
|
873
|
+
});
|
|
874
|
+
groups.push(['group history', [history('undo'), history('redo')]]);
|
|
875
|
+
}
|
|
876
|
+
return groups;
|
|
877
|
+
}
|
|
878
|
+
#keydown(event) {
|
|
879
|
+
const target = event.target.closest('[data-item]');
|
|
880
|
+
if (target === null)
|
|
881
|
+
return;
|
|
882
|
+
const index = Number(target.dataset.item);
|
|
883
|
+
const item = this.#items[index];
|
|
884
|
+
const menu = item?.popup === 'style' || STYLE_POPUPS.has(item?.popup ?? '');
|
|
885
|
+
if (menu && (event.key === 'ArrowDown' || event.key === 'ArrowUp')) {
|
|
886
|
+
event.preventDefault();
|
|
887
|
+
if (item?.disabled !== true && item?.popup !== undefined)
|
|
888
|
+
void this.#open(item.popup);
|
|
889
|
+
return;
|
|
890
|
+
}
|
|
891
|
+
this.#roving.keydown(event, index);
|
|
892
|
+
}
|
|
893
|
+
#button(item, index) {
|
|
894
|
+
const unavailable = item.disabled === true;
|
|
895
|
+
// The parts: every button; an icon-only one is also an `icon-button`, and an option of the view switch a `view`.
|
|
896
|
+
const part = item.checked !== undefined ? 'button view' : item.text === null ? 'button icon-button' : 'button';
|
|
897
|
+
return html `<button
|
|
898
|
+
type="button"
|
|
899
|
+
part=${part}
|
|
900
|
+
class=${item.text === null ? 'icon-only' : 'labelled'}
|
|
901
|
+
data-item=${index}
|
|
902
|
+
data-id=${item.id}
|
|
903
|
+
data-tag=${item.data?.tag ?? nothing}
|
|
904
|
+
data-template=${item.data?.template ?? nothing}
|
|
905
|
+
data-content=${item.data?.content ?? nothing}
|
|
906
|
+
data-popup=${item.popup ?? nothing}
|
|
907
|
+
tabindex=${this.#roving.tabIndex(index)}
|
|
908
|
+
aria-label=${item.text === null || item.menu === true ? item.label : nothing}
|
|
909
|
+
title=${item.text === null ? item.label : nothing}
|
|
910
|
+
aria-pressed=${item.pressed === undefined ? nothing : String(item.pressed)}
|
|
911
|
+
role=${item.checked === undefined ? nothing : 'radio'}
|
|
912
|
+
aria-checked=${item.checked === undefined ? nothing : String(item.checked)}
|
|
913
|
+
aria-disabled=${unavailable ? 'true' : nothing}
|
|
914
|
+
aria-haspopup=${item.popup === undefined
|
|
915
|
+
? nothing
|
|
916
|
+
: item.popup === 'style' || item.menu === true || item.popup === 'align' || item.popup === 'spacing'
|
|
917
|
+
? 'menu'
|
|
918
|
+
: 'dialog'}
|
|
919
|
+
aria-expanded=${item.popup === undefined ? nothing : String(this.#popup === item.popup)}
|
|
920
|
+
aria-controls=${item.popup === undefined ? nothing : popoverOf(item.popup)}
|
|
921
|
+
@click=${() => {
|
|
922
|
+
this.#roving.focus(index);
|
|
923
|
+
if (!unavailable)
|
|
924
|
+
item.press();
|
|
925
|
+
}}
|
|
926
|
+
>
|
|
927
|
+
${item.menu === true
|
|
928
|
+
? html `<span class="value">${item.text}</span>
|
|
929
|
+
<svg class="icon chevron" part="icon icon-chevron" viewBox="0 0 20 20" aria-hidden="true" focusable="false">${item.icon}</svg>`
|
|
930
|
+
: html `${item.icon === null ? nothing : html `<svg class="icon" part="icon icon-${item.iconName}" data-icon=${item.iconName} viewBox="0 0 20 20" aria-hidden="true" focusable="false">${item.icon}</svg>`}
|
|
931
|
+
${item.text === null ? nothing : html `<span>${item.text}</span>`}${item.bar === undefined || item.bar === null
|
|
932
|
+
? nothing
|
|
933
|
+
: html `<span class="color-mark" data-color=${item.bar}></span>`}`}
|
|
934
|
+
</button>`;
|
|
935
|
+
}
|
|
936
|
+
/** The arrows, Home and End along a menu's buttons; Escape closes it, back on its button; Tab goes on. */
|
|
937
|
+
#menuKeydown = (event) => {
|
|
938
|
+
const buttons = [...event.currentTarget.querySelectorAll('button')];
|
|
939
|
+
const at = buttons.indexOf(event.target);
|
|
940
|
+
const step = event.key === 'ArrowDown' ? 1 : event.key === 'ArrowUp' ? -1 : 0;
|
|
941
|
+
if (step !== 0) {
|
|
942
|
+
event.preventDefault();
|
|
943
|
+
buttons[(at + step + buttons.length) % buttons.length]?.focus();
|
|
944
|
+
}
|
|
945
|
+
else if (event.key === 'Home' || event.key === 'End') {
|
|
946
|
+
event.preventDefault();
|
|
947
|
+
buttons[event.key === 'Home' ? 0 : buttons.length - 1]?.focus();
|
|
948
|
+
}
|
|
949
|
+
else if (event.key === 'Escape') {
|
|
950
|
+
event.preventDefault();
|
|
951
|
+
this.#close(true);
|
|
952
|
+
}
|
|
953
|
+
else if (event.key === 'Tab')
|
|
954
|
+
this.#close(false);
|
|
955
|
+
};
|
|
956
|
+
/**
|
|
957
|
+
* The rest of the style menu: the document's named styles, the ready styles it does not have yet, then the forms of
|
|
958
|
+
* what the cursor is in.
|
|
959
|
+
*/
|
|
960
|
+
#styleMenuMore() {
|
|
961
|
+
const styles = this.#styles;
|
|
962
|
+
if (styles === null)
|
|
963
|
+
return nothing;
|
|
964
|
+
const named = this.adapter?.style?.rules().filter((rule) => rule.name !== undefined) ?? [];
|
|
965
|
+
const own = new Set(named.map((rule) => rule.name));
|
|
966
|
+
const ready = (this.authoring?.styles ?? []).filter((preset) => !own.has(preset.name));
|
|
967
|
+
const targets = styles.targets;
|
|
968
|
+
const group = (label, items) => html `<hr />
|
|
969
|
+
<div class="menu-section" role="group" aria-label=${label}>
|
|
970
|
+
<div class="menu-group" aria-hidden="true">${label}</div>
|
|
971
|
+
${items}
|
|
972
|
+
</div>`;
|
|
973
|
+
return html `${named.length === 0
|
|
974
|
+
? nothing
|
|
975
|
+
: group(this.t('toolbar.myStyles'), named.map((rule) => {
|
|
976
|
+
const name = rule.name ?? '';
|
|
977
|
+
const box = this.#isBox(name);
|
|
978
|
+
const none = this.#namedTarget(box) === null;
|
|
979
|
+
return html `<button
|
|
980
|
+
type="button"
|
|
981
|
+
role="menuitemradio"
|
|
982
|
+
tabindex="-1"
|
|
983
|
+
data-named=${name}
|
|
984
|
+
aria-checked=${String(this.#namedNow(box) === name)}
|
|
985
|
+
aria-disabled=${none ? 'true' : nothing}
|
|
986
|
+
@click=${() => {
|
|
987
|
+
if (!none)
|
|
988
|
+
this.#setNamed(name);
|
|
989
|
+
}}
|
|
990
|
+
>
|
|
991
|
+
${rule.label ?? name}
|
|
992
|
+
</button>`;
|
|
993
|
+
}))}
|
|
994
|
+
${ready.length === 0
|
|
995
|
+
? nothing
|
|
996
|
+
: group(this.t('toolbar.readyStyles'), ready.map(({ name, label, style, text }) => {
|
|
997
|
+
const none = this.#namedTarget(text !== true) === null;
|
|
998
|
+
return html `<button
|
|
999
|
+
type="button"
|
|
1000
|
+
role="menuitem"
|
|
1001
|
+
tabindex="-1"
|
|
1002
|
+
data-ready=${name}
|
|
1003
|
+
aria-disabled=${none ? 'true' : nothing}
|
|
1004
|
+
@click=${() => {
|
|
1005
|
+
// Its label as the author reads it now: from here on it is a style of this exercise.
|
|
1006
|
+
if (!none)
|
|
1007
|
+
this.#setNamed(name, { name, label, style });
|
|
1008
|
+
}}
|
|
1009
|
+
>
|
|
1010
|
+
${label}
|
|
1011
|
+
</button>`;
|
|
1012
|
+
}))}
|
|
1013
|
+
<hr />
|
|
1014
|
+
${targets.map((target) => html `<button
|
|
1015
|
+
type="button"
|
|
1016
|
+
role="menuitem"
|
|
1017
|
+
tabindex="-1"
|
|
1018
|
+
data-style-target=${target.kind}
|
|
1019
|
+
@click=${() => void this.openStyle(target.kind)}
|
|
1020
|
+
>
|
|
1021
|
+
${this.t('style.target.more', { target: this.t(`style.target.${target.kind}`) })}
|
|
1022
|
+
</button>`)}
|
|
1023
|
+
<button
|
|
1024
|
+
type="button"
|
|
1025
|
+
role="menuitem"
|
|
1026
|
+
tabindex="-1"
|
|
1027
|
+
data-style-target="rules"
|
|
1028
|
+
@click=${() => void this.openStyle('rules')}
|
|
1029
|
+
>
|
|
1030
|
+
${this.t('style.rules.open')}
|
|
1031
|
+
</button>`;
|
|
1032
|
+
}
|
|
1033
|
+
/** The open popup of the style controls, from its module. */
|
|
1034
|
+
#stylePopup() {
|
|
1035
|
+
const module = this.#stylePopupsModule;
|
|
1036
|
+
const popup = this.#popup;
|
|
1037
|
+
const styles = this.#styles;
|
|
1038
|
+
if (module === null || styles === null || (popup !== 'panel' && !STYLE_POPUPS.has(popup ?? '')))
|
|
1039
|
+
return nothing;
|
|
1040
|
+
return module.stylePopup({
|
|
1041
|
+
popup: popup === 'panel' ? { panel: this.#panel } : popup,
|
|
1042
|
+
styles,
|
|
1043
|
+
fonts: this.authoring?.fonts ?? [],
|
|
1044
|
+
colorError: this.#colorError,
|
|
1045
|
+
authoring: this.authoring,
|
|
1046
|
+
adapter: this.adapter,
|
|
1047
|
+
t: (key, params) => this.t(key, params),
|
|
1048
|
+
setText: (changes) => this.#setText(changes),
|
|
1049
|
+
setBlock: (changes) => this.#setBlock(changes),
|
|
1050
|
+
refuseColor: () => {
|
|
1051
|
+
this.#colorError = true;
|
|
1052
|
+
this.requestUpdate();
|
|
1053
|
+
},
|
|
1054
|
+
close: (back) => this.#close(back),
|
|
1055
|
+
menuKeydown: this.#menuKeydown,
|
|
1056
|
+
});
|
|
1057
|
+
}
|
|
1058
|
+
#styleMenu() {
|
|
1059
|
+
const state = this.#state;
|
|
1060
|
+
const choose = (style) => {
|
|
1061
|
+
this.#close(false);
|
|
1062
|
+
if (!state.active.includes(style) || style === 'paragraph')
|
|
1063
|
+
this.#format(style);
|
|
1064
|
+
else
|
|
1065
|
+
this.#done();
|
|
1066
|
+
};
|
|
1067
|
+
return html `<div
|
|
1068
|
+
id="style"
|
|
1069
|
+
class="menu"
|
|
1070
|
+
popover="manual"
|
|
1071
|
+
role="menu"
|
|
1072
|
+
aria-label=${this.t('toolbar.style', { style: this.t(`toolbar.${STYLES.find((s) => state.active.includes(s)) ?? 'paragraph'}`) })}
|
|
1073
|
+
part="style-menu"
|
|
1074
|
+
@keydown=${this.#menuKeydown}
|
|
1075
|
+
>
|
|
1076
|
+
${STYLES.map((style) => html `<button
|
|
1077
|
+
type="button"
|
|
1078
|
+
role="menuitemradio"
|
|
1079
|
+
tabindex="-1"
|
|
1080
|
+
data-format=${style}
|
|
1081
|
+
aria-checked=${String(state.active.includes(style))}
|
|
1082
|
+
aria-disabled=${state.enabled.includes(style) ? nothing : 'true'}
|
|
1083
|
+
@click=${() => choose(style)}
|
|
1084
|
+
>
|
|
1085
|
+
${this.t(`toolbar.${style}`)}
|
|
1086
|
+
</button>`)}
|
|
1087
|
+
${this.#styleMenuMore()}
|
|
1088
|
+
</div>`;
|
|
1089
|
+
}
|
|
1090
|
+
#linkPopup() {
|
|
1091
|
+
const state = this.#state;
|
|
1092
|
+
const apply = () => {
|
|
1093
|
+
const input = this.renderRoot.querySelector('#link-address');
|
|
1094
|
+
const href = input?.value.trim() ?? '';
|
|
1095
|
+
if (href === '') {
|
|
1096
|
+
this.#close(false);
|
|
1097
|
+
this.#format('link');
|
|
1098
|
+
return;
|
|
1099
|
+
}
|
|
1100
|
+
if (this.authoring?.urlPolicy(href, { tag: 'a', attribute: 'href' }) !== true) {
|
|
1101
|
+
this.#linkError = true;
|
|
1102
|
+
this.requestUpdate();
|
|
1103
|
+
input?.focus();
|
|
1104
|
+
return;
|
|
1105
|
+
}
|
|
1106
|
+
this.#close(false);
|
|
1107
|
+
this.#format('link', href);
|
|
1108
|
+
};
|
|
1109
|
+
return html `<div
|
|
1110
|
+
id="link"
|
|
1111
|
+
class="link"
|
|
1112
|
+
popover="manual"
|
|
1113
|
+
role="dialog"
|
|
1114
|
+
aria-label=${this.t('toolbar.link')}
|
|
1115
|
+
part="link"
|
|
1116
|
+
@keydown=${(event) => {
|
|
1117
|
+
if (event.key === 'Escape') {
|
|
1118
|
+
event.preventDefault();
|
|
1119
|
+
this.#close(true);
|
|
1120
|
+
}
|
|
1121
|
+
else if (event.key === 'Enter' && event.target.localName === 'input') {
|
|
1122
|
+
event.preventDefault();
|
|
1123
|
+
apply();
|
|
1124
|
+
}
|
|
1125
|
+
}}
|
|
1126
|
+
>
|
|
1127
|
+
<label for="link-address">${this.t('toolbar.link.address')}</label>
|
|
1128
|
+
<input
|
|
1129
|
+
id="link-address"
|
|
1130
|
+
type="url"
|
|
1131
|
+
inputmode="url"
|
|
1132
|
+
.value=${this.#popup === 'link' ? (state.href ?? '') : ''}
|
|
1133
|
+
aria-invalid=${this.#linkError ? 'true' : nothing}
|
|
1134
|
+
aria-describedby=${this.#linkError ? 'link-error' : nothing}
|
|
1135
|
+
/>
|
|
1136
|
+
<div role="alert">${this.#linkError ? html `<p class="error" id="link-error">${this.t('toolbar.link.blocked')}</p>` : nothing}</div>
|
|
1137
|
+
<div class="buttons">
|
|
1138
|
+
<button type="button" class="primary" @click=${apply}>${this.t('toolbar.link.apply')}</button>
|
|
1139
|
+
${state.href === null
|
|
1140
|
+
? nothing
|
|
1141
|
+
: html `<button
|
|
1142
|
+
type="button"
|
|
1143
|
+
@click=${() => {
|
|
1144
|
+
this.#close(false);
|
|
1145
|
+
this.#format('link');
|
|
1146
|
+
}}
|
|
1147
|
+
>
|
|
1148
|
+
${this.t('toolbar.link.remove')}
|
|
1149
|
+
</button>`}
|
|
1150
|
+
</div>
|
|
1151
|
+
</div>`;
|
|
1152
|
+
}
|
|
1153
|
+
#insertPopup() {
|
|
1154
|
+
const authoring = this.authoring;
|
|
1155
|
+
return html `<div
|
|
1156
|
+
id="insert"
|
|
1157
|
+
class="insert"
|
|
1158
|
+
popover="manual"
|
|
1159
|
+
role="dialog"
|
|
1160
|
+
aria-label=${this.t(`toolbar.${this.#menu}`)}
|
|
1161
|
+
part="insert"
|
|
1162
|
+
@keydown=${(event) => {
|
|
1163
|
+
if (event.key === 'Escape') {
|
|
1164
|
+
event.preventDefault();
|
|
1165
|
+
this.#close(true);
|
|
1166
|
+
}
|
|
1167
|
+
}}
|
|
1168
|
+
@focusout=${(event) => {
|
|
1169
|
+
const next = event.relatedTarget;
|
|
1170
|
+
if (next !== null && !event.currentTarget.contains(next))
|
|
1171
|
+
this.#close(false);
|
|
1172
|
+
}}
|
|
1173
|
+
@astl-inserted=${() => {
|
|
1174
|
+
this.#close(false);
|
|
1175
|
+
this.#done();
|
|
1176
|
+
}}
|
|
1177
|
+
>
|
|
1178
|
+
<div class="search">
|
|
1179
|
+
<input
|
|
1180
|
+
type="search"
|
|
1181
|
+
aria-label=${this.t('toolbar.search')}
|
|
1182
|
+
placeholder=${this.t('toolbar.search')}
|
|
1183
|
+
.value=${this.#query}
|
|
1184
|
+
@input=${(event) => {
|
|
1185
|
+
this.#query = event.target.value;
|
|
1186
|
+
this.requestUpdate();
|
|
1187
|
+
}}
|
|
1188
|
+
@keydown=${(event) => {
|
|
1189
|
+
if (event.key !== 'Enter')
|
|
1190
|
+
return;
|
|
1191
|
+
event.preventDefault();
|
|
1192
|
+
const first = this.menu?.renderRoot.querySelector('button.insert:not(:disabled)');
|
|
1193
|
+
first?.click();
|
|
1194
|
+
}}
|
|
1195
|
+
/>
|
|
1196
|
+
</div>
|
|
1197
|
+
<astl-insert-menu
|
|
1198
|
+
part="menu"
|
|
1199
|
+
.authoring=${authoring}
|
|
1200
|
+
.adapter=${this.adapter}
|
|
1201
|
+
.filter=${this.#query}
|
|
1202
|
+
.groups=${MENU_GROUPS[this.#menu]}
|
|
1203
|
+
></astl-insert-menu>
|
|
1204
|
+
</div>`;
|
|
1205
|
+
}
|
|
1206
|
+
render() {
|
|
1207
|
+
if (this.authoring === null)
|
|
1208
|
+
return nothing;
|
|
1209
|
+
const { lang, dir } = this.ui;
|
|
1210
|
+
// Out of the editing view (the first), only the switch works.
|
|
1211
|
+
const away = this.views.length > 0 && this.view !== this.views[0]?.name;
|
|
1212
|
+
const groups = this.#groups().map(([name, items]) => [
|
|
1213
|
+
name,
|
|
1214
|
+
away
|
|
1215
|
+
? items.map((item) => (item.checked === undefined ? { ...item, disabled: true } : item))
|
|
1216
|
+
: items,
|
|
1217
|
+
]);
|
|
1218
|
+
this.#items = groups.flatMap(([, items]) => items);
|
|
1219
|
+
let index = 0;
|
|
1220
|
+
// The picture dialog is outside the popovers: a picture asked for from a menu or a featured button shows while
|
|
1221
|
+
// they are closed.
|
|
1222
|
+
return html `<div
|
|
1223
|
+
class="bar"
|
|
1224
|
+
part="toolbar"
|
|
1225
|
+
role="toolbar"
|
|
1226
|
+
aria-label=${this.t('toolbar.label')}
|
|
1227
|
+
lang=${lang}
|
|
1228
|
+
dir=${dir}
|
|
1229
|
+
@mousedown=${(event) => {
|
|
1230
|
+
// A press keeps the editor's focus and selection.
|
|
1231
|
+
if (event.target.closest('button') !== null)
|
|
1232
|
+
event.preventDefault();
|
|
1233
|
+
}}
|
|
1234
|
+
@keydown=${(event) => this.#keydown(event)}
|
|
1235
|
+
>
|
|
1236
|
+
${groups.map(([name, items], at) => html `<div class=${name} role=${name === 'group views' ? 'radiogroup' : 'presentation'}
|
|
1237
|
+
part=${name === 'group views' ? 'views' : nothing}
|
|
1238
|
+
aria-label=${name === 'group views' ? this.t('toolbar.view') : nothing}>
|
|
1239
|
+
${at === 0 || name === 'group views' ? nothing : html `<span class="separator" part="separator" aria-hidden="true"></span>`}
|
|
1240
|
+
${items.map((item) => this.#button(item, index++))}
|
|
1241
|
+
</div>`)}
|
|
1242
|
+
</div>
|
|
1243
|
+
<div lang=${lang} dir=${dir}>
|
|
1244
|
+
${this.#styleMenu()} ${this.#linkPopup()} ${this.#insertPopup()} ${this.#stylePopup()}
|
|
1245
|
+
</div>
|
|
1246
|
+
<astl-picture-dialog .authoring=${this.authoring}></astl-picture-dialog>`;
|
|
1247
|
+
}
|
|
1248
|
+
updated(changed) {
|
|
1249
|
+
super.updated(changed);
|
|
1250
|
+
const menu = this.menu;
|
|
1251
|
+
if (menu !== null)
|
|
1252
|
+
menu.dialog = this.renderRoot.querySelector('astl-picture-dialog');
|
|
1253
|
+
// Through the CSSOM: a strict Content Security Policy blocks a style attribute, which Lit's styleMap writes on its
|
|
1254
|
+
// first render.
|
|
1255
|
+
for (const mark of this.renderRoot.querySelectorAll('.color-mark')) {
|
|
1256
|
+
mark.style.background = mark.dataset.color ?? '';
|
|
1257
|
+
}
|
|
1258
|
+
for (const choice of this.renderRoot.querySelectorAll('[data-font]')) {
|
|
1259
|
+
choice.style.fontFamily = choice.dataset.font ?? '';
|
|
1260
|
+
}
|
|
1261
|
+
// Popups stay where their buttons are while the page scrolls or the bar wraps.
|
|
1262
|
+
if (this.#popup === null)
|
|
1263
|
+
return;
|
|
1264
|
+
const element = this.renderRoot.querySelector(`#${popoverOf(this.#popup)}`);
|
|
1265
|
+
const opener = this.renderRoot.querySelector(`[data-popup="${openerOf(this.#popup)}"]`);
|
|
1266
|
+
if (element !== null && opener !== null && element.matches(':popover-open'))
|
|
1267
|
+
this.#place(element, opener);
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
//# sourceMappingURL=toolbar.js.map
|