@dxos/react-ui-chat 0.8.4-main.1da679c
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/LICENSE +8 -0
- package/README.md +1 -0
- package/dist/lib/browser/index.mjs +351 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/node-esm/index.mjs +353 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/types/src/components/ChatDialog/ChatDialog.d.ts +34 -0
- package/dist/types/src/components/ChatDialog/ChatDialog.d.ts.map +1 -0
- package/dist/types/src/components/ChatDialog/ChatDialog.stories.d.ts +39 -0
- package/dist/types/src/components/ChatDialog/ChatDialog.stories.d.ts.map +1 -0
- package/dist/types/src/components/ChatDialog/index.d.ts +2 -0
- package/dist/types/src/components/ChatDialog/index.d.ts.map +1 -0
- package/dist/types/src/components/ChatEditor/ChatEditor.d.ts +21 -0
- package/dist/types/src/components/ChatEditor/ChatEditor.d.ts.map +1 -0
- package/dist/types/src/components/ChatEditor/ChatEditor.stories.d.ts +26 -0
- package/dist/types/src/components/ChatEditor/ChatEditor.stories.d.ts.map +1 -0
- package/dist/types/src/components/ChatEditor/index.d.ts +3 -0
- package/dist/types/src/components/ChatEditor/index.d.ts.map +1 -0
- package/dist/types/src/components/ChatEditor/references.d.ts +30 -0
- package/dist/types/src/components/ChatEditor/references.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +3 -0
- package/dist/types/src/components/index.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +3 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/translations.d.ts +7 -0
- package/dist/types/src/translations.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +75 -0
- package/src/components/ChatDialog/ChatDialog.stories.tsx +66 -0
- package/src/components/ChatDialog/ChatDialog.tsx +193 -0
- package/src/components/ChatDialog/index.ts +5 -0
- package/src/components/ChatEditor/ChatEditor.stories.tsx +97 -0
- package/src/components/ChatEditor/ChatEditor.tsx +89 -0
- package/src/components/ChatEditor/index.ts +7 -0
- package/src/components/ChatEditor/references.ts +175 -0
- package/src/components/index.ts +6 -0
- package/src/index.ts +6 -0
- package/src/translations.ts +15 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type CompletionResult, autocompletion, completionKeymap } from '@codemirror/autocomplete';
|
|
6
|
+
import { type Extension, RangeSet } from '@codemirror/state';
|
|
7
|
+
import {
|
|
8
|
+
Decoration,
|
|
9
|
+
type DecorationSet,
|
|
10
|
+
EditorView,
|
|
11
|
+
ViewPlugin,
|
|
12
|
+
type ViewUpdate,
|
|
13
|
+
WidgetType,
|
|
14
|
+
keymap,
|
|
15
|
+
} from '@codemirror/view';
|
|
16
|
+
|
|
17
|
+
import { Mutex } from '@dxos/async';
|
|
18
|
+
import { invariant } from '@dxos/invariant';
|
|
19
|
+
|
|
20
|
+
// TODO(dmaretskyi): Consider adding details renderer for when you hover over the reference.
|
|
21
|
+
export type ReferenceData = {
|
|
22
|
+
uri: string;
|
|
23
|
+
label: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export interface ReferencesProvider {
|
|
27
|
+
getReferences({ query }: { query: string }): Promise<ReferenceData[]>;
|
|
28
|
+
resolveReference({ uri }: { uri: string }): Promise<ReferenceData | null>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type ReferencesOptions = {
|
|
32
|
+
provider: ReferencesProvider;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @default '@'
|
|
36
|
+
*/
|
|
37
|
+
triggerCharacter?: string;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Prevent the autocomplete from closing when the user blurs the editor.
|
|
41
|
+
* @default false
|
|
42
|
+
*/
|
|
43
|
+
debug?: boolean;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Lookup object references.
|
|
48
|
+
*/
|
|
49
|
+
export const references = ({ provider, triggerCharacter = '@', debug = false }: ReferencesOptions): Extension => {
|
|
50
|
+
invariant(triggerCharacter.length === 1);
|
|
51
|
+
|
|
52
|
+
const decorationField = ViewPlugin.fromClass(
|
|
53
|
+
class ReferenceView {
|
|
54
|
+
// TODO(burdon): Why?
|
|
55
|
+
private _mutex = new Mutex();
|
|
56
|
+
|
|
57
|
+
decorations: DecorationSet = Decoration.set([]);
|
|
58
|
+
|
|
59
|
+
constructor(view: EditorView) {
|
|
60
|
+
queueMicrotask(async () => {
|
|
61
|
+
const guard = await this._mutex.acquire();
|
|
62
|
+
try {
|
|
63
|
+
this.decorations = await this._computeDecorations(view);
|
|
64
|
+
} finally {
|
|
65
|
+
guard.release();
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
update(update: ViewUpdate) {
|
|
71
|
+
if (update.docChanged) {
|
|
72
|
+
queueMicrotask(async () => {
|
|
73
|
+
const guard = await this._mutex.acquire();
|
|
74
|
+
try {
|
|
75
|
+
this.decorations = await this._computeDecorations(update.view);
|
|
76
|
+
} finally {
|
|
77
|
+
guard.release();
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private async _computeDecorations(view: EditorView): Promise<DecorationSet> {
|
|
84
|
+
const text = view.state.doc.toString();
|
|
85
|
+
const references = text.matchAll(new RegExp(`${triggerCharacter}[a-zA-Z0-9@:]+\\s`, 'g'));
|
|
86
|
+
|
|
87
|
+
const decorations = [];
|
|
88
|
+
for (const match of references) {
|
|
89
|
+
const reference = match[0];
|
|
90
|
+
const uri = reference.slice(1, -1);
|
|
91
|
+
const data = await provider.resolveReference({ uri });
|
|
92
|
+
if (data) {
|
|
93
|
+
decorations.push(
|
|
94
|
+
Decoration.replace({
|
|
95
|
+
widget: new ReferenceWidget(data),
|
|
96
|
+
}).range(match.index!, match.index! + reference.length),
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return Decoration.set(decorations);
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
decorations: (v) => v.decorations,
|
|
106
|
+
provide: (plugin) => [
|
|
107
|
+
EditorView.atomicRanges.of(
|
|
108
|
+
(view): DecorationSet => view.plugin(decorationField)?.decorations ?? RangeSet.empty,
|
|
109
|
+
),
|
|
110
|
+
],
|
|
111
|
+
},
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
return [
|
|
115
|
+
decorationField,
|
|
116
|
+
|
|
117
|
+
EditorView.theme({
|
|
118
|
+
'.cm-reference-pill': {
|
|
119
|
+
borderRadius: '0.25rem',
|
|
120
|
+
borderWidth: '1px',
|
|
121
|
+
marginRight: '0.25rem',
|
|
122
|
+
marginLeft: '0.25rem',
|
|
123
|
+
},
|
|
124
|
+
}),
|
|
125
|
+
|
|
126
|
+
autocompletion({
|
|
127
|
+
tooltipClass: () => 'shadow rounded',
|
|
128
|
+
activateOnTyping: true,
|
|
129
|
+
aboveCursor: true,
|
|
130
|
+
closeOnBlur: !debug,
|
|
131
|
+
override: [
|
|
132
|
+
async (context): Promise<CompletionResult | null> => {
|
|
133
|
+
const match = context.matchBefore(new RegExp(`${triggerCharacter}[a-zA-Z0-9]+`));
|
|
134
|
+
if (!match || match?.to === match?.from) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const query = match.text.slice(1);
|
|
139
|
+
const references = await provider.getReferences({ query });
|
|
140
|
+
return {
|
|
141
|
+
from: match.from,
|
|
142
|
+
filter: false,
|
|
143
|
+
options: references.map((reference) => ({
|
|
144
|
+
label: reference.label,
|
|
145
|
+
apply: `${triggerCharacter}${reference.uri} `,
|
|
146
|
+
})),
|
|
147
|
+
};
|
|
148
|
+
},
|
|
149
|
+
],
|
|
150
|
+
}),
|
|
151
|
+
|
|
152
|
+
keymap.of(completionKeymap),
|
|
153
|
+
];
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
class ReferenceWidget extends WidgetType {
|
|
157
|
+
constructor(private data: ReferenceData) {
|
|
158
|
+
super();
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
override toDOM(): HTMLSpanElement {
|
|
162
|
+
const span = document.createElement('span');
|
|
163
|
+
span.textContent = `@ ${this.data.label}`;
|
|
164
|
+
span.className = 'cm-reference-pill';
|
|
165
|
+
return span;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
override eq(other: ReferenceWidget): boolean {
|
|
169
|
+
return other.data.uri === this.data.uri;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
override ignoreEvent(): boolean {
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Resource } from '@dxos/react-ui';
|
|
6
|
+
|
|
7
|
+
export const translationKey = 'react-ui-chat';
|
|
8
|
+
|
|
9
|
+
export const translations = [
|
|
10
|
+
{
|
|
11
|
+
'en-US': {
|
|
12
|
+
[translationKey]: {},
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
] as const satisfies Resource[];
|