mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-09 14:03:33 +09:00
fix: Fix expression editor decoration (#4261)
## Description 2 bugs ### Click on test broke decoration   ### Symbols after decoration broke decoration   ### Would be good to improve Error messages: <img width="534" alt="image" src="https://github.com/user-attachments/assets/a42509e6-8e41-40d3-b1ae-ad739487c68e"> ## Steps for reproduction 1. click button 2. expect xyz ## Code Review - [ ] hi @kof, I need you to do - conceptual review (architecture, feature-correctness) - detailed review (read every line) - test it on preview ## Before requesting a review - [ ] made a self-review - [ ] added inline comments where things may be not obvious (the "why", not "what") ## Before merging - [ ] tested locally and on preview environment (preview dev login: 5de6) - [ ] updated [test cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md) document - [ ] added tests - [ ] if any new env variables are added, added them to `.env` file
This commit is contained in:
1 parent
ada2b47042
commit
4ee045fce9
1 file changed
+24
-5
@@ -271,12 +271,31 @@ class VariableWidget extends WidgetType {
|
||||
|
||||
const variableMatcher = new MatchDecorator({
|
||||
regexp: /(\$ws\$dataSource\$\w+)/g,
|
||||
decoration: (match, view) => {
|
||||
|
||||
decorate: (add, from, _to, match, view) => {
|
||||
const startPos = match.index;
|
||||
|
||||
const name = match[1];
|
||||
const [data] = view.state.facet(VariablesData);
|
||||
return Decoration.replace({
|
||||
widget: new VariableWidget(data.aliases.get(name) ?? name),
|
||||
});
|
||||
const [{ aliases }] = view.state.facet(VariablesData);
|
||||
|
||||
// The regexp may match variables not in scope, but the key problem we're solving is this:
|
||||
// We have an alias $ws$dataSource$321 -> SomeVar, which we display as '[SomeVar]' ([] means decoration in the editor).
|
||||
// If the user types a symbol (e.g., 'a') immediately after '[SomeVar]',
|
||||
// the raw text becomes $ws$dataSource$321a, but we want to display '[SomeVar]a'.
|
||||
const dataSourceId = [...aliases.keys()].find((key) => name.includes(key));
|
||||
|
||||
if (dataSourceId === undefined) {
|
||||
return;
|
||||
}
|
||||
const endPos = startPos + dataSourceId.length;
|
||||
|
||||
add(
|
||||
from,
|
||||
endPos,
|
||||
Decoration.replace({
|
||||
widget: new VariableWidget(aliases.get(dataSourceId)!),
|
||||
})
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user