145 lines
5.5 KiB
TypeScript
145 lines
5.5 KiB
TypeScript
import { tr } from '../i18n/index.ts';
|
|
import type { Editor } from "@tiptap/core";
|
|
import type { MenuItem } from "../layout/menuTypes";
|
|
|
|
function selectedText(editor: Editor): string {
|
|
const { from, to } = editor.state.selection;
|
|
return editor.state.doc.textBetween(from, to, "\n");
|
|
}
|
|
|
|
async function writeClipboard(text: string): Promise<boolean> {
|
|
try {
|
|
await navigator.clipboard.writeText(text);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export async function cutSelection(editor: Editor): Promise<void> {
|
|
const text = selectedText(editor);
|
|
if (text && !(await writeClipboard(text))) {
|
|
return;
|
|
}
|
|
editor.chain().focus().deleteSelection().run();
|
|
}
|
|
|
|
export async function copySelection(editor: Editor): Promise<void> {
|
|
const text = selectedText(editor);
|
|
if (text) {
|
|
await writeClipboard(text);
|
|
}
|
|
}
|
|
|
|
export async function pasteClipboard(editor: Editor): Promise<void> {
|
|
let text = "";
|
|
try {
|
|
text = await navigator.clipboard.readText();
|
|
} catch {
|
|
return;
|
|
}
|
|
if (!text) {
|
|
return;
|
|
}
|
|
editor.chain().focus().run();
|
|
editor.view.dispatch(editor.state.tr.insertText(text));
|
|
}
|
|
|
|
export function writerContextItems(editor: Editor): MenuItem[] {
|
|
const inTable = editor.isActive("table");
|
|
return [
|
|
{ kind: "action", label: tr("Cut"), run: () => void cutSelection(editor) },
|
|
{ kind: "action", label: tr("Copy"), run: () => void copySelection(editor) },
|
|
{ kind: "action", label: tr("Paste"), run: () => void pasteClipboard(editor) },
|
|
{ kind: "sep" },
|
|
{
|
|
kind: "group",
|
|
label: tr("Style"),
|
|
items: [
|
|
{ kind: "action", label: tr("Heading {level}", { level: 1 }), run: () => editor.chain().focus().toggleHeading({ level: 1 }).run() },
|
|
{ kind: "action", label: tr("Heading {level}", { level: 2 }), run: () => editor.chain().focus().toggleHeading({ level: 2 }).run() },
|
|
{ kind: "action", label: tr("Heading {level}", { level: 3 }), run: () => editor.chain().focus().toggleHeading({ level: 3 }).run() },
|
|
{ kind: "action", label: tr("Heading {level}", { level: 4 }), run: () => editor.chain().focus().toggleHeading({ level: 4 }).run() },
|
|
{ kind: "action", label: tr("Heading {level}", { level: 5 }), run: () => editor.chain().focus().toggleHeading({ level: 5 }).run() },
|
|
{ kind: "action", label: tr("Heading {level}", { level: 6 }), run: () => editor.chain().focus().toggleHeading({ level: 6 }).run() },
|
|
{ kind: "action", label: tr("Body"), run: () => editor.chain().focus().setParagraph().run() },
|
|
{ kind: "action", label: tr("Quote"), run: () => editor.chain().focus().toggleBlockquote().run() },
|
|
{ kind: "sep" },
|
|
{ kind: "action", label: tr("Bullet list"), run: () => editor.chain().focus().toggleBulletList().run() },
|
|
{ kind: "action", label: tr("Numbered list"), run: () => editor.chain().focus().toggleOrderedList().run() },
|
|
{ kind: "action", label: tr("Task list"), run: () => editor.chain().focus().toggleTaskList().run() },
|
|
],
|
|
},
|
|
{
|
|
kind: "group",
|
|
label: tr("Format"),
|
|
items: [
|
|
{ kind: "action", label: tr("Bold"), run: () => editor.chain().focus().toggleBold().run() },
|
|
{ kind: "action", label: tr("Italic"), run: () => editor.chain().focus().toggleItalic().run() },
|
|
{ kind: "action", label: tr("Strikethrough"), run: () => editor.chain().focus().toggleStrike().run() },
|
|
{ kind: "action", label: tr("Highlight"), run: () => editor.chain().focus().toggleHighlight().run() },
|
|
{ kind: "action", label: tr("Code"), run: () => editor.chain().focus().toggleCode().run() },
|
|
{ kind: "action", label: tr("Math"), run: () => editor.chain().focus().toggleMark("math").run() },
|
|
{ kind: "action", label: tr("Comment"), run: () => editor.chain().focus().toggleMark("comment").run() },
|
|
{ kind: "sep" },
|
|
{ kind: "action", label: tr("Clear formatting"), run: () => editor.chain().focus().unsetAllMarks().run() },
|
|
],
|
|
},
|
|
{
|
|
kind: "group",
|
|
label: tr("Table"),
|
|
items: [
|
|
{
|
|
kind: "action",
|
|
label: tr("Insert table"),
|
|
run: () => editor.chain().focus().insertTable({ rows: 3, cols: 3, withHeaderRow: true }).run(),
|
|
},
|
|
{ kind: "sep" },
|
|
{
|
|
kind: "action",
|
|
label: tr("Add row above"),
|
|
disabled: !inTable,
|
|
run: () => editor.chain().focus().addRowBefore().run(),
|
|
},
|
|
{
|
|
kind: "action",
|
|
label: tr("Add row below"),
|
|
disabled: !inTable,
|
|
run: () => editor.chain().focus().addRowAfter().run(),
|
|
},
|
|
{
|
|
kind: "action",
|
|
label: tr("Delete row"),
|
|
disabled: !inTable,
|
|
run: () => editor.chain().focus().deleteRow().run(),
|
|
},
|
|
{ kind: "sep" },
|
|
{
|
|
kind: "action",
|
|
label: tr("Add column left"),
|
|
disabled: !inTable,
|
|
run: () => editor.chain().focus().addColumnBefore().run(),
|
|
},
|
|
{
|
|
kind: "action",
|
|
label: tr("Add column right"),
|
|
disabled: !inTable,
|
|
run: () => editor.chain().focus().addColumnAfter().run(),
|
|
},
|
|
{
|
|
kind: "action",
|
|
label: tr("Delete column"),
|
|
disabled: !inTable,
|
|
run: () => editor.chain().focus().deleteColumn().run(),
|
|
},
|
|
{
|
|
kind: "action",
|
|
label: tr("Delete table"),
|
|
disabled: !inTable,
|
|
run: () => editor.chain().focus().deleteTable().run(),
|
|
},
|
|
],
|
|
},
|
|
];
|
|
}
|