-
-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathembedded-toolbar.js
More file actions
450 lines (400 loc) · 16.9 KB
/
embedded-toolbar.js
File metadata and controls
450 lines (400 loc) · 16.9 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
/**
* Minimal embedded toolbar for Phoenix live preview.
* Read mode: "Edit" button
* Edit mode: Format row + "Done" button
* Responsive: progressively collapses groups into dropdowns as width shrinks.
* Level 0: all expanded
* Level 1: block elements collapse
* Level 2: block elements + lists collapse
* Level 3: all groups collapse
*/
import {
createIcons,
Pencil,
Bold,
Italic,
Strikethrough,
Underline,
Code,
Link,
List,
ListOrdered,
ListChecks,
Quote,
Minus,
Table,
FileCode,
ChevronDown,
Type,
MoreHorizontal,
BookOpen,
Link2,
Link2Off,
Printer,
Image as ImageIcon,
Upload,
Sun,
Moon
} from "lucide";
import { on, emit } from "../core/events.js";
import { getState, setState } from "../core/state.js";
import { t, tp } from "../core/i18n.js";
let toolbar = null;
let resizeObserver = null;
let cursorSyncEnabled = true;
let collapseLevel = 0; // 0=expanded, 1=blocks, 2=blocks+lists, 3=all
// Width thresholds for progressive collapse
const THRESHOLD_BLOCKS = 640; // collapse block elements + image first
const THRESHOLD_LISTS = 590; // then lists
const THRESHOLD_TEXT = 590; // finally text formatting (all dropdowns collapsed)
// window.print() from inside an iframe is a no-op in WKWebView (Safari on macOS),
// which is what Tauri uses for the Mac desktop build. Hide the button there.
const _isMacWebKit = /Mac/.test(navigator.platform)
&& /AppleWebKit/.test(navigator.userAgent)
&& !/Chrome|CriOS|Edg|Firefox|FxiOS/.test(navigator.userAgent);
const allIcons = { Bold, Italic, Strikethrough, Underline, Code, Link, List, ListOrdered,
ListChecks, Quote, Minus, Table, FileCode, ChevronDown, Type, MoreHorizontal, Pencil, BookOpen, Link2, Link2Off, Printer, Image: ImageIcon, Upload, Sun, Moon };
export function initEmbeddedToolbar() {
toolbar = document.getElementById("toolbar");
if (!toolbar) return;
render();
on("state:editMode", () => render());
on("state:theme", () => render());
on("editor:selection-state", updateFormatState);
on("state:locale", () => render());
}
function render() {
if (!toolbar) return;
const state = getState();
if (resizeObserver) {
resizeObserver.disconnect();
resizeObserver = null;
}
if (state.editMode) {
renderEditMode(collapseLevel);
setupResponsiveToggle();
} else {
renderReadMode();
}
}
function renderReadMode() {
const isDark = getState().theme === "dark";
toolbar.innerHTML = `<div class="embedded-toolbar">
<div class="toolbar-spacer"></div>
<button class="toolbar-btn theme-toggle-btn" id="emb-theme-toggle" data-tooltip="${t("toolbar.theme") || "Toggle theme"}">
<i data-lucide="${isDark ? "sun" : "moon"}"></i>
</button>
${_isMacWebKit ? "" : `<button class="toolbar-btn print-btn" id="emb-print-btn" data-tooltip="${t("toolbar.print") || "Print"}">
<i data-lucide="printer"></i>
</button>`}
<button class="toolbar-btn cursor-sync-btn${cursorSyncEnabled ? " active" : ""}" id="emb-cursor-sync" data-tooltip="${t("toolbar.cursor_sync") || "Cursor sync"}" aria-pressed="${cursorSyncEnabled}">
<i data-lucide="link-2" class="sync-on-icon"${cursorSyncEnabled ? "" : ' style="display:none"'}></i>
<i data-lucide="link-2-off" class="sync-off-icon"${cursorSyncEnabled ? ' style="display:none"' : ""}></i>
</button>
<button class="edit-toggle-btn" id="emb-edit-btn" title="${t("toolbar.switch_to_edit") || "Switch to edit mode"}">
<i data-lucide="pencil"></i>
<span>${t("toolbar.edit") || "Edit"}</span>
</button>
</div>`;
createIcons({ icons: allIcons, attrs: { class: "" } });
toolbar.querySelectorAll("svg[data-lucide]").forEach(svg => svg.removeAttribute("data-lucide"));
wireThemeToggle();
wireCursorSyncButton();
wirePrintButton();
const editBtn = document.getElementById("emb-edit-btn");
if (editBtn) {
editBtn.addEventListener("click", () => {
emit("request:editMode");
});
}
}
function btn(id, icon, tooltip) {
return `<button class="toolbar-btn format-btn" id="${id}" data-tooltip="${tooltip}" aria-pressed="false"><i data-lucide="${icon}"></i></button>`;
}
function dropdown(group, triggerIcon, tooltip, content) {
return `<div class="toolbar-dropdown" data-group="${group}">
<button class="toolbar-btn toolbar-dropdown-trigger" data-tooltip="${tooltip}"><i data-lucide="${triggerIcon}"></i><i data-lucide="chevron-down" class="dropdown-chevron"></i></button>
<div class="toolbar-dropdown-panel">${content}</div>
</div>`;
}
function renderEditMode(level) {
const isMac = /Mac|iPhone|iPad/.test(navigator.platform);
const mod = isMac ? "\u2318" : "Ctrl";
const blockTypeSelect = `
<select class="block-type-select" id="emb-block-type" title="${t("format.block_type") || "Block type"}">
<option value="<p>">${t("slash.paragraph") || "Paragraph"}</option>
<option value="<h1>">${t("slash.heading1") || "Heading 1"}</option>
<option value="<h2>">${t("slash.heading2") || "Heading 2"}</option>
<option value="<h3>">${t("slash.heading3") || "Heading 3"}</option>
<option value="<h4>">${t("slash.heading4") || "Heading 4"}</option>
<option value="<h5>">${t("slash.heading5") || "Heading 5"}</option>
</select>`;
const textBtns = [
btn("emb-bold", "bold", tp("format.bold", { mod }) || "Bold"),
btn("emb-italic", "italic", tp("format.italic", { mod }) || "Italic"),
btn("emb-strike", "strikethrough", tp("format.strikethrough", { mod }) || "Strikethrough"),
btn("emb-underline", "underline", tp("format.underline", { mod }) || "Underline"),
btn("emb-code", "code", t("format.code") || "Code"),
btn("emb-link", "link", tp("format.link", { mod }) || "Link")
].join("");
const listBtns = [
btn("emb-ul", "list", t("format.bullet_list") || "Bullet list"),
btn("emb-ol", "list-ordered", t("format.numbered_list") || "Numbered list"),
btn("emb-task", "list-checks", t("format.task_list") || "Task list")
].join("");
const blockBtns = [
btn("emb-quote", "quote", t("format.blockquote") || "Quote"),
btn("emb-hr", "minus", t("format.divider") || "Divider"),
btn("emb-table", "table", t("format.table") || "Table"),
btn("emb-codeblock", "file-code", t("format.code_block") || "Code block")
].join("");
const imageBtns = `
<button class="toolbar-btn toolbar-menu-item" id="emb-image-url"><i data-lucide="link"></i><span>${t("format.image_url") || "Image URL"}</span></button>
<button class="toolbar-btn toolbar-menu-item" id="emb-image-upload"><i data-lucide="upload"></i><span>${t("format.image_upload") || "Upload from Computer"}</span></button>`;
// Build the text section (inline or dropdown)
const textSection = level >= 3
? dropdown("text", "type", t("format.text_formatting") || "Text formatting", textBtns)
: textBtns;
// Build the list section (inline or dropdown)
const listSection = level >= 2
? dropdown("lists", "list", t("format.lists") || "Lists", listBtns)
: listBtns;
// Build the block section (inline or dropdown)
const blockSection = level >= 1
? dropdown("blocks", "more-horizontal", t("format.more_elements") || "More", blockBtns)
: blockBtns;
// Image section is always a dropdown (two options inside)
const imageSection = dropdown("image", "image", t("format.image") || "Image", imageBtns);
const formatRow = `
<div class="format-row">
${blockTypeSelect}
<div class="toolbar-divider"></div>
${textSection}
<div class="toolbar-divider"></div>
${listSection}
<div class="toolbar-divider"></div>
${blockSection}
<div class="toolbar-divider"></div>
${imageSection}
</div>`;
const isDark = getState().theme === "dark";
toolbar.innerHTML = `<div class="embedded-toolbar">
${formatRow}
<div class="toolbar-spacer"></div>
<button class="toolbar-btn theme-toggle-btn" id="emb-theme-toggle" data-tooltip="${t("toolbar.theme") || "Toggle theme"}">
<i data-lucide="${isDark ? "sun" : "moon"}"></i>
</button>
${_isMacWebKit ? "" : `<button class="toolbar-btn print-btn" id="emb-print-btn" data-tooltip="${t("toolbar.print") || "Print"}">
<i data-lucide="printer"></i>
</button>`}
<button class="toolbar-btn cursor-sync-btn${cursorSyncEnabled ? " active" : ""}" id="emb-cursor-sync" data-tooltip="${t("toolbar.cursor_sync") || "Cursor sync"}" aria-pressed="${cursorSyncEnabled}">
<i data-lucide="link-2" class="sync-on-icon"${cursorSyncEnabled ? "" : ' style="display:none"'}></i>
<i data-lucide="link-2-off" class="sync-off-icon"${cursorSyncEnabled ? ' style="display:none"' : ""}></i>
</button>
<button class="done-btn" id="emb-done-btn" title="${t("toolbar.switch_to_reader") || "Switch to reader mode"}">
<i data-lucide="book-open"></i>
<span>${t("toolbar.reader") || "Reader"}</span>
</button>
</div>`;
createIcons({ icons: allIcons, attrs: { class: "" } });
toolbar.querySelectorAll("svg[data-lucide]").forEach(svg => svg.removeAttribute("data-lucide"));
wireFormatButtons();
wireBlockTypeSelect();
wireDropdowns();
wireThemeToggle();
wireCursorSyncButton();
wirePrintButton();
wireDoneButton();
}
const formatBindings = [
{ id: "emb-bold", command: "bold" },
{ id: "emb-italic", command: "italic" },
{ id: "emb-strike", command: "strikethrough" },
{ id: "emb-underline", command: "underline" },
{ id: "emb-code", command: "code" },
{ id: "emb-link", command: "createLink" },
{ id: "emb-ul", command: "insertUnorderedList" },
{ id: "emb-ol", command: "insertOrderedList" },
{ id: "emb-task", command: "taskList" },
{ id: "emb-quote", command: "formatBlock", value: "<blockquote>" },
{ id: "emb-hr", command: "insertHorizontalRule" },
{ id: "emb-table", command: "table" },
{ id: "emb-codeblock", command: "codeBlock" },
{ id: "emb-image-url", command: "imageFromUrl" },
{ id: "emb-image-upload", command: "imageUpload" }
];
function wireFormatButtons() {
for (const binding of formatBindings) {
const el = document.getElementById(binding.id);
if (el) {
el.addEventListener("mousedown", (e) => {
e.preventDefault();
emit("action:format", { command: binding.command, value: binding.value });
});
}
}
}
function wireBlockTypeSelect() {
const blockTypeSelect = document.getElementById("emb-block-type");
if (blockTypeSelect) {
blockTypeSelect.addEventListener("change", (e) => {
emit("action:format", { command: "formatBlock", value: e.target.value });
e.target.blur();
});
}
}
function wireDropdowns() {
const dropdowns = toolbar.querySelectorAll(".toolbar-dropdown");
for (const dropdown of dropdowns) {
const trigger = dropdown.querySelector(".toolbar-dropdown-trigger");
if (!trigger) continue;
trigger.addEventListener("mousedown", (e) => {
e.preventDefault();
e.stopPropagation();
const wasOpen = dropdown.classList.contains("open");
closeAllDropdowns();
if (!wasOpen) {
dropdown.classList.add("open");
}
});
}
document.addEventListener("mousedown", (e) => {
if (!e.target.closest(".toolbar-dropdown")) {
closeAllDropdowns();
}
});
}
function closeAllDropdowns() {
const openDropdowns = toolbar.querySelectorAll(".toolbar-dropdown.open");
for (const d of openDropdowns) {
d.classList.remove("open");
}
}
function wireCursorSyncButton() {
const syncBtn = document.getElementById("emb-cursor-sync");
if (syncBtn) {
syncBtn.addEventListener("click", () => {
cursorSyncEnabled = !cursorSyncEnabled;
syncBtn.classList.toggle("active", cursorSyncEnabled);
syncBtn.setAttribute("aria-pressed", String(cursorSyncEnabled));
const onIcon = syncBtn.querySelector(".sync-on-icon");
const offIcon = syncBtn.querySelector(".sync-off-icon");
if (onIcon) onIcon.style.display = cursorSyncEnabled ? "" : "none";
if (offIcon) offIcon.style.display = cursorSyncEnabled ? "none" : "";
emit("toggle:cursorSync", { enabled: cursorSyncEnabled });
});
}
}
function wireThemeToggle() {
const toggleBtn = document.getElementById("emb-theme-toggle");
if (toggleBtn) {
toggleBtn.addEventListener("click", () => {
const current = getState().theme || "light";
const newTheme = current === "light" ? "dark" : "light";
// Send to parent (Phoenix) for persistence
window.parent.postMessage({
type: "MDVIEWR_EVENT",
eventName: "mdviewrThemeToggle",
theme: newTheme
}, "*");
});
}
}
function wirePrintButton() {
const printBtn = document.getElementById("emb-print-btn");
if (printBtn) {
printBtn.addEventListener("click", () => {
window.print();
});
}
}
function wireDoneButton() {
const doneBtn = document.getElementById("emb-done-btn");
if (doneBtn) {
doneBtn.addEventListener("click", () => {
setState({ editMode: false });
});
}
}
function widthToCollapseLevel(width) {
if (width < THRESHOLD_TEXT) return 3;
if (width < THRESHOLD_LISTS) return 2;
if (width < THRESHOLD_BLOCKS) return 1;
return 0;
}
function setupResponsiveToggle() {
function checkWidth() {
const width = toolbar.offsetWidth;
const newLevel = widthToCollapseLevel(width);
if (newLevel !== collapseLevel) {
collapseLevel = newLevel;
renderEditMode(collapseLevel);
resizeObserver.observe(toolbar);
}
}
resizeObserver = new ResizeObserver(() => checkWidth());
resizeObserver.observe(toolbar);
}
function updateFormatState(state) {
if (!toolbar || !getState().editMode) return;
const mappings = [
{ id: "emb-bold", key: "bold" },
{ id: "emb-italic", key: "italic" },
{ id: "emb-strike", key: "strikethrough" },
{ id: "emb-underline", key: "underline" },
{ id: "emb-code", key: "isCode" },
{ id: "emb-link", key: "isLink" },
{ id: "emb-ul", key: "unorderedList" },
{ id: "emb-ol", key: "orderedList" }
];
for (const m of mappings) {
const el = document.getElementById(m.id);
if (el) {
const active = !!state[m.key];
el.classList.toggle("active", active);
el.setAttribute("aria-pressed", String(active));
}
}
// Hide block-level controls when inside a table or list
const hideBlocks = !!state.inTable || !!state.inList;
const blockLevelIds = ["emb-quote", "emb-hr", "emb-table", "emb-codeblock"];
const blockDropdowns = toolbar.querySelectorAll('.toolbar-dropdown[data-group="blocks"]');
for (const id of blockLevelIds) {
const el = document.getElementById(id);
if (el) el.style.display = hideBlocks ? "none" : "";
}
for (const dd of blockDropdowns) {
dd.style.display = hideBlocks ? "none" : "";
}
// Hide list buttons only in tables (they're useful in lists for switching UL/OL)
const listIds = ["emb-ul", "emb-ol", "emb-task"];
const listDropdowns = toolbar.querySelectorAll('.toolbar-dropdown[data-group="lists"]');
for (const id of listIds) {
const el = document.getElementById(id);
if (el) el.style.display = state.inTable ? "none" : "";
}
for (const dd of listDropdowns) {
dd.style.display = state.inTable ? "none" : "";
}
// Disable bold button in headings (headings are already bold)
const boldBtn = document.getElementById("emb-bold");
if (boldBtn) {
boldBtn.style.opacity = state.inHeading ? "0.3" : "";
boldBtn.style.pointerEvents = state.inHeading ? "none" : "";
}
// Hide block type selector in tables and lists
const blockTypeSelect = document.getElementById("emb-block-type");
if (blockTypeSelect) {
blockTypeSelect.style.display = (state.inTable || state.inList) ? "none" : "";
}
if (blockTypeSelect && state.blockType) {
const tagToValue = {
"H1": "<h1>", "H2": "<h2>", "H3": "<h3>", "H4": "<h4>", "H5": "<h5>",
"P": "<p>", "DIV": "<p>"
};
const val = tagToValue[state.blockType] || "<p>";
if (blockTypeSelect.value !== val) {
blockTypeSelect.value = val;
}
}
}