Skip to content

Commit f70c31d

Browse files
committed
♻️ 本文データをshallow copyして使う
1 parent 3cc9f1c commit f70c31d

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

browser/dom/node.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import { isNone, isNumber, isString } from "../../is.ts";
55
import { ensureArray } from "../../ensure.ts";
66
import { getCachedLines } from "./getCachedLines.ts";
7-
import type { Line, Scrapbox } from "../../deps/scrapbox.ts";
7+
import { takeInternalLines } from "./takeInternalLines.ts";
8+
import type { BaseLine, Line } from "../../deps/scrapbox.ts";
89
import { lines } from "./dom.ts";
910
import * as Text from "../../text.ts";
10-
declare const scrapbox: Scrapbox;
1111

1212
/** Get the line id from value
1313
*
@@ -21,9 +21,7 @@ export const getLineId = <T extends HTMLElement>(
2121
if (isNone(value)) return undefined;
2222

2323
// 行番号のとき
24-
if (isNumber(value)) {
25-
return getLine(value)?.id;
26-
}
24+
if (isNumber(value)) return getBaseLine(value)?.id;
2725
// 行IDのとき
2826
if (isString(value)) return value.startsWith("L") ? value.slice(1) : value;
2927

@@ -51,7 +49,7 @@ export const getLineNo = <T extends HTMLElement>(
5149
if (isNumber(value)) return value;
5250
// 行ID or DOMのとき
5351
const id = getLineId(value);
54-
return id ? getLines().findIndex((line) => line.id === id) : -1;
52+
return id ? takeInternalLines().findIndex((line) => line.id === id) : -1;
5553
};
5654

5755
export const getLine = <T extends HTMLElement>(
@@ -60,14 +58,24 @@ export const getLine = <T extends HTMLElement>(
6058
if (isNone(value)) return undefined;
6159

6260
// 行番号のとき
63-
if (isNumber(value)) {
64-
return getLines()[value];
65-
}
61+
if (isNumber(value)) return getLines()[value];
6662
// 行ID or DOMのとき
6763
const id = getLineId(value);
6864
return id ? getLines().find((line) => line.id === id) : undefined;
6965
};
7066

67+
export const getBaseLine = <T extends HTMLElement>(
68+
value?: number | string | T,
69+
): BaseLine | undefined => {
70+
if (isNone(value)) return undefined;
71+
72+
// 行番号のとき
73+
if (isNumber(value)) return takeInternalLines()[value];
74+
// 行ID or DOMのとき
75+
const id = getLineId(value);
76+
return id ? takeInternalLines().find((line) => line.id === id) : undefined;
77+
};
78+
7179
export const getLineDOM = <T extends HTMLElement>(
7280
value?: number | string | T,
7381
): HTMLDivElement | undefined => {
@@ -82,7 +90,7 @@ export const getLineDOM = <T extends HTMLElement>(
8290
export const isLineDOM = (dom: unknown): dom is HTMLDivElement =>
8391
dom instanceof HTMLDivElement && dom.classList.contains("line");
8492

85-
export const getLineCount = (): number => getLines().length;
93+
export const getLineCount = (): number => takeInternalLines().length;
8694

8795
export const getLines = (): readonly Line[] => {
8896
const lines = getCachedLines();
@@ -96,9 +104,9 @@ export const getText = <T extends HTMLElement>(
96104
if (isNone(value)) return undefined;
97105

98106
// 数字と文字列は行として扱う
99-
if (isNumber(value) || isString(value)) return getLine(value)?.text;
107+
if (isNumber(value) || isString(value)) return getBaseLine(value)?.text;
100108
if (!(value instanceof HTMLElement)) return;
101-
if (isLineDOM(value)) return getLine(value)?.text;
109+
if (isLineDOM(value)) return getBaseLine(value)?.text;
102110
// 文字のDOMだったとき
103111
if (value.classList.contains("char-index")) {
104112
return value.textContent ?? undefined;
@@ -108,11 +116,11 @@ export const getText = <T extends HTMLElement>(
108116
value.classList.contains("line") ||
109117
value.getElementsByClassName("lines")?.[0]
110118
) {
111-
return getLines().map(({ text }) => text).join("\n");
119+
return takeInternalLines().map(({ text }) => text).join("\n");
112120
}
113121
//中に含まれている文字の列番号を全て取得し、それに対応する文字列を返す
114122
const chars = [] as number[];
115-
const line = getLine(value);
123+
const line = getBaseLine(value);
116124
if (isNone(line)) return;
117125
for (const dom of getChars(value)) {
118126
chars.push(getIndex(dom));

0 commit comments

Comments
 (0)