import { Icon, useStatusBar } from "./statusBar.ts"; import { caret, downBlocks, downLines, getText, indentBlocks, indentLines, insertText, outdentBlocks, outdentLines, press, redo, takeCursor, takeSelection, undo, upBlocks, upLines, } from "../../takker/scrapbox-userscript-std/dom.ts"; const selection = takeSelection(); const cursor = takeCursor(); const data: { type: Icon; onClick: () => void }[] = [ { type: "caret-left", onClick: () => {cursor.focus();caret().selectedText === "" ? outdentBlocks() : outdentLines()}, }, { type: "caret-right", onClick: () => {cursor.focus();caret().selectedText === "" ? indentBlocks() : indentLines()}, }, { type: "caret-up", onClick: () => {cursor.focus();caret().selectedText === "" ? upBlocks() : upLines()}, }, { type: "caret-down", onClick: () => {cursor.focus();caret().selectedText === "" ? downBlocks() : downLines()}, }, { type: "copy", onClick: async () => { try { const { position, selectedText } = caret(); const text = selectedText || getText(position.line); if (!text) return; await navigator.clipboard.writeText(text); } catch (e: unknown) { console.error(e); alert(`Faild to copy:\n${JSON.stringify(e)}`); } }, }, { type: "cut", onClick: async () => { try { const hasSelection = selection.hasSelection(); const start = selection.getRange().start.line; const text = hasSelection ? selection.getSelectedText() : getText(start); if (!text) return; await navigator.clipboard.writeText(text); if (!hasSelection) { selection.setRange({ start: { line: start, char: 0 }, end: { line: start, char: text.length }, }); } cursor.focus(); press("Delete"); } catch (e: unknown) { console.error(e); alert(`Faild to cut:\n${JSON.stringify(e)}`); } }, }, { type: "clipboard", onClick: async () => { try { const text = await navigator.clipboard.readText(); if (!text) return; cursor.focus(); await insertText(text); } catch (e: unknown) { console.error(e); alert(`Faild to paste:\n${JSON.stringify(e)}`); } }, }, { type: "undo", onClick: () => undo(), }, { type: "redo", onClick: () => redo(), }, { type: "i-cursor", onClick: () => { if (cursor.getVisible()) { cursor.hide(); } else { cursor.focus(); cursor.showEditPopupMenu(); } }, }, ]; if (/mobile/i.test(navigator.userAgent)) { for (const { type, onClick } of data) { const { render, dispose } = useStatusBar(); render([{ type }], onClick); } }