Initial RuneStone project
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
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: "Cut", run: () => void cutSelection(editor) },
|
||||
{ kind: "action", label: "Copy", run: () => void copySelection(editor) },
|
||||
{ kind: "action", label: "Paste", run: () => void pasteClipboard(editor) },
|
||||
{ kind: "sep" },
|
||||
{
|
||||
kind: "group",
|
||||
label: "Style",
|
||||
items: [
|
||||
{ kind: "action", label: "Heading 1", run: () => editor.chain().focus().toggleHeading({ level: 1 }).run() },
|
||||
{ kind: "action", label: "Heading 2", run: () => editor.chain().focus().toggleHeading({ level: 2 }).run() },
|
||||
{ kind: "action", label: "Heading 3", run: () => editor.chain().focus().toggleHeading({ level: 3 }).run() },
|
||||
{ kind: "action", label: "Heading 4", run: () => editor.chain().focus().toggleHeading({ level: 4 }).run() },
|
||||
{ kind: "action", label: "Heading 5", run: () => editor.chain().focus().toggleHeading({ level: 5 }).run() },
|
||||
{ kind: "action", label: "Heading 6", run: () => editor.chain().focus().toggleHeading({ level: 6 }).run() },
|
||||
{ kind: "action", label: "Body", run: () => editor.chain().focus().setParagraph().run() },
|
||||
{ kind: "action", label: "Quote", run: () => editor.chain().focus().toggleBlockquote().run() },
|
||||
{ kind: "sep" },
|
||||
{ kind: "action", label: "Bullet list", run: () => editor.chain().focus().toggleBulletList().run() },
|
||||
{ kind: "action", label: "Numbered list", run: () => editor.chain().focus().toggleOrderedList().run() },
|
||||
{ kind: "action", label: "Task list", run: () => editor.chain().focus().toggleTaskList().run() },
|
||||
],
|
||||
},
|
||||
{
|
||||
kind: "group",
|
||||
label: "Format",
|
||||
items: [
|
||||
{ kind: "action", label: "Bold", run: () => editor.chain().focus().toggleBold().run() },
|
||||
{ kind: "action", label: "Italic", run: () => editor.chain().focus().toggleItalic().run() },
|
||||
{ kind: "action", label: "Strikethrough", run: () => editor.chain().focus().toggleStrike().run() },
|
||||
{ kind: "action", label: "Highlight", run: () => editor.chain().focus().toggleHighlight().run() },
|
||||
{ kind: "action", label: "Code", run: () => editor.chain().focus().toggleCode().run() },
|
||||
{ kind: "action", label: "Math", run: () => editor.chain().focus().toggleMark("math").run() },
|
||||
{ kind: "action", label: "Comment", run: () => editor.chain().focus().toggleMark("comment").run() },
|
||||
{ kind: "sep" },
|
||||
{ kind: "action", label: "Clear formatting", run: () => editor.chain().focus().unsetAllMarks().run() },
|
||||
],
|
||||
},
|
||||
{
|
||||
kind: "group",
|
||||
label: "Table",
|
||||
items: [
|
||||
{
|
||||
kind: "action",
|
||||
label: "Insert table",
|
||||
run: () => editor.chain().focus().insertTable({ rows: 3, cols: 3, withHeaderRow: true }).run(),
|
||||
},
|
||||
{ kind: "sep" },
|
||||
{
|
||||
kind: "action",
|
||||
label: "Add row above",
|
||||
disabled: !inTable,
|
||||
run: () => editor.chain().focus().addRowBefore().run(),
|
||||
},
|
||||
{
|
||||
kind: "action",
|
||||
label: "Add row below",
|
||||
disabled: !inTable,
|
||||
run: () => editor.chain().focus().addRowAfter().run(),
|
||||
},
|
||||
{
|
||||
kind: "action",
|
||||
label: "Delete row",
|
||||
disabled: !inTable,
|
||||
run: () => editor.chain().focus().deleteRow().run(),
|
||||
},
|
||||
{ kind: "sep" },
|
||||
{
|
||||
kind: "action",
|
||||
label: "Add column left",
|
||||
disabled: !inTable,
|
||||
run: () => editor.chain().focus().addColumnBefore().run(),
|
||||
},
|
||||
{
|
||||
kind: "action",
|
||||
label: "Add column right",
|
||||
disabled: !inTable,
|
||||
run: () => editor.chain().focus().addColumnAfter().run(),
|
||||
},
|
||||
{
|
||||
kind: "action",
|
||||
label: "Delete column",
|
||||
disabled: !inTable,
|
||||
run: () => editor.chain().focus().deleteColumn().run(),
|
||||
},
|
||||
{
|
||||
kind: "action",
|
||||
label: "Delete table",
|
||||
disabled: !inTable,
|
||||
run: () => editor.chain().focus().deleteTable().run(),
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user