From f70c31d8155b416ea27aae273f7ba84b475b9687 Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Fri, 7 Apr 2023 07:53:20 +0900 Subject: [PATCH] =?UTF-8?q?:recycle:=20=E6=9C=AC=E6=96=87=E3=83=87?= =?UTF-8?q?=E3=83=BC=E3=82=BF=E3=82=92shallow=20copy=E3=81=97=E3=81=A6?= =?UTF-8?q?=E4=BD=BF=E3=81=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- browser/dom/node.ts | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/browser/dom/node.ts b/browser/dom/node.ts index 3abd2be..699a659 100644 --- a/browser/dom/node.ts +++ b/browser/dom/node.ts @@ -4,10 +4,10 @@ import { isNone, isNumber, isString } from "../../is.ts"; import { ensureArray } from "../../ensure.ts"; import { getCachedLines } from "./getCachedLines.ts"; -import type { Line, Scrapbox } from "../../deps/scrapbox.ts"; +import { takeInternalLines } from "./takeInternalLines.ts"; +import type { BaseLine, Line } from "../../deps/scrapbox.ts"; import { lines } from "./dom.ts"; import * as Text from "../../text.ts"; -declare const scrapbox: Scrapbox; /** Get the line id from value * @@ -21,9 +21,7 @@ export const getLineId = ( if (isNone(value)) return undefined; // 行番号のとき - if (isNumber(value)) { - return getLine(value)?.id; - } + if (isNumber(value)) return getBaseLine(value)?.id; // 行IDのとき if (isString(value)) return value.startsWith("L") ? value.slice(1) : value; @@ -51,7 +49,7 @@ export const getLineNo = ( if (isNumber(value)) return value; // 行ID or DOMのとき const id = getLineId(value); - return id ? getLines().findIndex((line) => line.id === id) : -1; + return id ? takeInternalLines().findIndex((line) => line.id === id) : -1; }; export const getLine = ( @@ -60,14 +58,24 @@ export const getLine = ( if (isNone(value)) return undefined; // 行番号のとき - if (isNumber(value)) { - return getLines()[value]; - } + if (isNumber(value)) return getLines()[value]; // 行ID or DOMのとき const id = getLineId(value); return id ? getLines().find((line) => line.id === id) : undefined; }; +export const getBaseLine = ( + value?: number | string | T, +): BaseLine | undefined => { + if (isNone(value)) return undefined; + + // 行番号のとき + if (isNumber(value)) return takeInternalLines()[value]; + // 行ID or DOMのとき + const id = getLineId(value); + return id ? takeInternalLines().find((line) => line.id === id) : undefined; +}; + export const getLineDOM = ( value?: number | string | T, ): HTMLDivElement | undefined => { @@ -82,7 +90,7 @@ export const getLineDOM = ( export const isLineDOM = (dom: unknown): dom is HTMLDivElement => dom instanceof HTMLDivElement && dom.classList.contains("line"); -export const getLineCount = (): number => getLines().length; +export const getLineCount = (): number => takeInternalLines().length; export const getLines = (): readonly Line[] => { const lines = getCachedLines(); @@ -96,9 +104,9 @@ export const getText = ( if (isNone(value)) return undefined; // 数字と文字列は行として扱う - if (isNumber(value) || isString(value)) return getLine(value)?.text; + if (isNumber(value) || isString(value)) return getBaseLine(value)?.text; if (!(value instanceof HTMLElement)) return; - if (isLineDOM(value)) return getLine(value)?.text; + if (isLineDOM(value)) return getBaseLine(value)?.text; // 文字のDOMだったとき if (value.classList.contains("char-index")) { return value.textContent ?? undefined; @@ -108,11 +116,11 @@ export const getText = ( value.classList.contains("line") || value.getElementsByClassName("lines")?.[0] ) { - return getLines().map(({ text }) => text).join("\n"); + return takeInternalLines().map(({ text }) => text).join("\n"); } //中に含まれている文字の列番号を全て取得し、それに対応する文字列を返す const chars = [] as number[]; - const line = getLine(value); + const line = getBaseLine(value); if (isNone(line)) return; for (const dom of getChars(value)) { chars.push(getIndex(dom));