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 3
3
/// <reference lib="dom" />
4
4
import { isNone , isNumber , isString } from "../../is.ts" ;
5
5
import { ensureArray } from "../../ensure.ts" ;
6
+ import { getCachedLines } from "./getCachedLines.ts" ;
6
7
import type { Line , Scrapbox } from "../../deps/scrapbox.ts" ;
7
8
import { lines } from "./dom.ts" ;
8
9
import * as Text from "../../text.ts" ;
@@ -83,9 +84,10 @@ export const isLineDOM = (dom: unknown): dom is HTMLDivElement =>
83
84
84
85
export const getLineCount = ( ) : number => getLines ( ) . length ;
85
86
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 ;
89
91
} ;
90
92
91
93
export const getText = < T extends HTMLElement > (
Original file line number Diff line number Diff line change 1
1
import { isString } from "./is.ts" ;
2
2
3
+ /** インデント数を数える */
3
4
export const getIndentCount = ( text : string ) : number =>
4
5
text . match ( / ^ ( \s * ) / ) ?. [ 1 ] ?. length ?? 0 ;
5
6
@@ -8,10 +9,10 @@ export const getIndentCount = (text: string): number =>
8
9
* @param index 指定したい行の行番号
9
10
* @param lines 行のリスト
10
11
*/
11
- export function getIndentLineCount (
12
+ export const getIndentLineCount = (
12
13
index : number ,
13
- lines : string [ ] | { text : string } [ ] ,
14
- ) : number {
14
+ lines : readonly string [ ] | readonly { text : string } [ ] ,
15
+ ) : number => {
15
16
const base = getIndentCount ( getText ( index , lines ) ) ;
16
17
let count = 0 ;
17
18
while (
@@ -21,9 +22,12 @@ export function getIndentLineCount(
21
22
count ++ ;
22
23
}
23
24
return count ;
24
- }
25
+ } ;
25
26
26
- function getText ( index : number , lines : string [ ] | { text : string } [ ] ) {
27
+ const getText = (
28
+ index : number ,
29
+ lines : readonly string [ ] | readonly { text : string } [ ] ,
30
+ ) => {
27
31
const line = lines [ index ] ;
28
32
return isString ( line ) ? line : line . text ;
29
- }
33
+ } ;
You can’t perform that action at this time.
0 commit comments