File tree Expand file tree Collapse file tree 3 files changed +37
-9
lines changed Expand file tree Collapse file tree 3 files changed +37
-9
lines changed Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 33/// <reference lib="dom" />
44import { isNone , isNumber , isString } from "../../is.ts" ;
55import { ensureArray } from "../../ensure.ts" ;
6+ import { getCachedLines } from "./getCachedLines.ts" ;
67import type { Line , Scrapbox } from "../../deps/scrapbox.ts" ;
78import { lines } from "./dom.ts" ;
89import * as Text from "../../text.ts" ;
@@ -83,9 +84,10 @@ export const isLineDOM = (dom: unknown): dom is HTMLDivElement =>
8384
8485export 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
9193export const getText = < T extends HTMLElement > (
Original file line number Diff line number Diff line change 11import { isString } from "./is.ts" ;
22
3+ /** インデント数を数える */
34export 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+ } ;
You can’t perform that action at this time.
0 commit comments