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
@@ -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>
|
||||
Reference in new issue
Block a user