knowidea-ui 0.10.1 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/AppShell.d.ts +29 -0
- package/dist/AppShell.js +12 -0
- package/dist/CodeEditor.d.ts +73 -0
- package/dist/CodeEditor.js +191 -0
- package/dist/Conversation.d.ts +29 -0
- package/dist/Conversation.js +14 -0
- package/dist/DiffView.d.ts +25 -0
- package/dist/DiffView.js +90 -0
- package/dist/Layout.d.ts +1 -1
- package/dist/ListDetail.d.ts +24 -0
- package/dist/ListDetail.js +10 -0
- package/dist/breakpoints.d.ts +12 -0
- package/dist/breakpoints.js +8 -0
- package/dist/diff.d.ts +46 -0
- package/dist/diff.js +172 -0
- package/dist/editor.d.ts +2 -0
- package/dist/editor.js +3 -0
- package/dist/editorText.d.ts +10 -0
- package/dist/editorText.js +38 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +8 -0
- package/package.json +25 -9
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# knowidea-ui
|
|
2
2
|
|
|
3
|
-
KNOW IDEA UI の React の部品。
|
|
3
|
+
KNOW IDEA UI の React の部品。63 個。ボタン、入力、表、ダイアログ、トースト、大きな見出しと題字のジャケットなど。ライトとダークの両方に対応し、押せる所はすべて 44px 以上。
|
|
4
4
|
|
|
5
5
|
[ドキュメント](https://takikou347.github.io/knowidea-ui/) / [GitHub](https://github.com/takikou347/knowidea-ui) / MIT
|
|
6
6
|
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { CSSProperties, ReactNode } from "react";
|
|
2
|
+
import type { NavItem } from "./NavItems.js";
|
|
3
|
+
export interface AppShellProps {
|
|
4
|
+
/** 画面の切り替え。3〜5 個 */
|
|
5
|
+
items: NavItem[];
|
|
6
|
+
/** 今いる所の id */
|
|
7
|
+
current?: string;
|
|
8
|
+
/** 左の切り替えの上に出す名前(PC の幅だけ) */
|
|
9
|
+
brand?: {
|
|
10
|
+
name: string;
|
|
11
|
+
sub?: string;
|
|
12
|
+
};
|
|
13
|
+
/** 切り替えの nav の名前。既定「画面の切り替え」 */
|
|
14
|
+
label?: string;
|
|
15
|
+
/** 中身の上に置く物。AppBar など */
|
|
16
|
+
header?: ReactNode;
|
|
17
|
+
/** 左の切り替えの下に置く物(PC の幅だけ) */
|
|
18
|
+
sideFooter?: ReactNode;
|
|
19
|
+
/** 枠の最小の高さ。既定 100dvh。見本の中に置く時に絞る */
|
|
20
|
+
minHeight?: string;
|
|
21
|
+
className?: string;
|
|
22
|
+
style?: CSSProperties;
|
|
23
|
+
children?: ReactNode;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 画面の枠。スマホと iPad(1024px 未満)は下に、PC(1024px 以上)は左に画面の切り替えを出す。
|
|
27
|
+
* 中身は <main> に入る。切り替えは両方を描き、幅で片方だけを見せる(見えない方は読み上げにも出ない)。
|
|
28
|
+
*/
|
|
29
|
+
export declare function AppShell({ items, current, brand, label, header, sideFooter, minHeight, className, style, children }: AppShellProps): import("react").JSX.Element;
|
package/dist/AppShell.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { BottomNavigation } from "./BottomNavigation.js";
|
|
3
|
+
import { Drawer } from "./Drawer.js";
|
|
4
|
+
import { cx } from "./utils.js";
|
|
5
|
+
/**
|
|
6
|
+
* 画面の枠。スマホと iPad(1024px 未満)は下に、PC(1024px 以上)は左に画面の切り替えを出す。
|
|
7
|
+
* 中身は <main> に入る。切り替えは両方を描き、幅で片方だけを見せる(見えない方は読み上げにも出ない)。
|
|
8
|
+
*/
|
|
9
|
+
export function AppShell({ items, current, brand, label = "画面の切り替え", header, sideFooter, minHeight, className, style, children }) {
|
|
10
|
+
const s = { ...(minHeight ? { "--ki-shell-height": minHeight } : null), ...style };
|
|
11
|
+
return (_jsxs("div", { className: cx("ki-shell", className), style: s, children: [_jsx(Drawer, { className: "ki-shell__side", variant: "permanent", items: items, current: current, brand: brand, label: label, footer: sideFooter }), _jsxs("div", { className: "ki-shell__body", children: [header, _jsx("main", { className: "ki-shell__main", children: children })] }), _jsx(BottomNavigation, { className: "ki-shell__bottom", items: items, current: current, label: label, position: "sticky" })] }));
|
|
12
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { type CSSProperties } from "react";
|
|
2
|
+
import { type Extension } from "@codemirror/state";
|
|
3
|
+
import { type EditorLanguage } from "./editorText.js";
|
|
4
|
+
/** エディタの中の場所。line と column は 1 から、offset は 0 から */
|
|
5
|
+
export interface EditorPlace {
|
|
6
|
+
line: number;
|
|
7
|
+
column: number;
|
|
8
|
+
offset: number;
|
|
9
|
+
}
|
|
10
|
+
export interface EditorDiagnostic {
|
|
11
|
+
/** 始まりの行と列(1 から) */
|
|
12
|
+
line: number;
|
|
13
|
+
column: number;
|
|
14
|
+
/** 終わりの行と列。無ければ始まりから 1 字 */
|
|
15
|
+
endLine?: number;
|
|
16
|
+
endColumn?: number;
|
|
17
|
+
severity: "error" | "warning" | "info";
|
|
18
|
+
message: string;
|
|
19
|
+
}
|
|
20
|
+
export interface EditorCompletion {
|
|
21
|
+
label: string;
|
|
22
|
+
/** 候補の右に出す短い説明(型など) */
|
|
23
|
+
detail?: string;
|
|
24
|
+
/** 選んだ時に横に出す説明 */
|
|
25
|
+
info?: string;
|
|
26
|
+
/** 印の種類。function、variable、property、keyword、type、class など */
|
|
27
|
+
type?: string;
|
|
28
|
+
/** 入れる文字。無ければ label */
|
|
29
|
+
apply?: string;
|
|
30
|
+
}
|
|
31
|
+
export interface CodeEditorProps {
|
|
32
|
+
value: string;
|
|
33
|
+
onChange?: (value: string) => void;
|
|
34
|
+
/** 読み上げの名前。ファイルの名前など */
|
|
35
|
+
label: string;
|
|
36
|
+
/** 言語。パスから決める時は editorLanguageOfPath(path) */
|
|
37
|
+
language?: EditorLanguage;
|
|
38
|
+
/** 読むだけにする。選ぶ、写す、キーで動くはできる */
|
|
39
|
+
readOnly?: boolean;
|
|
40
|
+
/** 長い行を折り返す。既定は、スマホの幅(600px 未満)だけ折り返す */
|
|
41
|
+
wrap?: boolean;
|
|
42
|
+
/** 書き間違いなどの知らせ。その場所に下線が付き、合わせると中身が出る */
|
|
43
|
+
diagnostics?: EditorDiagnostic[];
|
|
44
|
+
/** 候補を返す。言語が元から持つ候補に足される。無い時は null */
|
|
45
|
+
complete?: (place: EditorPlace & {
|
|
46
|
+
text: string;
|
|
47
|
+
word: string;
|
|
48
|
+
explicit: boolean;
|
|
49
|
+
}) => EditorCompletion[] | null | Promise<EditorCompletion[] | null>;
|
|
50
|
+
/** 合わせた所の型や説明を返す。無い時は null */
|
|
51
|
+
hover?: (place: EditorPlace & {
|
|
52
|
+
text: string;
|
|
53
|
+
}) => string | null | Promise<string | null>;
|
|
54
|
+
/** 定義へ移る(F12 か、Ctrl か Cmd を押しながら押す)時に呼ぶ。移る先は使う側が決める */
|
|
55
|
+
onDefinition?: (place: EditorPlace & {
|
|
56
|
+
text: string;
|
|
57
|
+
}) => void;
|
|
58
|
+
/** Ctrl+S か Cmd+S で呼ぶ */
|
|
59
|
+
onSave?: (value: string) => void;
|
|
60
|
+
/** 高さ。CSS の長さ(例 "20rem")。無ければ中身に合わせて伸びる */
|
|
61
|
+
height?: string;
|
|
62
|
+
/** CodeMirror の拡張を足す */
|
|
63
|
+
extensions?: Extension[];
|
|
64
|
+
className?: string;
|
|
65
|
+
style?: CSSProperties;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* コードを書く欄(CodeMirror 6)。色分け、括弧の対応、折り畳み、候補、書き間違いの下線を持つ。スマホでも動く。
|
|
69
|
+
* Tab は字下げに使う。Esc を押してから Tab を押すと、エディタの外へ移る。
|
|
70
|
+
* 外から value を替えると(別のファイルを開いた時など)、onChange は呼ばず、元に戻す履歴を空にする。
|
|
71
|
+
* knowidea-ui/editor から読み込む(CodeMirror を、使う画面でだけ読み込むため)。
|
|
72
|
+
*/
|
|
73
|
+
export declare function CodeEditor({ value, onChange, label, language, readOnly, wrap, diagnostics, complete, hover, onDefinition, onSave, height, extensions, className, style }: CodeEditorProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useId, useRef, useState } from "react";
|
|
3
|
+
import { autocompletion, closeBrackets, closeBracketsKeymap, completionKeymap } from "@codemirror/autocomplete";
|
|
4
|
+
import { defaultKeymap, history, historyKeymap, indentWithTab, temporarilySetTabFocusMode } from "@codemirror/commands";
|
|
5
|
+
import { css } from "@codemirror/lang-css";
|
|
6
|
+
import { javascript } from "@codemirror/lang-javascript";
|
|
7
|
+
import { json } from "@codemirror/lang-json";
|
|
8
|
+
import { markdown } from "@codemirror/lang-markdown";
|
|
9
|
+
import { bracketMatching, foldGutter, foldKeymap, indentOnInput, indentUnit, syntaxHighlighting } from "@codemirror/language";
|
|
10
|
+
import { lintKeymap, setDiagnostics } from "@codemirror/lint";
|
|
11
|
+
import { Compartment, EditorState, StateEffect, StateField } from "@codemirror/state";
|
|
12
|
+
import { EditorView, drawSelection, highlightActiveLine, highlightActiveLineGutter, hoverTooltip, keymap, lineNumbers, showTooltip } from "@codemirror/view";
|
|
13
|
+
import { classHighlighter } from "@lezer/highlight";
|
|
14
|
+
import { mediaQuery } from "./breakpoints.js";
|
|
15
|
+
import { editorOffset, editorPosition } from "./editorText.js";
|
|
16
|
+
import { cx } from "./utils.js";
|
|
17
|
+
function languageOf(l) {
|
|
18
|
+
switch (l) {
|
|
19
|
+
case "typescript": return javascript({ typescript: true });
|
|
20
|
+
case "tsx": return javascript({ typescript: true, jsx: true });
|
|
21
|
+
case "javascript": return javascript();
|
|
22
|
+
case "jsx": return javascript({ jsx: true });
|
|
23
|
+
case "css": return css();
|
|
24
|
+
case "markdown": return markdown();
|
|
25
|
+
case "json": return json();
|
|
26
|
+
case "text": return [];
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const placeOf = (state, offset) => {
|
|
30
|
+
const text = state.doc.toString();
|
|
31
|
+
return { ...editorPosition(text, offset), offset, text };
|
|
32
|
+
};
|
|
33
|
+
function toDiagnostics(text, list) {
|
|
34
|
+
return list.map((d) => {
|
|
35
|
+
const from = editorOffset(text, d.line, d.column);
|
|
36
|
+
const to = d.endLine !== undefined || d.endColumn !== undefined ? editorOffset(text, d.endLine ?? d.line, d.endColumn ?? d.column + 1) : Math.min(from + 1, text.length);
|
|
37
|
+
return { from: Math.min(from, to), to: Math.max(from, to), severity: d.severity, message: d.message };
|
|
38
|
+
}).sort((a, b) => a.from - b.from);
|
|
39
|
+
}
|
|
40
|
+
const hoverDom = (text) => () => {
|
|
41
|
+
const dom = document.createElement("div");
|
|
42
|
+
dom.className = "ki-editor__hover";
|
|
43
|
+
dom.textContent = text;
|
|
44
|
+
return { dom };
|
|
45
|
+
};
|
|
46
|
+
// キーで出した説明(Ctrl+Shift+Space。Ctrl+K は、アプリの探す欄などに使われやすいので避けた)。打つか、カーソルを動かすか、Esc で消える
|
|
47
|
+
const setInfo = StateEffect.define();
|
|
48
|
+
const infoField = StateField.define({
|
|
49
|
+
create: () => null,
|
|
50
|
+
update(value, tr) {
|
|
51
|
+
for (const e of tr.effects)
|
|
52
|
+
if (e.is(setInfo))
|
|
53
|
+
return e.value;
|
|
54
|
+
return tr.docChanged || tr.selection ? null : value;
|
|
55
|
+
},
|
|
56
|
+
provide: (f) => showTooltip.from(f),
|
|
57
|
+
});
|
|
58
|
+
/**
|
|
59
|
+
* コードを書く欄(CodeMirror 6)。色分け、括弧の対応、折り畳み、候補、書き間違いの下線を持つ。スマホでも動く。
|
|
60
|
+
* Tab は字下げに使う。Esc を押してから Tab を押すと、エディタの外へ移る。
|
|
61
|
+
* 外から value を替えると(別のファイルを開いた時など)、onChange は呼ばず、元に戻す履歴を空にする。
|
|
62
|
+
* knowidea-ui/editor から読み込む(CodeMirror を、使う画面でだけ読み込むため)。
|
|
63
|
+
*/
|
|
64
|
+
export function CodeEditor({ value, onChange, label, language = "text", readOnly = false, wrap, diagnostics, complete, hover, onDefinition, onSave, height, extensions, className, style }) {
|
|
65
|
+
const host = useRef(null);
|
|
66
|
+
const view = useRef(null);
|
|
67
|
+
// ready は、エディタを作った回数と、外から中身を入れ替えた回数。替わるたびに、下の effect が今の設定を送り直す
|
|
68
|
+
const [ready, setReady] = useState(0);
|
|
69
|
+
const base = useRef([]);
|
|
70
|
+
const hint = useId();
|
|
71
|
+
// キーで出した説明を、読み上げに伝える文。初めから在る role="status" に入れる
|
|
72
|
+
const [spoken, setSpoken] = useState("");
|
|
73
|
+
// 呼ぶ物は ref に入れて、渡す関数が替わってもエディタを作り直さない
|
|
74
|
+
const cb = useRef({ onChange, complete, hover, onDefinition, onSave });
|
|
75
|
+
cb.current = { onChange, complete, hover, onDefinition, onSave };
|
|
76
|
+
const parts = useRef({ language: new Compartment(), readOnly: new Compartment(), wrap: new Compartment(), extra: new Compartment(), label: new Compartment() });
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
if (!host.current)
|
|
79
|
+
return;
|
|
80
|
+
const p = parts.current;
|
|
81
|
+
const completion = async (ctx) => {
|
|
82
|
+
const fn = cb.current.complete;
|
|
83
|
+
if (!fn)
|
|
84
|
+
return null;
|
|
85
|
+
const word = ctx.matchBefore(/[\w$-]*/);
|
|
86
|
+
if (!ctx.explicit && (!word || word.from === word.to) && !/[.<"'/ ]/.test(ctx.state.sliceDoc(ctx.pos - 1, ctx.pos)))
|
|
87
|
+
return null;
|
|
88
|
+
const items = await fn({ ...placeOf(ctx.state, ctx.pos), word: word?.text ?? "", explicit: ctx.explicit });
|
|
89
|
+
if (!items || !items.length || ctx.aborted)
|
|
90
|
+
return null;
|
|
91
|
+
return { from: word ? word.from : ctx.pos, options: items.map((i) => ({ label: i.label, detail: i.detail, info: i.info, type: i.type, apply: i.apply })), validFor: /^[\w$-]*$/ };
|
|
92
|
+
};
|
|
93
|
+
const definition = (v, offset) => {
|
|
94
|
+
const fn = cb.current.onDefinition;
|
|
95
|
+
if (!fn)
|
|
96
|
+
return false;
|
|
97
|
+
fn(placeOf(v.state, offset));
|
|
98
|
+
return true;
|
|
99
|
+
};
|
|
100
|
+
const info = async (kv) => {
|
|
101
|
+
const pos = kv.state.selection.main.head;
|
|
102
|
+
const text = await cb.current.hover?.(placeOf(kv.state, pos));
|
|
103
|
+
if (!text || kv.state.selection.main.head !== pos)
|
|
104
|
+
return;
|
|
105
|
+
kv.dispatch({ effects: setInfo.of({ pos, above: true, create: hoverDom(text) }) });
|
|
106
|
+
setSpoken(text);
|
|
107
|
+
};
|
|
108
|
+
base.current = [
|
|
109
|
+
lineNumbers(), highlightActiveLineGutter(), foldGutter(), history(), drawSelection(), indentOnInput(), bracketMatching(), closeBrackets(), highlightActiveLine(),
|
|
110
|
+
indentUnit.of(" "), EditorState.tabSize.of(2),
|
|
111
|
+
syntaxHighlighting(classHighlighter),
|
|
112
|
+
autocompletion(),
|
|
113
|
+
EditorState.languageData.of(() => [{ autocomplete: completion }]),
|
|
114
|
+
hoverTooltip(async (hv, pos) => {
|
|
115
|
+
const text = await cb.current.hover?.(placeOf(hv.state, pos));
|
|
116
|
+
return text ? { pos, above: true, create: hoverDom(text) } : null;
|
|
117
|
+
}),
|
|
118
|
+
infoField,
|
|
119
|
+
keymap.of([
|
|
120
|
+
{ key: "Mod-s", run: (kv) => { const fn = cb.current.onSave; if (!fn)
|
|
121
|
+
return false; fn(kv.state.doc.toString()); return true; } },
|
|
122
|
+
{ key: "F12", run: (kv) => definition(kv, kv.state.selection.main.head) },
|
|
123
|
+
{ key: "Mod-Shift-Space", run: (kv) => { if (!cb.current.hover)
|
|
124
|
+
return false; void info(kv); return true; } },
|
|
125
|
+
{ key: "Escape", run: (kv) => { if (!kv.state.field(infoField))
|
|
126
|
+
return false; kv.dispatch({ effects: setInfo.of(null) }); setSpoken(""); return true; } },
|
|
127
|
+
...closeBracketsKeymap, ...completionKeymap, ...lintKeymap, ...foldKeymap, ...historyKeymap,
|
|
128
|
+
{ key: "Escape", run: temporarilySetTabFocusMode },
|
|
129
|
+
indentWithTab, ...defaultKeymap,
|
|
130
|
+
]),
|
|
131
|
+
EditorView.domEventHandlers({
|
|
132
|
+
mousedown: (e, mv) => {
|
|
133
|
+
if (!(e.metaKey || e.ctrlKey) || e.button !== 0 || !cb.current.onDefinition)
|
|
134
|
+
return false;
|
|
135
|
+
const pos = mv.posAtCoords({ x: e.clientX, y: e.clientY });
|
|
136
|
+
if (pos === null)
|
|
137
|
+
return false;
|
|
138
|
+
e.preventDefault();
|
|
139
|
+
return definition(mv, pos);
|
|
140
|
+
},
|
|
141
|
+
}),
|
|
142
|
+
EditorView.updateListener.of((u) => { if (u.docChanged)
|
|
143
|
+
cb.current.onChange?.(u.state.doc.toString()); }),
|
|
144
|
+
p.label.of([]), p.language.of([]), p.readOnly.of([]), p.wrap.of([]), p.extra.of([]),
|
|
145
|
+
];
|
|
146
|
+
const v = new EditorView({ parent: host.current, state: EditorState.create({ doc: value, extensions: base.current }) });
|
|
147
|
+
view.current = v;
|
|
148
|
+
setReady((n) => n + 1);
|
|
149
|
+
return () => { v.destroy(); view.current = null; setReady(0); };
|
|
150
|
+
// 最初の 1 回だけ作る。後の変わり方は下の effect が送る
|
|
151
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
152
|
+
}, []);
|
|
153
|
+
// 外から value が替わった時だけ、中身を入れ替える(打った字は onChange で同じ値が戻るので、何もしない)。
|
|
154
|
+
// 状態ごと作り直すので、onChange は呼ばれず、元に戻す履歴に前の中身は残らない
|
|
155
|
+
useEffect(() => {
|
|
156
|
+
const v = view.current;
|
|
157
|
+
if (!v || v.state.doc.toString() === value)
|
|
158
|
+
return;
|
|
159
|
+
v.setState(EditorState.create({ doc: value, extensions: base.current }));
|
|
160
|
+
setReady((n) => n + 1);
|
|
161
|
+
}, [value, ready]);
|
|
162
|
+
useEffect(() => {
|
|
163
|
+
view.current?.dispatch({ effects: parts.current.label.reconfigure(EditorView.contentAttributes.of({ "aria-label": label, "aria-describedby": hint })) });
|
|
164
|
+
}, [label, hint, ready]);
|
|
165
|
+
useEffect(() => { view.current?.dispatch({ effects: parts.current.language.reconfigure(languageOf(language)) }); }, [language, ready]);
|
|
166
|
+
useEffect(() => {
|
|
167
|
+
view.current?.dispatch({ effects: parts.current.readOnly.reconfigure(readOnly ? [EditorState.readOnly.of(true), EditorView.contentAttributes.of({ "aria-readonly": "true" })] : []) });
|
|
168
|
+
}, [readOnly, ready]);
|
|
169
|
+
useEffect(() => { view.current?.dispatch({ effects: parts.current.extra.reconfigure(extensions ?? []) }); }, [extensions, ready]);
|
|
170
|
+
useEffect(() => {
|
|
171
|
+
const v = view.current;
|
|
172
|
+
if (!v)
|
|
173
|
+
return;
|
|
174
|
+
const set = (on) => v.dispatch({ effects: parts.current.wrap.reconfigure(on ? EditorView.lineWrapping : []) });
|
|
175
|
+
if (wrap !== undefined) {
|
|
176
|
+
set(wrap);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
const mq = window.matchMedia(mediaQuery.phone);
|
|
180
|
+
const onChangeWidth = () => set(mq.matches);
|
|
181
|
+
onChangeWidth();
|
|
182
|
+
mq.addEventListener("change", onChangeWidth);
|
|
183
|
+
return () => mq.removeEventListener("change", onChangeWidth);
|
|
184
|
+
}, [wrap, ready]);
|
|
185
|
+
useEffect(() => {
|
|
186
|
+
const v = view.current;
|
|
187
|
+
if (v)
|
|
188
|
+
v.dispatch(setDiagnostics(v.state, toDiagnostics(v.state.doc.toString(), diagnostics ?? [])));
|
|
189
|
+
}, [diagnostics, value, ready]);
|
|
190
|
+
return (_jsxs("div", { className: cx("ki-editor", className), style: height ? { ...style, ["--ki-editor-height"]: height } : style, "data-readonly": readOnly ? "" : undefined, "data-fixed": height ? "" : undefined, children: [_jsx("div", { ref: host, className: "ki-editor__host" }), ready ? null : _jsx("pre", { className: "ki-editor__fallback", role: "textbox", "aria-readonly": "true", "aria-multiline": "true", "aria-label": label, tabIndex: 0, children: value }), _jsx("span", { role: "status", className: "ki-sr-only", children: spoken }), _jsx("span", { id: hint, className: "ki-sr-only", children: "Tab \u306F\u5B57\u4E0B\u3052\u3002Esc \u3092\u62BC\u3057\u3066\u304B\u3089 Tab \u3092\u62BC\u3059\u3068\u3001\u30A8\u30C7\u30A3\u30BF\u306E\u5916\u3078\u79FB\u308B\u3002" })] }));
|
|
191
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
export interface ConversationMessage {
|
|
3
|
+
id: string;
|
|
4
|
+
/** self は自分(右)、other は相手(左) */
|
|
5
|
+
from: "self" | "other";
|
|
6
|
+
/** 話した人の名前。相手の時は吹き出しの上に出る。続けて同じ人が話した時は省く */
|
|
7
|
+
author: string;
|
|
8
|
+
/** 相手の顔の画像。無ければ頭文字 */
|
|
9
|
+
avatar?: string;
|
|
10
|
+
/** 時刻(ISO)。<time> の datetime になる */
|
|
11
|
+
time?: string;
|
|
12
|
+
/** 見せる時刻。既定は time のまま */
|
|
13
|
+
timeLabel?: string;
|
|
14
|
+
body: ReactNode;
|
|
15
|
+
/** sending は送っている途中、failed は送れなかった */
|
|
16
|
+
status?: "sending" | "failed";
|
|
17
|
+
}
|
|
18
|
+
export interface ConversationProps {
|
|
19
|
+
messages: ConversationMessage[];
|
|
20
|
+
/** 読み上げの名前。既定「会話」 */
|
|
21
|
+
label?: string;
|
|
22
|
+
/** 相手が返事を書いている時の 1 行(例「社長が返事を書いている」)。動かさない */
|
|
23
|
+
pending?: string;
|
|
24
|
+
/** 話が 1 つも無い時に出す物 */
|
|
25
|
+
empty?: ReactNode;
|
|
26
|
+
className?: string;
|
|
27
|
+
}
|
|
28
|
+
/** 会話の流れ。話しかけ(自分、右)と返事(相手、左)を時の順に並べる。新しい話は下に足す。 */
|
|
29
|
+
export declare function Conversation({ messages, label, pending, empty, className }: ConversationProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Avatar } from "./Avatar.js";
|
|
3
|
+
import { cx } from "./utils.js";
|
|
4
|
+
const statusText = { sending: "送っている", failed: "送れなかった" };
|
|
5
|
+
/** 会話の流れ。話しかけ(自分、右)と返事(相手、左)を時の順に並べる。新しい話は下に足す。 */
|
|
6
|
+
export function Conversation({ messages, label = "会話", pending, empty, className }) {
|
|
7
|
+
if (messages.length === 0 && !pending)
|
|
8
|
+
return _jsx("div", { className: cx("ki-convo", className), role: "log", "aria-label": label, children: empty });
|
|
9
|
+
return (_jsxs("div", { className: cx("ki-convo", className), role: "log", "aria-label": label, children: [_jsx("ol", { className: "ki-convo__list", children: messages.map((m, i) => {
|
|
10
|
+
const prev = messages[i - 1];
|
|
11
|
+
const cont = !!prev && prev.from === m.from && prev.author === m.author;
|
|
12
|
+
return (_jsxs("li", { className: "ki-convo__msg", "data-from": m.from, "data-continued": cont ? "" : undefined, "data-status": m.status, children: [m.from === "other" ? _jsx("span", { className: "ki-convo__avatar", children: cont ? null : _jsx(Avatar, { name: m.author, src: m.avatar, size: "sm", shape: "circle" }) }) : null, _jsxs("div", { className: "ki-convo__main", children: [cont ? null : _jsx("p", { className: "ki-convo__author", children: m.from === "self" ? _jsx("span", { className: "ki-sr-only", children: m.author }) : m.author }), _jsx("div", { className: "ki-convo__bubble", children: m.body }), m.time || m.status ? (_jsxs("p", { className: "ki-convo__meta", children: [m.time ? _jsx("time", { dateTime: m.time, children: m.timeLabel ?? m.time }) : null, m.status ? _jsx("span", { className: "ki-convo__status", children: statusText[m.status] }) : null] })) : null] })] }, m.id));
|
|
13
|
+
}) }), pending ? _jsx("p", { className: "ki-convo__pending", children: pending }) : null] }));
|
|
14
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
import { type DiffFile, type DiffLine } from "./diff.js";
|
|
3
|
+
export interface DiffViewProps {
|
|
4
|
+
files: DiffFile[];
|
|
5
|
+
/**
|
|
6
|
+
* 並べ方。unified は 1 列、split は前と後を左右に並べる。
|
|
7
|
+
* auto(既定)は PC の幅(1024px 以上)で split、それより狭い時は unified。サーバーで描く時と最初の 1 回は unified
|
|
8
|
+
*/
|
|
9
|
+
layout?: "unified" | "split" | "auto";
|
|
10
|
+
/** 行に一言を付けるボタンを出す。押すとその行が渡る */
|
|
11
|
+
onLineComment?: (file: DiffFile, line: DiffLine) => void;
|
|
12
|
+
/** 行の下に置く物(付いた一言、書く欄など)。無ければ null を返す */
|
|
13
|
+
renderAfterLine?: (file: DiffFile, line: DiffLine) => ReactNode;
|
|
14
|
+
/** 一言を付けるボタンの読み上げの名前。既定「<パス> の <番号> 行目に一言を付ける」。消した行は「<パス> の消した <番号> 行目に一言を付ける」 */
|
|
15
|
+
commentLabel?: (file: DiffFile, line: DiffLine) => string;
|
|
16
|
+
/** ファイルを閉じた形で始める */
|
|
17
|
+
collapsed?: boolean;
|
|
18
|
+
className?: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 差分の表示。ファイルごとに開け閉めでき、行ごとに前と後の番号、足した印と消した印を出す。行の中で変わった所には印が付く。
|
|
22
|
+
* PC の幅では前と後を左右に並べ、狭い幅では 1 列にまとめる(layout で固定もできる)。
|
|
23
|
+
* onLineComment を渡すと、行の番号が一言を付けるボタンになる(左右の時は、消した行は左、ほかは右の番号。1 列の時は 2 つの番号がまとめて 1 つのボタンになる。指で触る端末では 44px 四方以上になる)。
|
|
24
|
+
*/
|
|
25
|
+
export declare function DiffView({ files, layout, onLineComment, renderAfterLine, commentLabel, collapsed, className }: DiffViewProps): import("react").JSX.Element;
|
package/dist/DiffView.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { jsxs as _jsxs, jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useSyncExternalStore } from "react";
|
|
3
|
+
import { mediaQuery } from "./breakpoints.js";
|
|
4
|
+
import { Icon } from "./Icon.js";
|
|
5
|
+
import { diffLineKey, diffWords, pairDiffLines } from "./diff.js";
|
|
6
|
+
import { cx } from "./utils.js";
|
|
7
|
+
const statusName = { added: "新しいファイル", deleted: "消したファイル", modified: "", renamed: "名前を変えた" };
|
|
8
|
+
const typeName = { add: "足した行", del: "消した行", context: "" };
|
|
9
|
+
function count(file) {
|
|
10
|
+
let add = 0;
|
|
11
|
+
let del = 0;
|
|
12
|
+
for (const h of file.hunks)
|
|
13
|
+
for (const l of h.lines) {
|
|
14
|
+
if (l.type === "add")
|
|
15
|
+
add++;
|
|
16
|
+
else if (l.type === "del")
|
|
17
|
+
del++;
|
|
18
|
+
}
|
|
19
|
+
return { add, del };
|
|
20
|
+
}
|
|
21
|
+
function subscribeDesktop(onChange) {
|
|
22
|
+
const mq = window.matchMedia(mediaQuery.desktop);
|
|
23
|
+
mq.addEventListener("change", onChange);
|
|
24
|
+
return () => mq.removeEventListener("change", onChange);
|
|
25
|
+
}
|
|
26
|
+
const isDesktop = () => window.matchMedia(mediaQuery.desktop).matches;
|
|
27
|
+
const notDesktop = () => false;
|
|
28
|
+
/** 対になった消した行と足した行の、行の中で変わった所。行がまるごと違う時は印を付けない */
|
|
29
|
+
function wordParts(rows) {
|
|
30
|
+
const map = new Map();
|
|
31
|
+
for (const r of rows) {
|
|
32
|
+
if (!r.left || !r.right || r.left.type !== "del" || r.right.type !== "add")
|
|
33
|
+
continue;
|
|
34
|
+
const w = diffWords(r.left.text, r.right.text);
|
|
35
|
+
// 同じ所が少ない対は、別の行として扱う。片方に足しただけ、消しただけの時は印を付ける
|
|
36
|
+
const common = (w.prefix + w.suffix).trim().length;
|
|
37
|
+
const longer = Math.max(r.left.text.trim().length, r.right.text.trim().length);
|
|
38
|
+
if (common === 0 || (w.left && w.right && common * 2 < longer))
|
|
39
|
+
continue;
|
|
40
|
+
if (w.left)
|
|
41
|
+
map.set(r.left, { prefix: w.prefix, mid: w.left, suffix: w.suffix });
|
|
42
|
+
if (w.right)
|
|
43
|
+
map.set(r.right, { prefix: w.prefix, mid: w.right, suffix: w.suffix });
|
|
44
|
+
}
|
|
45
|
+
return map;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* 差分の表示。ファイルごとに開け閉めでき、行ごとに前と後の番号、足した印と消した印を出す。行の中で変わった所には印が付く。
|
|
49
|
+
* PC の幅では前と後を左右に並べ、狭い幅では 1 列にまとめる(layout で固定もできる)。
|
|
50
|
+
* onLineComment を渡すと、行の番号が一言を付けるボタンになる(左右の時は、消した行は左、ほかは右の番号。1 列の時は 2 つの番号がまとめて 1 つのボタンになる。指で触る端末では 44px 四方以上になる)。
|
|
51
|
+
*/
|
|
52
|
+
export function DiffView({ files, layout = "auto", onLineComment, renderAfterLine, commentLabel, collapsed = false, className }) {
|
|
53
|
+
const desktop = useSyncExternalStore(subscribeDesktop, isDesktop, notDesktop);
|
|
54
|
+
const split = layout === "split" || (layout === "auto" && desktop);
|
|
55
|
+
const code = (l, parts) => (_jsxs("span", { className: "ki-diff__code", children: [typeName[l.type] ? _jsxs("span", { className: "ki-sr-only", children: [typeName[l.type], " "] }) : null, parts ? _jsxs(_Fragment, { children: [parts.prefix, _jsx("mark", { className: "ki-diff__word", children: parts.mid }), parts.suffix] }) : l.text || " "] }));
|
|
56
|
+
const sign = (l) => _jsx("span", { className: "ki-diff__sign", "aria-hidden": "true", children: l.type === "add" ? "+" : l.type === "del" ? "−" : "" });
|
|
57
|
+
const labelOf = (f, l) => commentLabel
|
|
58
|
+
? commentLabel(f, l)
|
|
59
|
+
: l.newNumber !== undefined ? `${f.path} の ${l.newNumber} 行目に一言を付ける` : `${f.path} の消した ${l.oldNumber} 行目に一言を付ける`;
|
|
60
|
+
const number = (f, l, n, button) => {
|
|
61
|
+
if (!button || !onLineComment)
|
|
62
|
+
return _jsx("span", { className: "ki-diff__num", "aria-hidden": "true", children: n ?? "" });
|
|
63
|
+
return _jsx("button", { type: "button", className: "ki-diff__num ki-diff__comment", "aria-label": labelOf(f, l), onClick: () => onLineComment(f, l), children: n ?? "" });
|
|
64
|
+
};
|
|
65
|
+
return (_jsx("div", { className: cx("ki-diff", className), "data-layout": split ? "split" : "unified", "data-commentable": onLineComment ? "" : undefined, children: files.map((f) => {
|
|
66
|
+
const c = count(f);
|
|
67
|
+
const st = f.status ? statusName[f.status] : "";
|
|
68
|
+
return (_jsxs("details", { className: "ki-diff__file", open: !collapsed, children: [_jsxs("summary", { className: "ki-diff__head", children: [_jsx(Icon, { name: "chevron-right" }), _jsxs("span", { className: "ki-diff__path", children: [f.oldPath ? _jsxs(_Fragment, { children: [f.oldPath, _jsx("span", { "aria-hidden": "true", children: " \u2192 " }), _jsx("span", { className: "ki-sr-only", children: "\u304B\u3089" })] }) : null, f.path] }), st ? _jsx("span", { className: "ki-diff__status", children: st }) : null, _jsxs("span", { className: "ki-diff__count", children: [_jsxs("span", { "data-type": "add", "aria-hidden": "true", children: ["+", c.add] }), _jsxs("span", { className: "ki-sr-only", children: [c.add, " \u884C\u8DB3\u3057\u305F"] }), _jsxs("span", { "data-type": "del", "aria-hidden": "true", children: ["\u2212", c.del] }), _jsxs("span", { className: "ki-sr-only", children: [c.del, " \u884C\u6D88\u3057\u305F"] })] })] }), _jsx("div", { className: "ki-diff__body", children: f.hunks.map((h, hi) => {
|
|
69
|
+
const rows = pairDiffLines(h.lines);
|
|
70
|
+
const words = wordParts(rows);
|
|
71
|
+
return (_jsxs("div", { className: "ki-diff__hunk", children: [_jsx("div", { className: "ki-diff__hunk-head", children: h.header }), split
|
|
72
|
+
? rows.map((r) => {
|
|
73
|
+
const same = r.left === r.right;
|
|
74
|
+
const afterLeft = r.left && !same ? renderAfterLine?.(f, r.left) : null;
|
|
75
|
+
const afterRight = r.right ? renderAfterLine?.(f, r.right) : null;
|
|
76
|
+
return (_jsxs("div", { className: "ki-diff__row", children: [_jsxs("div", { className: "ki-diff__pair", children: [r.left
|
|
77
|
+
? _jsxs("div", { className: "ki-diff__line", "data-side": "left", "data-type": same ? "context" : r.left.type, "aria-hidden": same ? true : undefined, children: [number(f, r.left, r.left.oldNumber, !same), same ? _jsx("span", { className: "ki-diff__sign", "aria-hidden": "true" }) : sign(r.left), code(r.left, words.get(r.left))] })
|
|
78
|
+
: _jsx("div", { className: "ki-diff__line", "data-side": "left", "data-type": "none" }), r.right
|
|
79
|
+
? _jsxs("div", { className: "ki-diff__line", "data-side": "right", "data-type": r.right.type, children: [number(f, r.right, r.right.newNumber, true), sign(r.right), code(r.right, words.get(r.right))] })
|
|
80
|
+
: _jsx("div", { className: "ki-diff__line", "data-side": "right", "data-type": "none" })] }), afterLeft ? _jsx("div", { className: "ki-diff__after", children: afterLeft }) : null, afterRight ? _jsx("div", { className: "ki-diff__after", children: afterRight }) : null] }, `${r.left ? diffLineKey(r.left) : ""}:${r.right ? diffLineKey(r.right) : ""}`));
|
|
81
|
+
})
|
|
82
|
+
: h.lines.map((l) => {
|
|
83
|
+
const after = renderAfterLine?.(f, l);
|
|
84
|
+
return (_jsxs("div", { className: "ki-diff__row", children: [_jsxs("div", { className: "ki-diff__line", "data-type": l.type, children: [onLineComment
|
|
85
|
+
? (_jsxs("button", { type: "button", className: "ki-diff__nums ki-diff__comment", "aria-label": labelOf(f, l), onClick: () => onLineComment(f, l), children: [_jsx("span", { className: "ki-diff__num", children: l.oldNumber ?? "" }), _jsx("span", { className: "ki-diff__num", children: l.newNumber ?? "" })] }))
|
|
86
|
+
: _jsxs(_Fragment, { children: [_jsx("span", { className: "ki-diff__num", "aria-hidden": "true", children: l.oldNumber ?? "" }), _jsx("span", { className: "ki-diff__num", "aria-hidden": "true", children: l.newNumber ?? "" })] }), sign(l), code(l, words.get(l))] }), after ? _jsx("div", { className: "ki-diff__after", children: after }) : null] }, diffLineKey(l)));
|
|
87
|
+
})] }, hi));
|
|
88
|
+
}) })] }, f.oldPath ? `${f.oldPath}->${f.path}` : f.path));
|
|
89
|
+
}) }));
|
|
90
|
+
}
|
package/dist/Layout.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ export interface StackProps extends Omit<HTMLAttributes<HTMLElement>, "children"
|
|
|
17
17
|
/** 子を 1 方向に並べ、間隔をそろえる。 */
|
|
18
18
|
export declare function Stack({ direction, gap, align, justify, wrap, as: Tag, className, style, children, ...rest }: StackProps): import("react").JSX.Element;
|
|
19
19
|
export interface GridProps extends Omit<HTMLAttributes<HTMLElement>, "children"> {
|
|
20
|
-
/**
|
|
20
|
+
/** 列の数を固定する。スマホの幅(600px 未満)では 1 列になる。無ければ minItemWidth で自動 */
|
|
21
21
|
columns?: number;
|
|
22
22
|
/** columns が無いとき、1 つの最小の幅(px)。既定 240 */
|
|
23
23
|
minItemWidth?: number;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { CSSProperties, ReactNode } from "react";
|
|
2
|
+
export interface ListDetailProps {
|
|
3
|
+
/** 一覧。List や Table など */
|
|
4
|
+
list: ReactNode;
|
|
5
|
+
/** 選んだ物の詳細。無ければ empty を出す */
|
|
6
|
+
detail?: ReactNode;
|
|
7
|
+
/** 1 列の時に、一覧でなく詳細を出す。2 列の時は両方が出るので関係しない */
|
|
8
|
+
showDetail?: boolean;
|
|
9
|
+
/** 1 列で詳細を出している時の、一覧へ戻るボタン。渡した時だけ出る(2 列では出ない) */
|
|
10
|
+
onBack?: () => void;
|
|
11
|
+
backLabel?: string;
|
|
12
|
+
/** 2 列で、まだ何も選んでいない時に詳細の側に出す物 */
|
|
13
|
+
empty?: ReactNode;
|
|
14
|
+
/** 読み上げの名前。既定「一覧」と「詳細」 */
|
|
15
|
+
listLabel?: string;
|
|
16
|
+
detailLabel?: string;
|
|
17
|
+
className?: string;
|
|
18
|
+
style?: CSSProperties;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 一覧と詳細。PC(1024px 以上)と iPad の横は 2 列、スマホ(横向きも)と iPad の縦は 1 列で、showDetail で片方を出す。
|
|
22
|
+
* 切り替えは CSS だけで決まるので、幅が変わっても選んだ物はそのまま残る。
|
|
23
|
+
*/
|
|
24
|
+
export declare function ListDetail({ list, detail, showDetail, onBack, backLabel, empty, listLabel, detailLabel, className, style }: ListDetailProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Button } from "./Button.js";
|
|
3
|
+
import { cx } from "./utils.js";
|
|
4
|
+
/**
|
|
5
|
+
* 一覧と詳細。PC(1024px 以上)と iPad の横は 2 列、スマホ(横向きも)と iPad の縦は 1 列で、showDetail で片方を出す。
|
|
6
|
+
* 切り替えは CSS だけで決まるので、幅が変わっても選んだ物はそのまま残る。
|
|
7
|
+
*/
|
|
8
|
+
export function ListDetail({ list, detail, showDetail = false, onBack, backLabel = "一覧へ戻る", empty, listLabel = "一覧", detailLabel = "詳細", className, style }) {
|
|
9
|
+
return (_jsxs("div", { className: cx("ki-listdetail", className), style: style, "data-show": showDetail ? "detail" : "list", children: [_jsx("section", { className: "ki-listdetail__list", "aria-label": listLabel, children: list }), _jsxs("section", { className: "ki-listdetail__detail", "aria-label": detailLabel, children: [onBack ? _jsx("div", { className: "ki-listdetail__back", children: _jsx(Button, { variant: "quiet", onClick: onBack, children: backLabel }) }) : null, detail ?? _jsx("div", { className: "ki-listdetail__empty", children: empty })] })] }));
|
|
10
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** 幅の区切り(px)。knowidea-tokens の --breakpoint-tablet と --breakpoint-desktop と同じ値。 */
|
|
2
|
+
export declare const breakpoints: {
|
|
3
|
+
readonly tablet: 600;
|
|
4
|
+
readonly desktop: 1024;
|
|
5
|
+
};
|
|
6
|
+
export type Breakpoint = keyof typeof breakpoints;
|
|
7
|
+
/** matchMedia に渡す式。phone は 600px 未満、tablet は 600px から 1024px 未満、desktop は 1024px 以上 */
|
|
8
|
+
export declare const mediaQuery: {
|
|
9
|
+
readonly phone: `(max-width: ${number}px)`;
|
|
10
|
+
readonly tablet: `(min-width: 600px) and (max-width: ${number}px)`;
|
|
11
|
+
readonly desktop: "(min-width: 1024px)";
|
|
12
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** 幅の区切り(px)。knowidea-tokens の --breakpoint-tablet と --breakpoint-desktop と同じ値。 */
|
|
2
|
+
export const breakpoints = { tablet: 600, desktop: 1024 };
|
|
3
|
+
/** matchMedia に渡す式。phone は 600px 未満、tablet は 600px から 1024px 未満、desktop は 1024px 以上 */
|
|
4
|
+
export const mediaQuery = {
|
|
5
|
+
phone: `(max-width: ${breakpoints.tablet - 0.02}px)`,
|
|
6
|
+
tablet: `(min-width: ${breakpoints.tablet}px) and (max-width: ${breakpoints.desktop - 0.02}px)`,
|
|
7
|
+
desktop: `(min-width: ${breakpoints.desktop}px)`,
|
|
8
|
+
};
|
package/dist/diff.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export interface DiffLine {
|
|
2
|
+
/** add は足した行、del は消した行、context は前後の変わらない行 */
|
|
3
|
+
type: "add" | "del" | "context";
|
|
4
|
+
/** 前のファイルの行の番号(足した行には無い) */
|
|
5
|
+
oldNumber?: number;
|
|
6
|
+
/** 後のファイルの行の番号(消した行には無い) */
|
|
7
|
+
newNumber?: number;
|
|
8
|
+
text: string;
|
|
9
|
+
}
|
|
10
|
+
export interface DiffHunk {
|
|
11
|
+
/** @@ -1,4 +1,5 @@ の行 */
|
|
12
|
+
header: string;
|
|
13
|
+
lines: DiffLine[];
|
|
14
|
+
}
|
|
15
|
+
export interface DiffFile {
|
|
16
|
+
/** 後のパス(消したファイルは前のパス) */
|
|
17
|
+
path: string;
|
|
18
|
+
/** 名前を変えた時の前のパス */
|
|
19
|
+
oldPath?: string;
|
|
20
|
+
status?: "added" | "deleted" | "modified" | "renamed";
|
|
21
|
+
hunks: DiffHunk[];
|
|
22
|
+
}
|
|
23
|
+
/** 行を指す鍵。後の行の番号があれば n12、無ければ(消した行)o12 */
|
|
24
|
+
export declare function diffLineKey(line: DiffLine): string;
|
|
25
|
+
/** git diff(統一形式)の文字列を DiffFile の並びにする。 */
|
|
26
|
+
export declare function parseUnifiedDiff(text: string): DiffFile[];
|
|
27
|
+
export interface DiffRow {
|
|
28
|
+
/** 前の側の行。足しただけの行では無い */
|
|
29
|
+
left?: DiffLine;
|
|
30
|
+
/** 後の側の行。消しただけの行では無い */
|
|
31
|
+
right?: DiffLine;
|
|
32
|
+
}
|
|
33
|
+
/** 左右に並べる時の行の組。変わらない行は両側に、続く消した行と足した行は上から順に対にする */
|
|
34
|
+
export declare function pairDiffLines(lines: DiffLine[]): DiffRow[];
|
|
35
|
+
export interface DiffWords {
|
|
36
|
+
/** 前後で同じ頭 */
|
|
37
|
+
prefix: string;
|
|
38
|
+
/** 前の行だけにある所 */
|
|
39
|
+
left: string;
|
|
40
|
+
/** 後の行だけにある所 */
|
|
41
|
+
right: string;
|
|
42
|
+
/** 前後で同じ終わり */
|
|
43
|
+
suffix: string;
|
|
44
|
+
}
|
|
45
|
+
/** 行の中で変わった所。頭と終わりの同じ所を除いた残り。字は書記素でなくコードポイントで数える */
|
|
46
|
+
export declare function diffWords(before: string, after: string): DiffWords;
|
package/dist/diff.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// 差分の読み取りと組み方。React に依らないので、サーバーでも使える
|
|
2
|
+
/** 行を指す鍵。後の行の番号があれば n12、無ければ(消した行)o12 */
|
|
3
|
+
export function diffLineKey(line) {
|
|
4
|
+
return line.newNumber !== undefined ? `n${line.newNumber}` : `o${line.oldNumber}`;
|
|
5
|
+
}
|
|
6
|
+
const escapes = { a: 7, b: 8, t: 9, n: 10, v: 11, f: 12, r: 13, '"': 34, "\\": 92 };
|
|
7
|
+
/** git が引用符で囲んだパス("a/\346\227\245.txt")を元の文字に戻す。囲んでいなければそのまま返す */
|
|
8
|
+
function unquotePath(p) {
|
|
9
|
+
if (p.length < 2 || !p.startsWith('"') || !p.endsWith('"'))
|
|
10
|
+
return p;
|
|
11
|
+
const body = p.slice(1, -1);
|
|
12
|
+
const bytes = [];
|
|
13
|
+
const enc = new TextEncoder();
|
|
14
|
+
for (let i = 0; i < body.length; i++) {
|
|
15
|
+
if (body[i] !== "\\") {
|
|
16
|
+
const ch = String.fromCodePoint(body.codePointAt(i));
|
|
17
|
+
bytes.push(...enc.encode(ch));
|
|
18
|
+
i += ch.length - 1;
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
const oct = body.slice(i + 1, i + 4);
|
|
22
|
+
if (/^[0-3][0-7]{2}$/.test(oct)) {
|
|
23
|
+
bytes.push(parseInt(oct, 8));
|
|
24
|
+
i += 3;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const code = escapes[body[i + 1] ?? ""];
|
|
28
|
+
if (code !== undefined) {
|
|
29
|
+
bytes.push(code);
|
|
30
|
+
i += 1;
|
|
31
|
+
}
|
|
32
|
+
else
|
|
33
|
+
bytes.push(92);
|
|
34
|
+
}
|
|
35
|
+
return new TextDecoder().decode(new Uint8Array(bytes));
|
|
36
|
+
}
|
|
37
|
+
const quoted = String.raw `"(?:[^"\\]|\\.)*"`;
|
|
38
|
+
const headerPaths = new RegExp(`^(${quoted}|a/.+) (${quoted}|b/.+)$`);
|
|
39
|
+
/** diff --git の行の、前と後のパス。囲んでいない名前に空白がある時は、前と後が同じ名前の形を先に試す */
|
|
40
|
+
function gitPaths(rest) {
|
|
41
|
+
if (!rest.startsWith('"') && !rest.endsWith('"')) {
|
|
42
|
+
const half = (rest.length - 1) / 2;
|
|
43
|
+
if (Number.isInteger(half) && rest[half] === " " && rest.startsWith("a/") && rest.slice(half + 1).startsWith("b/") && rest.slice(2, half) === rest.slice(half + 3)) {
|
|
44
|
+
return [rest.slice(2, half), rest.slice(half + 3)];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const m = rest.match(headerPaths);
|
|
48
|
+
return m ? [strip(unquotePath(m[1])), strip(unquotePath(m[2]))] : undefined;
|
|
49
|
+
}
|
|
50
|
+
const strip = (p) => p.replace(/^[ab]\//, "");
|
|
51
|
+
/** --- と +++ の行のパス。空白の入った名前に git が付ける終わりのタブを除く */
|
|
52
|
+
const sidePath = (raw) => strip(unquotePath(raw.slice(4).replace(/\t$/, "")));
|
|
53
|
+
/** git diff(統一形式)の文字列を DiffFile の並びにする。 */
|
|
54
|
+
export function parseUnifiedDiff(text) {
|
|
55
|
+
const files = [];
|
|
56
|
+
let file;
|
|
57
|
+
let hunk;
|
|
58
|
+
let o = 0;
|
|
59
|
+
let n = 0;
|
|
60
|
+
// 見出しに書かれた、まとまりの残りの行数。頭の空白が落ちた空の行を、変わらない行として数えるのに使う
|
|
61
|
+
let oLeft = 0;
|
|
62
|
+
let nLeft = 0;
|
|
63
|
+
for (const raw of text.replace(/\r\n/g, "\n").split("\n")) {
|
|
64
|
+
if (raw.startsWith("diff --git ")) {
|
|
65
|
+
const m = gitPaths(raw.slice(11));
|
|
66
|
+
file = { path: m ? m[1] : raw.slice(11), status: "modified", hunks: [] };
|
|
67
|
+
if (m && m[0] !== m[1]) {
|
|
68
|
+
file.oldPath = m[0];
|
|
69
|
+
file.status = "renamed";
|
|
70
|
+
}
|
|
71
|
+
files.push(file);
|
|
72
|
+
hunk = undefined;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (!file || (!hunk && raw.startsWith("--- "))) {
|
|
76
|
+
if (raw.startsWith("--- ")) {
|
|
77
|
+
if (!file) {
|
|
78
|
+
file = { path: "", status: "modified", hunks: [] };
|
|
79
|
+
files.push(file);
|
|
80
|
+
}
|
|
81
|
+
if (raw === "--- /dev/null")
|
|
82
|
+
file.status = "added";
|
|
83
|
+
else if (!file.path)
|
|
84
|
+
file.path = sidePath(raw);
|
|
85
|
+
}
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (!hunk && raw.startsWith("+++ ")) {
|
|
89
|
+
if (raw === "+++ /dev/null")
|
|
90
|
+
file.status = "deleted";
|
|
91
|
+
else
|
|
92
|
+
file.path = sidePath(raw);
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (!hunk && /^(new|deleted) file mode/.test(raw)) {
|
|
96
|
+
file.status = raw.startsWith("new") ? "added" : "deleted";
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const h = raw.match(/^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/);
|
|
100
|
+
if (h) {
|
|
101
|
+
o = Number(h[1]);
|
|
102
|
+
n = Number(h[3]);
|
|
103
|
+
oLeft = h[2] === undefined ? 1 : Number(h[2]);
|
|
104
|
+
nLeft = h[4] === undefined ? 1 : Number(h[4]);
|
|
105
|
+
hunk = { header: raw, lines: [] };
|
|
106
|
+
file.hunks.push(hunk);
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (!hunk)
|
|
110
|
+
continue;
|
|
111
|
+
if (raw.startsWith("+")) {
|
|
112
|
+
hunk.lines.push({ type: "add", newNumber: n++, text: raw.slice(1) });
|
|
113
|
+
nLeft--;
|
|
114
|
+
}
|
|
115
|
+
else if (raw.startsWith("-")) {
|
|
116
|
+
hunk.lines.push({ type: "del", oldNumber: o++, text: raw.slice(1) });
|
|
117
|
+
oLeft--;
|
|
118
|
+
}
|
|
119
|
+
else if (raw.startsWith(" ") || (raw === "" && oLeft > 0 && nLeft > 0)) {
|
|
120
|
+
hunk.lines.push({ type: "context", oldNumber: o++, newNumber: n++, text: raw.slice(1) });
|
|
121
|
+
oLeft--;
|
|
122
|
+
nLeft--;
|
|
123
|
+
}
|
|
124
|
+
// "" と、まとまりの外の空の行は飛ばす
|
|
125
|
+
}
|
|
126
|
+
return files;
|
|
127
|
+
}
|
|
128
|
+
/** 左右に並べる時の行の組。変わらない行は両側に、続く消した行と足した行は上から順に対にする */
|
|
129
|
+
export function pairDiffLines(lines) {
|
|
130
|
+
const rows = [];
|
|
131
|
+
let dels = [];
|
|
132
|
+
let adds = [];
|
|
133
|
+
const flush = () => {
|
|
134
|
+
for (let i = 0; i < Math.max(dels.length, adds.length); i++)
|
|
135
|
+
rows.push({ left: dels[i], right: adds[i] });
|
|
136
|
+
dels = [];
|
|
137
|
+
adds = [];
|
|
138
|
+
};
|
|
139
|
+
for (const l of lines) {
|
|
140
|
+
if (l.type === "del") {
|
|
141
|
+
if (adds.length)
|
|
142
|
+
flush();
|
|
143
|
+
dels.push(l);
|
|
144
|
+
}
|
|
145
|
+
else if (l.type === "add")
|
|
146
|
+
adds.push(l);
|
|
147
|
+
else {
|
|
148
|
+
flush();
|
|
149
|
+
rows.push({ left: l, right: l });
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
flush();
|
|
153
|
+
return rows;
|
|
154
|
+
}
|
|
155
|
+
/** 行の中で変わった所。頭と終わりの同じ所を除いた残り。字は書記素でなくコードポイントで数える */
|
|
156
|
+
export function diffWords(before, after) {
|
|
157
|
+
const a = Array.from(before);
|
|
158
|
+
const b = Array.from(after);
|
|
159
|
+
const max = Math.min(a.length, b.length);
|
|
160
|
+
let p = 0;
|
|
161
|
+
while (p < max && a[p] === b[p])
|
|
162
|
+
p++;
|
|
163
|
+
let s = 0;
|
|
164
|
+
while (s < max - p && a[a.length - 1 - s] === b[b.length - 1 - s])
|
|
165
|
+
s++;
|
|
166
|
+
return {
|
|
167
|
+
prefix: a.slice(0, p).join(""),
|
|
168
|
+
left: a.slice(p, a.length - s).join(""),
|
|
169
|
+
right: b.slice(p, b.length - s).join(""),
|
|
170
|
+
suffix: a.slice(a.length - s).join(""),
|
|
171
|
+
};
|
|
172
|
+
}
|
package/dist/editor.d.ts
ADDED
package/dist/editor.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type EditorLanguage = "typescript" | "tsx" | "javascript" | "jsx" | "css" | "markdown" | "json" | "text";
|
|
2
|
+
/** パスの終わり(拡張子)から、エディタの言語を決める。知らない物は text */
|
|
3
|
+
export declare function editorLanguageOfPath(path: string): EditorLanguage;
|
|
4
|
+
/** 行と列(どちらも 1 から。列は UTF-16 の単位)を、文字の位置(0 から)にする。外にはみ出す時は中に収める */
|
|
5
|
+
export declare function editorOffset(text: string, line: number, column: number): number;
|
|
6
|
+
/** 文字の位置(0 から)を、行と列(どちらも 1 から)にする */
|
|
7
|
+
export declare function editorPosition(text: string, offset: number): {
|
|
8
|
+
line: number;
|
|
9
|
+
column: number;
|
|
10
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// エディタの、CodeMirror に依らない所。言語の決め方と、行と列と文字の位置の行き来
|
|
2
|
+
const byExt = {
|
|
3
|
+
ts: "typescript", mts: "typescript", cts: "typescript", tsx: "tsx",
|
|
4
|
+
js: "javascript", mjs: "javascript", cjs: "javascript", jsx: "jsx",
|
|
5
|
+
css: "css", md: "markdown", markdown: "markdown", json: "json", jsonc: "json",
|
|
6
|
+
};
|
|
7
|
+
/** パスの終わり(拡張子)から、エディタの言語を決める。知らない物は text */
|
|
8
|
+
export function editorLanguageOfPath(path) {
|
|
9
|
+
const name = path.slice(path.lastIndexOf("/") + 1);
|
|
10
|
+
const dot = name.lastIndexOf(".");
|
|
11
|
+
if (dot <= 0)
|
|
12
|
+
return "text";
|
|
13
|
+
return byExt[name.slice(dot + 1).toLowerCase()] ?? "text";
|
|
14
|
+
}
|
|
15
|
+
/** 行と列(どちらも 1 から。列は UTF-16 の単位)を、文字の位置(0 から)にする。外にはみ出す時は中に収める */
|
|
16
|
+
export function editorOffset(text, line, column) {
|
|
17
|
+
let start = 0;
|
|
18
|
+
for (let l = 1; l < line; l++) {
|
|
19
|
+
const next = text.indexOf("\n", start);
|
|
20
|
+
if (next < 0)
|
|
21
|
+
return text.length;
|
|
22
|
+
start = next + 1;
|
|
23
|
+
}
|
|
24
|
+
const end = text.indexOf("\n", start);
|
|
25
|
+
const length = (end < 0 ? text.length : end) - start;
|
|
26
|
+
return start + Math.min(Math.max(column - 1, 0), length);
|
|
27
|
+
}
|
|
28
|
+
/** 文字の位置(0 から)を、行と列(どちらも 1 から)にする */
|
|
29
|
+
export function editorPosition(text, offset) {
|
|
30
|
+
const at = Math.min(Math.max(offset, 0), text.length);
|
|
31
|
+
let line = 1;
|
|
32
|
+
let start = 0;
|
|
33
|
+
for (let next = text.indexOf("\n"); next >= 0 && next < at; next = text.indexOf("\n", start)) {
|
|
34
|
+
line++;
|
|
35
|
+
start = next + 1;
|
|
36
|
+
}
|
|
37
|
+
return { line, column: at - start + 1 };
|
|
38
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -55,3 +55,10 @@ export { BottomNavigation, type BottomNavigationProps } from "./BottomNavigation
|
|
|
55
55
|
export type { NavItem } from "./NavItems.js";
|
|
56
56
|
export { ThemeProvider, createTheme, type ThemeProviderProps, type ThemeOptions, type ThemeWarning } from "./ThemeProvider.js";
|
|
57
57
|
export { formatNumber } from "./utils.js";
|
|
58
|
+
export { AppShell, type AppShellProps } from "./AppShell.js";
|
|
59
|
+
export { ListDetail, type ListDetailProps } from "./ListDetail.js";
|
|
60
|
+
export { DiffView, type DiffViewProps } from "./DiffView.js";
|
|
61
|
+
export { editorLanguageOfPath, editorOffset, editorPosition, type EditorLanguage } from "./editorText.js";
|
|
62
|
+
export { parseUnifiedDiff, diffLineKey, pairDiffLines, diffWords, type DiffFile, type DiffHunk, type DiffLine, type DiffRow, type DiffWords } from "./diff.js";
|
|
63
|
+
export { Conversation, type ConversationProps, type ConversationMessage } from "./Conversation.js";
|
|
64
|
+
export { breakpoints, mediaQuery, type Breakpoint } from "./breakpoints.js";
|
package/dist/index.js
CHANGED
|
@@ -54,3 +54,11 @@ export { Drawer } from "./Drawer.js";
|
|
|
54
54
|
export { BottomNavigation } from "./BottomNavigation.js";
|
|
55
55
|
export { ThemeProvider, createTheme } from "./ThemeProvider.js";
|
|
56
56
|
export { formatNumber } from "./utils.js";
|
|
57
|
+
export { AppShell } from "./AppShell.js";
|
|
58
|
+
export { ListDetail } from "./ListDetail.js";
|
|
59
|
+
export { DiffView } from "./DiffView.js";
|
|
60
|
+
// エディタの本体(CodeEditor)は knowidea-ui/editor から。CodeMirror に依らない所だけ、ここからも出す
|
|
61
|
+
export { editorLanguageOfPath, editorOffset, editorPosition } from "./editorText.js";
|
|
62
|
+
export { parseUnifiedDiff, diffLineKey, pairDiffLines, diffWords } from "./diff.js";
|
|
63
|
+
export { Conversation } from "./Conversation.js";
|
|
64
|
+
export { breakpoints, mediaQuery } from "./breakpoints.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "knowidea-ui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "KNOW IDEA UI の React の部品(Base UI が土台)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -22,17 +22,32 @@
|
|
|
22
22
|
"types": "./dist/index.d.ts",
|
|
23
23
|
"default": "./dist/index.js"
|
|
24
24
|
},
|
|
25
|
+
"./editor": {
|
|
26
|
+
"types": "./dist/editor.d.ts",
|
|
27
|
+
"default": "./dist/editor.js"
|
|
28
|
+
},
|
|
25
29
|
"./package.json": "./package.json"
|
|
26
30
|
},
|
|
27
31
|
"dependencies": {
|
|
28
|
-
"@base-ui/react": "1.9.0"
|
|
32
|
+
"@base-ui/react": "1.9.0",
|
|
33
|
+
"@codemirror/autocomplete": "6.20.4",
|
|
34
|
+
"@codemirror/commands": "6.11.1",
|
|
35
|
+
"@codemirror/lang-css": "6.3.1",
|
|
36
|
+
"@codemirror/lang-javascript": "6.2.5",
|
|
37
|
+
"@codemirror/lang-json": "6.0.2",
|
|
38
|
+
"@codemirror/lang-markdown": "6.5.2",
|
|
39
|
+
"@codemirror/language": "^6.13.1",
|
|
40
|
+
"@codemirror/lint": "6.9.7",
|
|
41
|
+
"@codemirror/state": "^6.7.6",
|
|
42
|
+
"@codemirror/view": "^6.43.15",
|
|
43
|
+
"@lezer/highlight": "1.2.5"
|
|
29
44
|
},
|
|
30
45
|
"peerDependencies": {
|
|
31
46
|
"react": "^19.0.0",
|
|
32
47
|
"react-dom": "^19.0.0",
|
|
33
|
-
"knowidea-css": "^0.
|
|
34
|
-
"knowidea-
|
|
35
|
-
"knowidea-
|
|
48
|
+
"knowidea-css": "^0.11.0",
|
|
49
|
+
"knowidea-tokens": "^0.11.0",
|
|
50
|
+
"knowidea-icons": "^0.11.0"
|
|
36
51
|
},
|
|
37
52
|
"devDependencies": {
|
|
38
53
|
"@types/react": "19.3.0",
|
|
@@ -40,9 +55,9 @@
|
|
|
40
55
|
"react": "19.3.0",
|
|
41
56
|
"react-dom": "19.3.0",
|
|
42
57
|
"typescript": "5.9.3",
|
|
43
|
-
"knowidea-
|
|
44
|
-
"knowidea-
|
|
45
|
-
"knowidea-
|
|
58
|
+
"knowidea-icons": "0.11.0",
|
|
59
|
+
"knowidea-css": "0.11.0",
|
|
60
|
+
"knowidea-tokens": "0.11.0"
|
|
46
61
|
},
|
|
47
62
|
"files": [
|
|
48
63
|
"dist",
|
|
@@ -51,6 +66,7 @@
|
|
|
51
66
|
],
|
|
52
67
|
"sideEffects": false,
|
|
53
68
|
"scripts": {
|
|
54
|
-
"build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.json"
|
|
69
|
+
"build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.json",
|
|
70
|
+
"test": "node --test test/*.test.mjs"
|
|
55
71
|
}
|
|
56
72
|
}
|