-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
209 lines (196 loc) · 5.77 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/// <reference no-default-lib="true" />
/// <reference lib="esnext" />
/// <reference lib="dom" />
/** @jsx h */
/** @jsxFrag Fragment */
import {
Fragment,
h,
useEffect,
useMemo,
useRef,
useState,
} from "./deps/preact.tsx";
import { useSelection } from "./useSelection.ts";
import { useFrag } from "./useFrag.ts";
import { useSource } from "./useSource.ts";
import { usePosition } from "./usePosition.ts";
import { Candidate as CandidateComponent } from "./Candidate.tsx";
import { SelectInit, useSelect } from "./useSelect.ts";
import { detectURL } from "./util.ts";
import { incrementalSearch } from "./incrementalSearch.ts";
import { sort } from "./search.ts";
import { insertText, Scrapbox } from "./deps/scrapbox.ts";
declare const scrapbox: Scrapbox;
export interface Operators {
selectNext: (init?: SelectInit) => boolean;
selectPrev: (init?: SelectInit) => boolean;
selectFirst: () => boolean;
selectLast: () => boolean;
confirm: () => boolean;
cancel: () => boolean;
}
/** 外部開放用APIの初期値 */
const opInit: Operators = {
selectNext: () => false,
selectPrev: () => false,
selectFirst: () => false,
selectLast: () => false,
confirm: () => false,
cancel: () => false,
} as const;
export interface AppProps {
/** 表示する最大候補数 */
limit: number;
callback: (operators: Operators) => void;
projects: string[];
mark: Record<string, string | URL>;
hideSelfMark: boolean;
debug?: boolean;
}
export const App = (props: AppProps) => {
const { limit, callback, projects, debug, mark, hideSelfMark } = props;
const { text, range } = useSelection();
const [frag, setFrag] = useFrag(text, range);
const source = useSource(projects, { debug });
// 検索
const [candidates, setCandidates] = useState<{
title: string;
projects: {
name: string;
mark: string | URL;
confirm: () => void;
}[];
confirm: () => void;
}[]>([]);
useEffect(() => {
setCandidates([]); // 以前のを消して、描画がちらつかないようにする
if (frag !== "enable") return;
if (text.trim() === "") return;
return incrementalSearch(text, source, (candidates) =>
setCandidates(
sort(candidates, projects)
.map((page) => ({
title: page.title,
projects: page.metadata.map(({ project }) => ({
name: project,
mark: hideSelfMark && project === scrapbox.Project.name
? ""
: detectURL(mark[project] ?? "", location.href) || project[0],
confirm: () => insertText(`[/${project}/${page.title}]`),
})),
confirm: () => insertText(`[${page.title}]`),
})),
));
}, [text, source, frag, projects, hideSelfMark]);
// 候補選択
const visibleCandidateCount = Math.min(candidates.length, limit);
const { selectedIndex, next, prev, selectFirst, selectLast } = useSelect(
visibleCandidateCount,
);
// スタイル設定
const { ref, top, left } = usePosition(range);
/** windowの開閉およびwindows操作の有効状態を決めるフラグ */
const isOpen = useMemo(
() => {
return frag === "enable" && candidates.length > 0 && top !== undefined &&
left !== undefined;
},
[frag, candidates.length, top, left],
);
const divStyle = useMemo<h.JSX.CSSProperties>(
() => !isOpen ? { display: "none" } : { top, left },
[isOpen, top, left],
);
// API提供
// ...でopInitが破壊されないようにする
const exportRef = useRef<Operators>({ ...opInit });
useEffect(() => {
// currentの参照を壊さずに更新する
Object.assign(
exportRef.current,
!isOpen ? opInit : {
selectNext: (init?: SelectInit) => (next(init), true),
selectPrev: (init?: SelectInit) => (prev(init), true),
selectFirst: () => (selectFirst(), true),
selectLast: () => (selectLast(), true),
confirm: () => {
const a = ref.current?.getElementsByClassName?.("candidate selected")
?.[0];
return a instanceof HTMLAnchorElement ? (a.click(), true) : false;
},
cancel: () => (setFrag("disable"), true),
},
);
}, [isOpen, next, prev, selectFirst, selectLast]);
useEffect(
() => callback(exportRef.current),
[callback],
);
return (
<>
<style>
{`.container {
position: absolute;
max-width: 80vw;
max-height: 80vh;
margin-top: 14px;
z-index: 301;
background-color: var(--select-suggest-bg, #111);
font-family: var(--select-suggest-font-family, "Open Sans", Helvetica, Arial, "Hiragino Sans", sans-serif);
color: var(--select-suggest-text-color, #eee);
border-radius: 4px;
}
.container > :not(:first-child) {
border-top: 1px solid var(--select-suggest-border-color, #eee);
}
.container > *{
font-size: 11px;
line-height: 1.2em;
padding: 0.5em 10px;
}
.candidate {
display: flex;
}
a {
display: block;
text-decoration: none;
color: inherit;
}
a:not(.mark) {
width: 100%;
}
.selected a {
background-color: var(--select-suggest-selected-bg, #222);
text-decoration: underline
}
.candidate img {
height: 1.3em;
width: 1.3em;
position: relative;
object-fit: cover;
object-position: 0% 0%;
}
.counter {
color: var(--select-suggest-information-text-color, #aaa);
font-size: 80%;
font-style: italic;
}`}
</style>
<div className="container" ref={ref} style={divStyle}>
{candidates.slice(0, visibleCandidateCount).map((props, i) => (
<CandidateComponent
key={props.title}
{...props}
selected={selectedIndex === i}
/>
))}
{candidates.length > limit && (
<div className="counter">
{`${candidates.length - limit} more links`}
</div>
)}
</div>
</>
);
};