import type { Cursor, Selection } from "../scrapbox-userscript-std/dom.ts"; import { getText, insertText, press, } from "../scrapbox-userscript-std/dom.ts"; /** porterの挙動に合わせたcopy command */ export const porterCopy = async (cursor: Cursor, selection: Selection): Promise => { try { const text = selection.getSelectedText() || getText(cursor.getPosition().line); if (!text) return; await navigator.clipboard.writeText(text); } catch (e: unknown) { console.error(e); alert(`Faild to copy:\n${JSON.stringify(e)}`); } }; /** porterの挙動に合わせたcut command */ export const porterCut = async (cursor: Cursor, selection: Selection): Promise => { try { const hasSelection = selection.hasSelection(); const start = hasSelection ? selection.getRange().start.line : cursor.getPosition().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)}`); } }; /** porterの挙動に合わせたpaste command */ export const porterPaste = async (cursor: Cursor): Promise => { 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)}`); } }; /** cursorを表示を切り替える * * mobileだとpopup menuを表示するcommandとしても使える */ export const toggleCaret = (cursor: Cursor) => { if (cursor.getVisible() && cursor.hasFocus // @ts-ignore private扱いだけど、これしかアクセス方法がないため使わせていただく && cursor.visiblePopupMenu) { cursor.hide(); } else { cursor.focus(); cursor.showEditPopupMenu(); } };