Skip to content

Commit 71f5da1

Browse files
authored
feat: remove value option and add ValueSync, add options.reset for asyncOption (#125)
Co-authored-by: stream-pipe <stream-pipe@users.noreply.github.com>
1 parent 7c394e6 commit 71f5da1

15 files changed

Lines changed: 139 additions & 59 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@coze-editor/core-plugins",
5+
"comment": "remove value option and add ValueSync, add options.reset for asyncOption",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@coze-editor/core-plugins",
10+
"email": "stream-pipe@users.noreply.github.com"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@coze-editor/core",
5+
"comment": "remove value option and add ValueSync, add options.reset for asyncOption",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@coze-editor/core",
10+
"email": "stream-pipe@users.noreply.github.com"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@coze-editor/extensions",
5+
"comment": "remove value option and add ValueSync, add options.reset for asyncOption",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@coze-editor/extensions",
10+
"email": "stream-pipe@users.noreply.github.com"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@coze-editor/preset-code-languages",
5+
"comment": "remove value option and add ValueSync, add options.reset for asyncOption",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@coze-editor/preset-code-languages",
10+
"email": "stream-pipe@users.noreply.github.com"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@coze-editor/preset-universal",
5+
"comment": "remove value option and add ValueSync, add options.reset for asyncOption",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@coze-editor/preset-universal",
10+
"email": "stream-pipe@users.noreply.github.com"
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@coze-editor/react-components",
5+
"comment": "remove value option and add ValueSync, add options.reset for asyncOption",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@coze-editor/react-components",
10+
"email": "stream-pipe@users.noreply.github.com"
11+
}

packages/text-editor/core-plugins/src/option.ts

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
// Copyright (c) 2025 coze-dev
22
// SPDX-License-Identifier: MIT
33

4-
import { FacetCombineStrategy } from '@coze-editor/utils';
54
import { placeholder as cmPlaceholder } from '@coze-editor/extension-placeholder';
6-
import {
7-
EditorView,
8-
ViewPlugin,
9-
type ViewUpdate,
10-
lineNumbers as cmLineNumbers,
11-
} from '@codemirror/view';
12-
import { EditorState, Facet, Prec } from '@codemirror/state';
5+
import { EditorView, lineNumbers as cmLineNumbers } from '@codemirror/view';
6+
import { EditorState, Prec } from '@codemirror/state';
137

148
export const fontSize = (value?: number) => {
159
if (typeof value === 'undefined') {
@@ -146,32 +140,3 @@ export const lineNumbers = (enable?: boolean) =>
146140

147141
export const lineWrapping = (enable: boolean) =>
148142
enable ? EditorView.lineWrapping : [];
149-
150-
const valueFacet = Facet.define<string, string>({
151-
combine: FacetCombineStrategy.Last,
152-
});
153-
export const valueExtension = ViewPlugin.fromClass(
154-
class {
155-
update(update: ViewUpdate) {
156-
const currentValue = update.state.doc.toString();
157-
const newValue = update.state.facet(valueFacet);
158-
159-
if (
160-
typeof newValue === 'string' &&
161-
newValue &&
162-
newValue !== currentValue
163-
) {
164-
queueMicrotask(() => {
165-
update.view.dispatch({
166-
changes: {
167-
from: 0,
168-
to: currentValue.length,
169-
insert: newValue,
170-
},
171-
});
172-
});
173-
}
174-
}
175-
},
176-
);
177-
export const value = (v: string) => valueFacet.of(v);

packages/text-editor/core/src/async-option.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function last<T>(values: readonly T[]): T {
1515
function asyncOption<Name extends string, Value>(
1616
name: Name,
1717
handler: (value: Value) => Promise<Extension>,
18+
options?: { reset?: boolean },
1819
): [ExtensionPluginSpec, OptionPluginSpec<Name, Value>] {
1920
const facet = Facet.define<Value, Value>({
2021
combine: last,
@@ -36,10 +37,20 @@ function asyncOption<Name extends string, Value>(
3637

3738
apply(view: EditorView) {
3839
const value = view.state.facet(facet);
40+
if (options?.reset === true) {
41+
queueMicrotask(() => {
42+
view.dispatch({
43+
effects: compartment.reconfigure([]),
44+
});
45+
});
46+
}
47+
3948
handler(value).then(ext => {
4049
if (view.state.facet(facet) === value) {
41-
view.dispatch({
42-
effects: compartment.reconfigure(ext),
50+
queueMicrotask(() => {
51+
view.dispatch({
52+
effects: compartment.reconfigure(ext),
53+
});
4354
});
4455
}
4556
});

packages/text-editor/dev/src/pages/highlight/examples.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function App() {
1717
return (
1818
<div>
1919
<h1>Hello World</h1>
20+
{/* hello() */}
2021
</div>
2122
);
2223
}
@@ -31,6 +32,7 @@ export default App;
3132
function App() {
3233
return (
3334
<div>
35+
{/* hello() */}
3436
<h1>Hello World</h1>
3537
</div>
3638
);
@@ -51,6 +53,7 @@ export default App;
5153
</head>
5254
<body>
5355
<h1>Hello World</h1>
56+
<!-- hello() -->
5457
</body>
5558
</html>
5659
`,
@@ -83,7 +86,7 @@ This is a markdown example.
8386
path: 'a.md',
8487
},
8588
{
86-
code: 'const a = 1;',
89+
code: 'const a = (1);',
8790
path: 'a.js',
8891
},
8992
{

packages/text-editor/dev/src/pages/highlight/index.tsx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ import universalCode from '@coze-editor/editor/preset-universal-code';
33
import autoLanguage from '@coze-editor/editor/preset-code-languages';
44
import { useState } from 'react';
55
import { examples } from './examples';
6-
import { createEditor } from '@coze-editor/editor/react';
6+
import { createEditor, ValueSync } from '@coze-editor/editor/react';
77

8-
const CodeHighlight = createEditor(
9-
[...universal, ...universalCode, ...autoLanguage],
10-
{
11-
defaultOptions: {
12-
fontSize: 15,
13-
readOnly: true,
14-
editable: false,
15-
},
16-
},
17-
);
8+
const CodeHighlight = createEditor([
9+
...universal,
10+
...universalCode,
11+
...autoLanguage,
12+
], {
13+
defaultOptions: {
14+
fontSize: 15,
15+
readOnly: true,
16+
editable: false,
17+
}
18+
})
1819

1920
const HighlightPage = () => {
2021
const [code, setCode] = useState('const a = 1;');
@@ -46,14 +47,15 @@ const HighlightPage = () => {
4647
},
4748
}}
4849
options={{
49-
value: code,
5050
path: path,
5151
activeLine: false,
5252
}}
5353
didMount={api => {
5454
console.log('didMount', api);
5555
}}
56-
/>
56+
>
57+
<ValueSync value={code}></ValueSync>
58+
</CodeHighlight>
5759
</div>
5860
);
5961
};

0 commit comments

Comments
 (0)