Add the editor: toolbar, photos in the text, a server preview and a focus point
This commit is contained in:
1 parent
6b41bfddd3
commit
6a51b0e649
18 files changed
+224
-17
No files matched your search
@@ -2,6 +2,7 @@
|
||||
import { ref } from 'vue';
|
||||
import { boot } from '../api';
|
||||
import { t } from '../i18n';
|
||||
import MarkdownEditor from './MarkdownEditor.vue';
|
||||
import MediaPicker from './MediaPicker.vue';
|
||||
|
||||
/** One field as the server describes it (src/Support/Fields.php). */
|
||||
@@ -35,15 +36,15 @@ const canPick = boot.permissions.includes('media.view');
|
||||
<option v-if="!field.required" value="" />
|
||||
<option v-for="(label, value) in field.options" :key="value" :value="value">{{ label }}</option>
|
||||
</select>
|
||||
<MarkdownEditor v-else-if="field.type === 'markdown'" :id="'f-' + field.key" v-model="values[field.key]" :required="field.required" :maxlength="field.max ?? undefined" />
|
||||
<textarea
|
||||
v-else-if="field.type === 'textarea' || field.type === 'lines' || field.type === 'markdown'"
|
||||
v-else-if="field.type === 'textarea' || field.type === 'lines'"
|
||||
:id="'f-' + field.key"
|
||||
v-model="values[field.key]"
|
||||
:rows="field.type === 'markdown' ? 12 : field.type === 'lines' ? 5 : 4"
|
||||
:rows="field.type === 'lines' ? 5 : 4"
|
||||
:maxlength="field.max ?? undefined"
|
||||
:required="field.required"
|
||||
class="m-input"
|
||||
:class="{ 'font-mono': field.type === 'markdown' }"
|
||||
/>
|
||||
<div v-else-if="field.type === 'media'" class="flex gap-2">
|
||||
<input :id="'f-' + field.key" v-model="values[field.key]" maxlength="2048" class="m-input" />
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue';
|
||||
import { api, boot } from '../api';
|
||||
import { t } from '../i18n';
|
||||
import MediaPicker from './MediaPicker.vue';
|
||||
|
||||
// A Markdown text with a toolbar, photos from the media library and a preview that is rendered by the server, like the site does it.
|
||||
const props = defineProps<{ modelValue: string; id?: string; rows?: number; required?: boolean; maxlength?: number }>();
|
||||
const emit = defineEmits<{ 'update:modelValue': [value: string] }>();
|
||||
|
||||
const area = ref<HTMLTextAreaElement | null>(null);
|
||||
const tab = ref<'write' | 'preview'>('write');
|
||||
const html = ref('');
|
||||
const loading = ref(false);
|
||||
const picking = ref(false);
|
||||
const canPick = boot.permissions.includes('media.view');
|
||||
|
||||
/** Puts `before` and `after` around the selection (or the placeholder when nothing is selected). */
|
||||
function wrap(before: string, after = before, placeholder = ''): void {
|
||||
const el = area.value!;
|
||||
const start = el.selectionStart;
|
||||
const end = el.selectionEnd;
|
||||
const chosen = props.modelValue.slice(start, end) || placeholder;
|
||||
const next = props.modelValue.slice(0, start) + before + chosen + after + props.modelValue.slice(end);
|
||||
|
||||
emit('update:modelValue', next);
|
||||
requestAnimationFrame(() => {
|
||||
el.focus();
|
||||
el.setSelectionRange(start + before.length, start + before.length + chosen.length);
|
||||
});
|
||||
}
|
||||
|
||||
/** Puts a mark at the start of every selected line. */
|
||||
function lines(mark: string): void {
|
||||
const el = area.value!;
|
||||
const start = props.modelValue.lastIndexOf('\n', el.selectionStart - 1) + 1;
|
||||
const end = el.selectionEnd;
|
||||
const block = props.modelValue.slice(start, end).split('\n').map((line) => mark + line).join('\n');
|
||||
|
||||
emit('update:modelValue', props.modelValue.slice(0, start) + block + props.modelValue.slice(end));
|
||||
requestAnimationFrame(() => el.focus());
|
||||
}
|
||||
|
||||
function insertImage(url: string, item?: { alt?: string | null; name?: string }): void {
|
||||
picking.value = false;
|
||||
const alt = (item?.alt || item?.name || '').replace(/[[\]]/g, '');
|
||||
|
||||
const el = area.value!;
|
||||
const at = el.selectionStart;
|
||||
const markup = ``;
|
||||
|
||||
emit('update:modelValue', props.modelValue.slice(0, at) + markup + props.modelValue.slice(el.selectionEnd));
|
||||
requestAnimationFrame(() => {
|
||||
el.focus();
|
||||
el.setSelectionRange(at + markup.length, at + markup.length);
|
||||
});
|
||||
}
|
||||
|
||||
async function show(next: 'write' | 'preview'): Promise<void> {
|
||||
tab.value = next;
|
||||
|
||||
if (next === 'preview') {
|
||||
loading.value = true;
|
||||
try {
|
||||
html.value = (await api.post(`${boot.base}/markdown`, { text: props.modelValue })).html;
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<div class="mb-2 flex flex-wrap items-center gap-1" role="toolbar" :aria-label="t('Formatting')">
|
||||
<button type="button" class="m-btn m-btn-secondary !px-2 !py-1 font-bold" :disabled="tab === 'preview'" :title="t('Bold')" @click="wrap('**', '**', t('bold'))">B</button>
|
||||
<button type="button" class="m-btn m-btn-secondary !px-2 !py-1 italic" :disabled="tab === 'preview'" :title="t('Italic')" @click="wrap('*', '*', t('italic'))">I</button>
|
||||
<button type="button" class="m-btn m-btn-secondary !px-2 !py-1" :disabled="tab === 'preview'" :title="t('Heading')" @click="lines('## ')">H</button>
|
||||
<button type="button" class="m-btn m-btn-secondary !px-2 !py-1" :disabled="tab === 'preview'" :title="t('List')" @click="lines('- ')">•</button>
|
||||
<button type="button" class="m-btn m-btn-secondary !px-2 !py-1" :disabled="tab === 'preview'" :title="t('Quote')" @click="lines('> ')">”</button>
|
||||
<button type="button" class="m-btn m-btn-secondary !px-2 !py-1" :disabled="tab === 'preview'" :title="t('Link')" @click="wrap('[', '](https://)', t('text'))">🔗</button>
|
||||
<button v-if="canPick" type="button" class="m-btn m-btn-secondary !px-2 !py-1" :disabled="tab === 'preview'" @click="picking = true">{{ t('Photo') }}</button>
|
||||
<span class="ml-auto flex gap-1">
|
||||
<button type="button" class="m-btn !px-3 !py-1" :class="tab === 'write' ? 'm-btn-primary' : 'm-btn-secondary'" @click="show('write')">{{ t('Write') }}</button>
|
||||
<button type="button" class="m-btn !px-3 !py-1" :class="tab === 'preview' ? 'm-btn-primary' : 'm-btn-secondary'" @click="show('preview')">{{ t('Preview') }}</button>
|
||||
</span>
|
||||
</div>
|
||||
<textarea
|
||||
v-show="tab === 'write'"
|
||||
:id="id"
|
||||
ref="area"
|
||||
:value="modelValue"
|
||||
:rows="rows ?? 14"
|
||||
:required="required"
|
||||
:maxlength="maxlength"
|
||||
class="m-input font-mono"
|
||||
@input="emit('update:modelValue', ($event.target as HTMLTextAreaElement).value)"
|
||||
/>
|
||||
<div v-if="tab === 'preview'" class="m-input min-h-40 overflow-auto">
|
||||
<p v-if="loading" class="text-sm text-muted">{{ t('Loading…') }}</p>
|
||||
<div v-else class="m-prose" v-html="html" />
|
||||
</div>
|
||||
<MediaPicker v-if="picking" @pick="insertImage" @close="picking = false" />
|
||||
</div>
|
||||
</template>
|
||||
@@ -3,8 +3,8 @@ import { onMounted, ref } from 'vue';
|
||||
import { api, boot } from '../api';
|
||||
import { t } from '../i18n';
|
||||
|
||||
const emit = defineEmits<{ pick: [url: string]; close: [] }>();
|
||||
const items = ref<{ id: number; name: string; url: string; is_image: boolean }[]>([]);
|
||||
const emit = defineEmits<{ pick: [url: string, item: { id: number; name: string; alt: string | null }]; close: [] }>();
|
||||
const items = ref<{ id: number; name: string; alt: string | null; url: string; is_image: boolean }[]>([]);
|
||||
const loading = ref(true);
|
||||
|
||||
onMounted(async () => {
|
||||
@@ -26,7 +26,7 @@ onMounted(async () => {
|
||||
<p v-if="loading" class="text-sm text-muted">{{ t('Loading…') }}</p>
|
||||
<p v-else-if="!items.length" class="text-sm text-muted">{{ t('No images yet.') }}</p>
|
||||
<div v-else class="grid grid-cols-2 gap-3 sm:grid-cols-3 md:grid-cols-4">
|
||||
<button v-for="item in items" :key="item.id" type="button" class="overflow-hidden rounded-lg border border-line text-left hover:border-brand" @click="emit('pick', item.url)">
|
||||
<button v-for="item in items" :key="item.id" type="button" class="overflow-hidden rounded-lg border border-line text-left hover:border-brand" @click="emit('pick', item.url, item)">
|
||||
<img :src="item.url" :alt="item.name" loading="lazy" class="aspect-square w-full object-cover" />
|
||||
<span class="block truncate px-2 py-1 text-xs">{{ item.name }}</span>
|
||||
</button>
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ const nl: Record<string, string> = {
|
||||
'Put online': 'Online zetten', 'Take offline': 'Offline halen', 'Draft': 'Concept', 'Scheduled': 'Ingepland', 'Published': 'Gepubliceerd',
|
||||
'Are you sure?': 'Weet je het zeker?', 'No posts yet.': 'Nog geen berichten.', 'Address (slug)': 'Adres (slug)', 'Summary': 'Samenvatting',
|
||||
'Text (Markdown)': 'Tekst (Markdown)', 'Cover image': 'Omslagafbeelding', 'Tags (comma separated)': 'Tags (met komma\'s)',
|
||||
'Featured': 'Uitgelicht', 'Publish': 'Publiceren', 'Publish at': 'Publiceren op', 'Menu': 'Menu', 'History': 'Geschiedenis', 'Now': 'Nu', 'Put back': 'Terugzetten', 'Put this version back? The text now is kept in the history.': 'Deze versie terugzetten? De tekst van nu blijft bewaard in de geschiedenis.', 'The earlier version is back.': 'De eerdere versie is terug.', 'New': 'Nieuw', 'New page': 'Nieuwe pagina', 'Description for search engines': 'Omschrijving voor zoekmachines', 'One per line.': 'Eén per regel.', 'Show everything': 'Alles tonen', 'Simple view': 'Simpele weergave', 'Loading…': 'Laden…',
|
||||
'Featured': 'Uitgelicht', 'Publish': 'Publiceren', 'Publish at': 'Publiceren op', 'Menu': 'Menu', 'Click the part of the picture that must stay in view.': 'Klik op het deel van de foto dat in beeld moet blijven.', 'Focus point': 'Scherpstelpunt', 'Clear': 'Wissen', 'Formatting': 'Opmaak', 'Bold': 'Vet', 'Italic': 'Cursief', 'Heading': 'Kop', 'List': 'Lijst', 'Quote': 'Citaat', 'Link': 'Link', 'bold': 'vet', 'italic': 'cursief', 'text': 'tekst', 'Photo': 'Foto', 'Write': 'Schrijven', 'Preview': 'Voorbeeld', 'History': 'Geschiedenis', 'Now': 'Nu', 'Put back': 'Terugzetten', 'Put this version back? The text now is kept in the history.': 'Deze versie terugzetten? De tekst van nu blijft bewaard in de geschiedenis.', 'The earlier version is back.': 'De eerdere versie is terug.', 'New': 'Nieuw', 'New page': 'Nieuwe pagina', 'Description for search engines': 'Omschrijving voor zoekmachines', 'One per line.': 'Eén per regel.', 'Show everything': 'Alles tonen', 'Simple view': 'Simpele weergave', 'Loading…': 'Laden…',
|
||||
'You may not open this page.': 'Je mag deze pagina niet openen.', 'Something went wrong. Try again.': 'Er ging iets mis. Probeer het opnieuw.',
|
||||
'This page has no screen in the manager yet.': 'Deze pagina heeft nog geen scherm in de manager.', 'Go to the dashboard': 'Naar het dashboard',
|
||||
'The page expired. Reload it and try again.': 'De pagina is verlopen. Laad hem opnieuw en probeer het nog eens.', 'Back': 'Terug',
|
||||
|
||||
@@ -6,7 +6,7 @@ import { flash, reload } from '../state';
|
||||
|
||||
interface Item {
|
||||
id: number; name: string; filename: string; url: string; mime_type: string; is_image: boolean; size: number;
|
||||
width: number | null; height: number | null; alt: string | null; uploaded_by: string | null; usage_count: number;
|
||||
width: number | null; height: number | null; alt: string | null; focus_x: number | null; focus_y: number | null; uploaded_by: string | null; usage_count: number;
|
||||
}
|
||||
|
||||
defineProps<{
|
||||
@@ -17,7 +17,7 @@ defineProps<{
|
||||
const can = (permission: string) => boot.permissions.includes(permission);
|
||||
const busy = ref(false);
|
||||
const selected = ref<Item | null>(null);
|
||||
const form = useForm({ name: '', alt: '' });
|
||||
const form = useForm({ name: '', alt: '', focus_x: null as number | null, focus_y: null as number | null });
|
||||
|
||||
const size = (bytes: number) => (bytes > 1e6 ? `${(bytes / 1e6).toFixed(1)} MB` : `${Math.max(1, Math.round(bytes / 1e3))} kB`);
|
||||
|
||||
@@ -26,8 +26,23 @@ function choose(item: Item): void {
|
||||
form.errors = {};
|
||||
form.values.name = item.name;
|
||||
form.values.alt = item.alt ?? '';
|
||||
form.values.focus_x = item.focus_x;
|
||||
form.values.focus_y = item.focus_y;
|
||||
}
|
||||
|
||||
/** The point of the picture that must stay in view when the site crops it: set with a click. */
|
||||
function setFocus(event: MouseEvent): void {
|
||||
if (!can('media.edit')) {
|
||||
return;
|
||||
}
|
||||
|
||||
const box = (event.currentTarget as HTMLElement).getBoundingClientRect();
|
||||
form.values.focus_x = Math.round(Math.min(1, Math.max(0, (event.clientX - box.left) / box.width)) * 1000) / 1000;
|
||||
form.values.focus_y = Math.round(Math.min(1, Math.max(0, (event.clientY - box.top) / box.height)) * 1000) / 1000;
|
||||
}
|
||||
|
||||
const position = (x: number | null, y: number | null) => `${(x ?? 0.5) * 100}% ${(y ?? 0.5) * 100}%`;
|
||||
|
||||
async function send(event: Event): Promise<void> {
|
||||
const input = event.target as HTMLInputElement;
|
||||
busy.value = true;
|
||||
@@ -84,7 +99,21 @@ async function remove(item: Item): Promise<void> {
|
||||
</div>
|
||||
|
||||
<form v-if="selected" class="m-card mb-6 flex max-w-3xl flex-wrap gap-5 p-5" @submit.prevent="save">
|
||||
<img v-if="selected.is_image" :src="selected.url" :alt="selected.alt ?? ''" class="h-32 w-32 rounded-lg object-cover" />
|
||||
<div v-if="selected.is_image" class="w-full shrink-0 sm:w-64">
|
||||
<div class="relative" :class="can('media.edit') ? 'cursor-crosshair' : ''" @click="setFocus">
|
||||
<img :src="selected.url" :alt="selected.alt ?? ''" class="w-full rounded-lg" draggable="false" />
|
||||
<span
|
||||
v-if="form.values.focus_x !== null"
|
||||
class="pointer-events-none absolute h-5 w-5 -translate-x-1/2 -translate-y-1/2 rounded-full border-2 border-white bg-brand/60 shadow"
|
||||
:style="{ left: (form.values.focus_x ?? 0) * 100 + '%', top: (form.values.focus_y ?? 0) * 100 + '%' }"
|
||||
/>
|
||||
</div>
|
||||
<p class="m-help">{{ can('media.edit') ? t('Click the part of the picture that must stay in view.') : t('Focus point') }}</p>
|
||||
<div class="mt-2 flex items-center gap-2">
|
||||
<img :src="selected.url" alt="" class="h-16 w-16 rounded-lg object-cover" :style="{ objectPosition: position(form.values.focus_x, form.values.focus_y) }" />
|
||||
<button v-if="can('media.edit') && form.values.focus_x !== null" type="button" class="m-btn m-btn-secondary" @click="form.values.focus_x = form.values.focus_y = null">{{ t('Clear') }}</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="min-w-0 flex-1 space-y-3">
|
||||
<div>
|
||||
<label class="m-label" for="m-name">{{ t('Name') }}</label>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { api, boot, useForm } from '../api';
|
||||
import FormFields, { type Field } from '../components/FormFields.vue';
|
||||
import MarkdownEditor from '../components/MarkdownEditor.vue';
|
||||
import Link from '../components/Link.vue';
|
||||
import RevisionsPanel from '../components/RevisionsPanel.vue';
|
||||
import { t } from '../i18n';
|
||||
@@ -56,7 +57,7 @@ async function remove(): Promise<void> {
|
||||
<template v-else>
|
||||
<div>
|
||||
<label for="body" class="m-label">{{ t('Text (Markdown)') }}</label>
|
||||
<textarea id="body" v-model="form.values.body" rows="14" class="m-input font-mono" />
|
||||
<MarkdownEditor id="body" v-model="form.values.body" />
|
||||
<p v-if="form.errors.body" class="m-error">{{ form.errors.body }}</p>
|
||||
</div>
|
||||
<label class="flex items-center gap-2 text-sm"><input v-model="form.values.is_published" type="checkbox" /> {{ t('Publish') }}</label>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import { boot, useForm } from '../api';
|
||||
import { ref } from 'vue';
|
||||
import Link from '../components/Link.vue';
|
||||
import MarkdownEditor from '../components/MarkdownEditor.vue';
|
||||
import MediaPicker from '../components/MediaPicker.vue';
|
||||
import RevisionsPanel from '../components/RevisionsPanel.vue';
|
||||
import { t } from '../i18n';
|
||||
@@ -62,7 +63,7 @@ async function submit(): Promise<void> {
|
||||
</div>
|
||||
<div>
|
||||
<label for="body" class="m-label">{{ t('Text (Markdown)') }}</label>
|
||||
<textarea id="body" v-model="form.values.body" rows="14" required class="m-input font-mono" />
|
||||
<MarkdownEditor id="body" v-model="form.values.body" required />
|
||||
<p v-if="form.errors.body" class="m-error">{{ form.errors.body }}</p>
|
||||
</div>
|
||||
<div class="grid gap-5 sm:grid-cols-2">
|
||||
|
||||
@@ -29,6 +29,14 @@ html, body { background: var(--m-ink); color: var(--m-text); font-family: var(--
|
||||
.m-input { width: 100%; border-radius: var(--m-radius, .5rem); border: 1px solid var(--m-line); background: var(--m-ink); color: var(--m-text); padding: .5rem .7rem; font-size: .9rem; }
|
||||
.m-input:focus { outline: 2px solid var(--m-brand); outline-offset: 1px; }
|
||||
.m-error { margin-top: .25rem; font-size: .8rem; color: #ef4444; }
|
||||
.m-prose { line-height: 1.6; overflow-wrap: anywhere; }
|
||||
.m-prose > * + * { margin-top: .75rem; }
|
||||
.m-prose h1, .m-prose h2, .m-prose h3 { font-family: var(--m-font-display, inherit); font-weight: 700; line-height: 1.25; }
|
||||
.m-prose h1 { font-size: 1.5rem; } .m-prose h2 { font-size: 1.25rem; } .m-prose h3 { font-size: 1.1rem; }
|
||||
.m-prose ul { list-style: disc; padding-left: 1.25rem; } .m-prose ol { list-style: decimal; padding-left: 1.25rem; }
|
||||
.m-prose blockquote { border-left: 3px solid var(--m-line); padding-left: .75rem; color: var(--m-muted); }
|
||||
.m-prose a { text-decoration: underline; } .m-prose img { max-width: 100%; height: auto; border-radius: .5rem; }
|
||||
.m-prose code { font-family: ui-monospace, monospace; font-size: .9em; }
|
||||
.m-help { margin-top: .25rem; font-size: .75rem; color: var(--m-muted); }
|
||||
.m-badge { display: inline-block; border-radius: 999px; padding: .1rem .55rem; font-size: .7rem; font-weight: 600; background: var(--m-panel-2); border: 1px solid var(--m-line); }
|
||||
}
|
||||
Reference in new issue
Block a user