Skip to content

Commit c8ced06

Browse files
authored
Merge pull request #116 from takker99/cached-lines
✨ 同じscrapbox.Page.linesの再生成コストをなくす関数を追加
2 parents 45d200f + 5056cca commit c8ced06

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

browser/dom/getCachedLines.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Line, Scrapbox } from "../../deps/scrapbox.ts";
2+
declare const scrapbox: Scrapbox;
3+
4+
let isLatestData = false;
5+
let lines: typeof scrapbox.Page.lines = null;
6+
7+
scrapbox.addListener("lines:changed", () => isLatestData = false);
8+
scrapbox.addListener("layout:changed", () => isLatestData = false);
9+
10+
/** scrapbox.Page.linesをcacheして取得する
11+
*
12+
* scrapbox.Page.linesの生成には時間がかかるので、実際に必要になるまで呼び出さないようにする
13+
*
14+
* @return `scrapbox.Page.lines`と同じ。常に最新のものが返される
15+
*/
16+
export const getCachedLines = (): readonly Line[] | null => {
17+
if (!isLatestData) {
18+
lines = scrapbox.Page.lines;
19+
isLatestData = true;
20+
}
21+
return lines;
22+
};

browser/dom/node.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/// <reference lib="dom" />
44
import { isNone, isNumber, isString } from "../../is.ts";
55
import { ensureArray } from "../../ensure.ts";
6+
import { getCachedLines } from "./getCachedLines.ts";
67
import type { Line, Scrapbox } from "../../deps/scrapbox.ts";
78
import { lines } from "./dom.ts";
89
import * as Text from "../../text.ts";
@@ -83,9 +84,10 @@ export const isLineDOM = (dom: unknown): dom is HTMLDivElement =>
8384

8485
export const getLineCount = (): number => getLines().length;
8586

86-
export const getLines = (): Line[] => {
87-
ensureArray(scrapbox.Page.lines, "scrapbox.Page.lines");
88-
return scrapbox.Page.lines;
87+
export const getLines = (): readonly Line[] => {
88+
const lines = getCachedLines();
89+
ensureArray<Line>(lines, "scrapbox.Page.lines");
90+
return lines;
8991
};
9092

9193
export const getText = <T extends HTMLElement>(

text.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { isString } from "./is.ts";
22

3+
/** インデント数を数える */
34
export const getIndentCount = (text: string): number =>
45
text.match(/^(\s*)/)?.[1]?.length ?? 0;
56

@@ -8,10 +9,10 @@ export const getIndentCount = (text: string): number =>
89
* @param index 指定したい行の行番号
910
* @param lines 行のリスト
1011
*/
11-
export function getIndentLineCount(
12+
export const getIndentLineCount = (
1213
index: number,
13-
lines: string[] | { text: string }[],
14-
): number {
14+
lines: readonly string[] | readonly { text: string }[],
15+
): number => {
1516
const base = getIndentCount(getText(index, lines));
1617
let count = 0;
1718
while (
@@ -21,9 +22,12 @@ export function getIndentLineCount(
2122
count++;
2223
}
2324
return count;
24-
}
25+
};
2526

26-
function getText(index: number, lines: string[] | { text: string }[]) {
27+
const getText = (
28+
index: number,
29+
lines: readonly string[] | readonly { text: string }[],
30+
) => {
2731
const line = lines[index];
2832
return isString(line) ? line : line.text;
29-
}
33+
};

0 commit comments

Comments
 (0)