import { useEditBar, ItemIcon } from "./editBar.ts"; import { caret, downBlocks, downLines, getText, indentBlocks, indentLines, insertText, outdentBlocks, outdentLines, press, redo, takeCursor, takeSelection, undo, upBlocks, upLines, } from "https://scrapbox.io/api/code/takker/scrapbox-userscript-std/dom.ts"; function getIconClass(name: string): string[] { switch(name){ case "spinner": return ["fa", "fa-spinner"]; case "check-circle": return ["kamon", "kamon-check-circle"]; case "exclamation-triangle": case "caret-up": case "caret-down": case "caret-left": case "caret-right": case "cut": case "expand": case "i-cursor": case "undo": case "redo": return ["fas", `fa-${name}`]; case "copy": case "clipboard": return ["far", `fa-${name}`]; default: return [""]; } } const selection = takeSelection(); const cursor = takeCursor(); const data: {name: string; onClick: (e:MouseEvent) => void}[] = [ { name: "caret-left", onClick: () => {cursor.focus();caret().selectedText === "" ? outdentBlocks() : outdentLines()}, }, { name: "caret-right", onClick: () => {cursor.focus();caret().selectedText === "" ? indentBlocks() : indentLines()}, }, { name: "caret-up", onClick: () => {cursor.focus();caret().selectedText === "" ? upBlocks() : upLines()}, }, { name: "caret-down", onClick: () => {cursor.focus();caret().selectedText === "" ? downBlocks() : downLines()}, }, { name: "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)}`); } }, }, { name: "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)}`); } }, }, { name: "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)}`); } }, }, { name: "undo", onClick: () => undo(), }, { name: "redo", onClick: () => redo(), }, { name: "i-cursor", onClick: () => { if (cursor.getVisible()) { cursor.hide(); } else { // カーソルを再表示する機能はいらないので無効化した // cursor.focus(); // cursor.showEditPopupMenu(); } }, }, ]; if (/mobile/i.test(navigator.userAgent)) { // dataやtextsの中身を読み取ってボタンを作成する for (const { name, onClick } of data) { const { render } = useEditBar(); render({type:"icon", name, iconClass:getIconClass(name), onClick} as ItemIcon); } }