Let the box editor panel be resized and its code scaled
Drag the panel's left edge (double-click to reset, arrow keys when focused) to change its width; the canvas pads itself by the same width. A−/A+ in the header, or Ctrl/Cmd +/-/0 in the editor, change the code's text size. Both are remembered per browser in localStorage. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
+4
-1
@@ -291,7 +291,10 @@ renders any page's boxes.
|
|||||||
automatically on leaving the box (selecting another, clicking empty canvas,
|
automatically on leaving the box (selecting another, clicking empty canvas,
|
||||||
Done, ✕).
|
Done, ✕).
|
||||||
CodeMirror is loaded only when that panel opens; the panel is teleported to
|
CodeMirror is loaded only when that panel opens; the panel is teleported to
|
||||||
`<body>`.
|
`<body>`. Its width (drag the left edge, double-click to reset) and the
|
||||||
|
code's text size (A−/A+, or Ctrl/Cmd +/−/0 in the editor) are kept per
|
||||||
|
browser in localStorage by `editorPrefs.js`; the canvas pads itself by the
|
||||||
|
same width so it rescales beside the panel.
|
||||||
Image and video files dropped onto the canvas in edit mode are uploaded and
|
Image and video files dropped onto the canvas in edit mode are uploaded and
|
||||||
each becomes an unframed box at the drop point. "Insert upload" stores a
|
each becomes an unframed box at the drop point. "Insert upload" stores a
|
||||||
file through `@/uploads` (see "Uploading files") and puts an `<img>`/`<video>`
|
file through `@/uploads` (see "Uploading files") and puts an `<img>`/`<video>`
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ import ConfirmDialog from "@/components/util/ConfirmDialog.vue";
|
|||||||
import UploadPicker from "@/components/util/UploadPicker.vue";
|
import UploadPicker from "@/components/util/UploadPicker.vue";
|
||||||
import BoxFrame from "./BoxFrame.vue";
|
import BoxFrame from "./BoxFrame.vue";
|
||||||
import BoxEditor from "./BoxEditor.vue";
|
import BoxEditor from "./BoxEditor.vue";
|
||||||
|
import { panelWidth } from "./editorPrefs";
|
||||||
import HtmlBox from "./HtmlBox.vue";
|
import HtmlBox from "./HtmlBox.vue";
|
||||||
import WidgetBox from "./WidgetBox.vue";
|
import WidgetBox from "./WidgetBox.vue";
|
||||||
import { WIDGETS } from "./widgetRegistry";
|
import { WIDGETS } from "./widgetRegistry";
|
||||||
@@ -355,6 +356,7 @@ function toggleEdit() {
|
|||||||
<div
|
<div
|
||||||
class="box-canvas"
|
class="box-canvas"
|
||||||
:class="{ 'with-editor': inEditMode && selected }"
|
:class="{ 'with-editor': inEditMode && selected }"
|
||||||
|
:style="{ '--editor-width': `${panelWidth}px` }"
|
||||||
>
|
>
|
||||||
<div v-if="canEdit" class="admin-bar">
|
<div v-if="canEdit" class="admin-bar">
|
||||||
<button type="button" class="bar-btn" :class="{ on: editing }" @click="toggleEdit">
|
<button type="button" class="bar-btn" :class="{ on: editing }" @click="toggleEdit">
|
||||||
@@ -483,9 +485,10 @@ function toggleEdit() {
|
|||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
}
|
}
|
||||||
/* Leave room for the fixed editor panel so the canvas rescales beside it
|
/* Leave room for the fixed editor panel so the canvas rescales beside it
|
||||||
instead of disappearing under it. */
|
instead of disappearing under it. The width is the admin's chosen one
|
||||||
|
(./editorPrefs), so the canvas follows the panel's edge as it is dragged. */
|
||||||
.box-canvas.with-editor {
|
.box-canvas.with-editor {
|
||||||
padding-right: calc(min(460px, 100vw) + 16px);
|
padding-right: calc(min(var(--editor-width), 100vw) + 16px);
|
||||||
}
|
}
|
||||||
|
|
||||||
.admin-bar {
|
.admin-bar {
|
||||||
|
|||||||
@@ -18,12 +18,16 @@
|
|||||||
* "Choose upload" does the same with a file that is already uploaded.
|
* "Choose upload" does the same with a file that is already uploaded.
|
||||||
*
|
*
|
||||||
* The panel is teleported to <body>, so it is fixed to the window whatever
|
* The panel is teleported to <body>, so it is fixed to the window whatever
|
||||||
* transform or stacking context the page puts around the canvas.
|
* transform or stacking context the page puts around the canvas. Its width
|
||||||
|
* (drag the left edge; double-click it to reset) and the code's text size
|
||||||
|
* (A−/A+, or Ctrl/Cmd with +, - or 0 in the editor) are remembered per
|
||||||
|
* browser through ./editorPrefs.
|
||||||
*/
|
*/
|
||||||
import { computed, onBeforeUnmount, ref, watch } from "vue";
|
import { computed, onBeforeUnmount, ref, watch } from "vue";
|
||||||
import { useBoxesStore } from "@/stores/boxes";
|
import { useBoxesStore } from "@/stores/boxes";
|
||||||
import { ACCEPT, tagFor, uploadFile } from "@/uploads";
|
import { ACCEPT, tagFor, uploadFile } from "@/uploads";
|
||||||
import CodeEditor from "./CodeEditor.vue";
|
import CodeEditor from "./CodeEditor.vue";
|
||||||
|
import { FONT_SIZE, PANEL_WIDTH, fontSize, panelWidth, setPanelWidth, zoom } from "./editorPrefs";
|
||||||
import { WIDGETS } from "./widgetRegistry";
|
import { WIDGETS } from "./widgetRegistry";
|
||||||
import OverlayShell from "@/components/util/OverlayShell.vue";
|
import OverlayShell from "@/components/util/OverlayShell.vue";
|
||||||
import UploadPicker from "@/components/util/UploadPicker.vue";
|
import UploadPicker from "@/components/util/UploadPicker.vue";
|
||||||
@@ -131,16 +135,67 @@ function close() {
|
|||||||
open.value = false;
|
open.value = false;
|
||||||
emit("close");
|
emit("close");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The panel is pinned to the right of the window, so its width is simply the
|
||||||
|
// distance from the pointer to that edge. Pointer capture keeps the drag
|
||||||
|
// alive when the pointer crosses a box's iframe, which would otherwise take
|
||||||
|
// the events.
|
||||||
|
function startResize(event) {
|
||||||
|
if (event.button !== 0) return;
|
||||||
|
event.preventDefault();
|
||||||
|
const handle = event.currentTarget;
|
||||||
|
handle.setPointerCapture(event.pointerId);
|
||||||
|
const move = (e) => setPanelWidth(window.innerWidth - e.clientX);
|
||||||
|
const end = () => {
|
||||||
|
handle.removeEventListener("pointermove", move);
|
||||||
|
handle.removeEventListener("pointerup", end);
|
||||||
|
handle.removeEventListener("pointercancel", end);
|
||||||
|
};
|
||||||
|
handle.addEventListener("pointermove", move);
|
||||||
|
handle.addEventListener("pointerup", end);
|
||||||
|
handle.addEventListener("pointercancel", end);
|
||||||
|
}
|
||||||
|
|
||||||
|
function resizeByKey(event) {
|
||||||
|
const step = event.shiftKey ? 100 : 20;
|
||||||
|
if (event.key === "ArrowLeft") setPanelWidth(panelWidth.value + step);
|
||||||
|
else if (event.key === "ArrowRight") setPanelWidth(panelWidth.value - step);
|
||||||
|
else return;
|
||||||
|
event.preventDefault();
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<OverlayShell v-model="open" title="Box editor" @close="close">
|
<OverlayShell v-model="open" title="Box editor" @close="close">
|
||||||
<aside class="box-editor">
|
<aside class="box-editor" :style="{ '--editor-width': `${panelWidth}px` }">
|
||||||
<header class="flex items-center justify-between gap-2">
|
<header class="flex items-center justify-between gap-2">
|
||||||
<h3 class="m-0">
|
<h3 class="m-0">
|
||||||
{{ box.kind === "widget" ? `Widget · ${box.widget}` : "HTML box" }}
|
{{ box.kind === "widget" ? `Widget · ${box.widget}` : "HTML box" }}
|
||||||
</h3>
|
</h3>
|
||||||
<button type="button" class="close" title="Close" @click="close">✕</button>
|
<div class="flex items-center gap-2">
|
||||||
|
<div v-if="box.kind === 'html'" class="zoom" role="group" aria-label="Code text size">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
title="Smaller code (Ctrl/Cmd -)"
|
||||||
|
:disabled="fontSize <= FONT_SIZE.min"
|
||||||
|
@click="zoom(-1)"
|
||||||
|
>
|
||||||
|
A−
|
||||||
|
</button>
|
||||||
|
<button type="button" title="Reset code size (Ctrl/Cmd 0)" @click="zoom(0)">
|
||||||
|
{{ fontSize }}px
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
title="Larger code (Ctrl/Cmd +)"
|
||||||
|
:disabled="fontSize >= FONT_SIZE.max"
|
||||||
|
@click="zoom(1)"
|
||||||
|
>
|
||||||
|
A+
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<button type="button" class="close" title="Close" @click="close">✕</button>
|
||||||
|
</div>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<label class="field">
|
<label class="field">
|
||||||
@@ -192,7 +247,14 @@ function close() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<template v-if="box.kind === 'html'">
|
<template v-if="box.kind === 'html'">
|
||||||
<CodeEditor ref="codeEditor" v-model="draftHtml" class="flex-1" @save="saveHtml" />
|
<CodeEditor
|
||||||
|
ref="codeEditor"
|
||||||
|
v-model="draftHtml"
|
||||||
|
:font-size="fontSize"
|
||||||
|
class="flex-1"
|
||||||
|
@save="saveHtml"
|
||||||
|
@zoom="zoom"
|
||||||
|
/>
|
||||||
<footer class="flex flex-wrap items-center justify-between gap-2">
|
<footer class="flex flex-wrap items-center justify-between gap-2">
|
||||||
<small class="text-muted">
|
<small class="text-muted">
|
||||||
Runs sandboxed: scripts work, cookies and /api don't. fetch() only reaches
|
Runs sandboxed: scripts work, cookies and /api don't. fetch() only reaches
|
||||||
@@ -227,6 +289,21 @@ function close() {
|
|||||||
</button>
|
</button>
|
||||||
</footer>
|
</footer>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
<!-- Last in the panel so the opening focus lands on a header button, not here. -->
|
||||||
|
<div
|
||||||
|
class="resize-handle"
|
||||||
|
role="separator"
|
||||||
|
aria-orientation="vertical"
|
||||||
|
aria-label="Resize editor"
|
||||||
|
:aria-valuenow="panelWidth"
|
||||||
|
:aria-valuemin="PANEL_WIDTH.min"
|
||||||
|
tabindex="0"
|
||||||
|
title="Drag to resize · double-click to reset"
|
||||||
|
@pointerdown="startResize"
|
||||||
|
@dblclick="setPanelWidth(PANEL_WIDTH.default)"
|
||||||
|
@keydown="resizeByKey"
|
||||||
|
/>
|
||||||
</aside>
|
</aside>
|
||||||
</OverlayShell>
|
</OverlayShell>
|
||||||
<UploadPicker
|
<UploadPicker
|
||||||
@@ -243,7 +320,7 @@ function close() {
|
|||||||
top: 0;
|
top: 0;
|
||||||
right: 0;
|
right: 0;
|
||||||
bottom: 0;
|
bottom: 0;
|
||||||
width: min(460px, 100vw);
|
width: min(var(--editor-width), 100vw);
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
gap: 0.6rem;
|
gap: 0.6rem;
|
||||||
@@ -254,6 +331,45 @@ function close() {
|
|||||||
color: var(--color-secondary);
|
color: var(--color-secondary);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.resize-handle {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
bottom: 0;
|
||||||
|
left: -5px;
|
||||||
|
width: 9px;
|
||||||
|
cursor: ew-resize;
|
||||||
|
touch-action: none;
|
||||||
|
}
|
||||||
|
.resize-handle:hover,
|
||||||
|
.resize-handle:focus-visible,
|
||||||
|
.resize-handle:active {
|
||||||
|
background: linear-gradient(to right, transparent 3px, var(--color-primary) 3px 6px, transparent 6px);
|
||||||
|
outline: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.zoom {
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
.zoom button {
|
||||||
|
min-width: 2rem;
|
||||||
|
padding: 0 0.4rem;
|
||||||
|
color: var(--color-primary);
|
||||||
|
background: var(--color-link-bg);
|
||||||
|
border: 1px solid var(--color-quaternary);
|
||||||
|
font-size: 0.85rem;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
.zoom button + button {
|
||||||
|
border-left: none;
|
||||||
|
}
|
||||||
|
.zoom button:hover:not(:disabled) {
|
||||||
|
border-color: var(--color-primary);
|
||||||
|
}
|
||||||
|
.zoom button:disabled {
|
||||||
|
opacity: 0.5;
|
||||||
|
cursor: default;
|
||||||
|
}
|
||||||
|
|
||||||
.close {
|
.close {
|
||||||
color: var(--color-primary);
|
color: var(--color-primary);
|
||||||
background: var(--color-link-bg);
|
background: var(--color-link-bg);
|
||||||
|
|||||||
@@ -9,13 +9,18 @@
|
|||||||
* The document is owned by CodeMirror; `modelValue` is pushed in only when it
|
* The document is owned by CodeMirror; `modelValue` is pushed in only when it
|
||||||
* differs from what the editor already holds, otherwise every keystroke would
|
* differs from what the editor already holds, otherwise every keystroke would
|
||||||
* round-trip through the parent and reset the cursor.
|
* round-trip through the parent and reset the cursor.
|
||||||
|
*
|
||||||
|
* `fontSize` scales the code. Ctrl/Cmd with +, - or 0 inside the editor asks
|
||||||
|
* the parent to change it (`zoom` with +1, -1 or 0 for reset) instead of
|
||||||
|
* zooming the whole page.
|
||||||
*/
|
*/
|
||||||
import { onBeforeUnmount, onMounted, ref, watch } from "vue";
|
import { nextTick, onBeforeUnmount, onMounted, ref, watch } from "vue";
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
modelValue: { type: String, default: "" },
|
modelValue: { type: String, default: "" },
|
||||||
|
fontSize: { type: Number, default: 13 },
|
||||||
});
|
});
|
||||||
const emit = defineEmits(["update:modelValue", "save"]);
|
const emit = defineEmits(["update:modelValue", "save", "zoom"]);
|
||||||
|
|
||||||
const host = ref(null);
|
const host = ref(null);
|
||||||
const failed = ref(false);
|
const failed = ref(false);
|
||||||
@@ -34,7 +39,7 @@ onMounted(async () => {
|
|||||||
|
|
||||||
const theme = EditorView.theme(
|
const theme = EditorView.theme(
|
||||||
{
|
{
|
||||||
"&": { height: "100%", fontSize: "13px", backgroundColor: "var(--color-surface-deep)", color: "var(--color-muted)" },
|
"&": { height: "100%", fontSize: "var(--code-font-size)", backgroundColor: "var(--color-surface-deep)", color: "var(--color-muted)" },
|
||||||
".cm-content": { caretColor: "var(--color-primary)" },
|
".cm-content": { caretColor: "var(--color-primary)" },
|
||||||
".cm-gutters": { backgroundColor: "var(--color-surface)", color: "var(--color-quaternary)", border: "none" },
|
".cm-gutters": { backgroundColor: "var(--color-surface)", color: "var(--color-quaternary)", border: "none" },
|
||||||
// The selection is drawn in a layer *behind* the text, so an opaque
|
// The selection is drawn in a layer *behind* the text, so an opaque
|
||||||
@@ -58,7 +63,13 @@ onMounted(async () => {
|
|||||||
doc: props.modelValue,
|
doc: props.modelValue,
|
||||||
extensions: [
|
extensions: [
|
||||||
// Before basicSetup so Mod-s wins over the browser's "save page".
|
// Before basicSetup so Mod-s wins over the browser's "save page".
|
||||||
keymap.of([{ key: "Mod-s", preventDefault: true, run: () => (emit("save"), true) }]),
|
keymap.of([
|
||||||
|
{ key: "Mod-s", preventDefault: true, run: () => (emit("save"), true) },
|
||||||
|
{ key: "Mod-=", preventDefault: true, run: () => (emit("zoom", 1), true) },
|
||||||
|
{ key: "Mod-+", preventDefault: true, run: () => (emit("zoom", 1), true) },
|
||||||
|
{ key: "Mod--", preventDefault: true, run: () => (emit("zoom", -1), true) },
|
||||||
|
{ key: "Mod-0", preventDefault: true, run: () => (emit("zoom", 0), true) },
|
||||||
|
]),
|
||||||
basicSetup,
|
basicSetup,
|
||||||
html(),
|
html(),
|
||||||
theme,
|
theme,
|
||||||
@@ -79,6 +90,14 @@ watch(
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// The size comes in through a CSS variable, which CodeMirror can't see change:
|
||||||
|
// have it re-measure line heights once the new size is applied, or clicks and
|
||||||
|
// the cursor land on the wrong line.
|
||||||
|
watch(
|
||||||
|
() => props.fontSize,
|
||||||
|
() => nextTick(() => view?.requestMeasure()),
|
||||||
|
);
|
||||||
|
|
||||||
onBeforeUnmount(() => view?.destroy());
|
onBeforeUnmount(() => view?.destroy());
|
||||||
|
|
||||||
/** Put text at the cursor (replacing any selection); false if the editor
|
/** Put text at the cursor (replacing any selection); false if the editor
|
||||||
@@ -94,7 +113,7 @@ defineExpose({ insert });
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div ref="host" class="code-editor">
|
<div ref="host" class="code-editor" :style="{ '--code-font-size': `${fontSize}px` }">
|
||||||
<p v-if="failed" class="p-2 text-tertiary">Couldn't load the editor.</p>
|
<p v-if="failed" class="p-2 text-tertiary">Couldn't load the editor.</p>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
/**
|
||||||
|
* The box editor panel's size and its code's text size, shared between
|
||||||
|
* BoxEditor (which draws the panel and its controls) and BoxCanvas (which
|
||||||
|
* pads itself by the panel's width so the canvas rescales beside it rather
|
||||||
|
* than under it). They are one admin's preference for their own screen, so
|
||||||
|
* they live in localStorage, not the database.
|
||||||
|
*/
|
||||||
|
import { ref, watch } from "vue";
|
||||||
|
|
||||||
|
const KEY = "boxEditor";
|
||||||
|
|
||||||
|
export const PANEL_WIDTH = { min: 320, default: 460 };
|
||||||
|
export const FONT_SIZE = { min: 9, max: 24, default: 13 };
|
||||||
|
|
||||||
|
function load() {
|
||||||
|
try {
|
||||||
|
return JSON.parse(localStorage.getItem(KEY)) ?? {};
|
||||||
|
} catch {
|
||||||
|
return {}; // storage unavailable or corrupted: keep the defaults
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const saved = load();
|
||||||
|
const clamp = (n, min, max) => Math.min(max, Math.max(min, n));
|
||||||
|
const valid = (n) => typeof n === "number" && Number.isFinite(n);
|
||||||
|
|
||||||
|
// Not clamped to the window here: the panel's CSS caps it with min(…, 100vw),
|
||||||
|
// so a width chosen on a wide monitor survives a visit from a narrow one.
|
||||||
|
export const panelWidth = ref(valid(saved.width) ? Math.max(PANEL_WIDTH.min, saved.width) : PANEL_WIDTH.default);
|
||||||
|
export const fontSize = ref(valid(saved.fontSize) ? clamp(saved.fontSize, FONT_SIZE.min, FONT_SIZE.max) : FONT_SIZE.default);
|
||||||
|
|
||||||
|
export function setPanelWidth(width) {
|
||||||
|
panelWidth.value = Math.round(clamp(width, PANEL_WIDTH.min, window.innerWidth));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Step the code's text size by `delta` px; 0 resets it. */
|
||||||
|
export function zoom(delta) {
|
||||||
|
fontSize.value = delta === 0 ? FONT_SIZE.default : clamp(fontSize.value + delta, FONT_SIZE.min, FONT_SIZE.max);
|
||||||
|
}
|
||||||
|
|
||||||
|
watch([panelWidth, fontSize], ([width, size]) => {
|
||||||
|
try {
|
||||||
|
localStorage.setItem(KEY, JSON.stringify({ width, fontSize: size }));
|
||||||
|
} catch {
|
||||||
|
/* storage unavailable: the preference just won't outlive the tab */
|
||||||
|
}
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user