дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 17:47:08 +02:00
fix(editor): show markup style colors once the background parse reaches them (#8237)
In markup a color literal gets a swatch only inside a style, which the syntax tree tells, but the color gutter recomputed only on document changes: a parse that finished after the editor opened left those lines without swatches. The gutter and its width now also update when a markup document's tree advances. Fixes the color preview e2e and the Vue gutter test failing on CI.
Цей коміт міститься в:
1 джерело
13b8024588
коміт
e9480d690c
2 files changed
+38
-5
No files matched your search
@@ -141,7 +141,7 @@ it("samples a screen color and offers it again as a recent color", async () => {
|
||||
expect(view.state.doc.toString()).toBe("color: #4c78ce;");
|
||||
});
|
||||
|
||||
it("ignores color-like Vue template text while previewing embedded styles", () => {
|
||||
it("ignores color-like Vue template text while previewing embedded styles", async () => {
|
||||
view = new EditorView({
|
||||
state: EditorState.create({
|
||||
doc: '<template>\n<h1>Issue #1811: ConfigProvider z-index</h1>\n<div style="color: #abc">Text</div>\n</template>\n<style>\nh1 { color: #ff0080; }\n</style>',
|
||||
@@ -152,8 +152,30 @@ it("ignores color-like Vue template text while previewing embedded styles", () =
|
||||
}),
|
||||
parent: document.body,
|
||||
});
|
||||
const swatches = [...view.dom.querySelectorAll(".cm-openidea-gutter-colors .cm-gutterElement span[title]")];
|
||||
expect(swatches.map((swatch) => swatch.getAttribute("title"))).toEqual(["#abc", "#ff0080"]);
|
||||
// Shown once the background parse reaches the styles (it may finish after the editor opened).
|
||||
await vi.waitFor(() => {
|
||||
const swatches = [...view!.dom.querySelectorAll(".cm-openidea-gutter-colors .cm-gutterElement span[title]")];
|
||||
expect(swatches.map((swatch) => swatch.getAttribute("title"))).toEqual(["#abc", "#ff0080"]);
|
||||
});
|
||||
});
|
||||
|
||||
it("shows a markup file's style colors once a parse that outlasted opening the editor finishes", async () => {
|
||||
const styles = Array.from({ length: 4000 }, (_, i) => `.c${i} { margin: ${i}px; }`).join("\n");
|
||||
view = new EditorView({
|
||||
state: EditorState.create({
|
||||
doc: `<style>\n${styles}\n.last { color: #ff0080; }\n</style>`,
|
||||
extensions: [
|
||||
vue(),
|
||||
colorPreviewGutter({ doc: 1, path: "/p/a.vue", language: "vue", uiMode: null, readOnly: false }),
|
||||
],
|
||||
}),
|
||||
parent: document.body,
|
||||
});
|
||||
view.scrollDOM.style.height = "100px";
|
||||
view.dispatch({ effects: EditorView.scrollIntoView(view.state.doc.length) });
|
||||
await vi.waitFor(() =>
|
||||
expect(view!.dom.querySelector('.cm-openidea-gutter-colors span[title="#ff0080"]')).not.toBeNull(),
|
||||
);
|
||||
});
|
||||
|
||||
it("ignores color-like HTML text while previewing inline styles", () => {
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* its declaration, and modern function syntaxes show the resolved value without a picker.
|
||||
*/
|
||||
import { Facet, StateEffect, StateField, type EditorState, type Extension, type RangeSet } from "@codemirror/state";
|
||||
import { Decoration, EditorView, GutterMarker, WidgetType, gutter } from "@codemirror/view";
|
||||
import { Decoration, EditorView, GutterMarker, WidgetType, gutter, type ViewUpdate } from "@codemirror/view";
|
||||
import { isolateHistory } from "@codemirror/commands";
|
||||
import { syntaxTree } from "@codemirror/language";
|
||||
import type { EditorDocContext } from "@/editor/api";
|
||||
@@ -320,6 +320,15 @@ function swatchCount(view: EditorView, held: number): number {
|
||||
return held;
|
||||
}
|
||||
|
||||
/**
|
||||
* In markup (HTML, Vue…) a literal counts only inside a style, which the syntax tree tells: the
|
||||
* background parse finishing after the editor opened changes the swatches of lines it reached.
|
||||
*/
|
||||
function parsedFurther(update: ViewUpdate): boolean {
|
||||
const ctx = update.state.facet(colorContext);
|
||||
return !!ctx && isMarkupLanguage(ctx.language) && syntaxTree(update.startState) !== syntaxTree(update.state);
|
||||
}
|
||||
|
||||
export const colorGutter = gutter({
|
||||
class: `${styles.gutter} cm-openidea-gutter-colors`,
|
||||
markers: (view) => view.state.facet(colorMarkers).map((markers) => markers(view)),
|
||||
@@ -330,7 +339,8 @@ export const colorGutter = gutter({
|
||||
!update.viewportChanged &&
|
||||
!update.geometryChanged &&
|
||||
update.startState.facet(colorContext) === update.state.facet(colorContext) &&
|
||||
!update.transactions.some((tr) => tr.effects.length > 0)
|
||||
!update.transactions.some((tr) => tr.effects.length > 0) &&
|
||||
!parsedFurther(update)
|
||||
)
|
||||
return previous;
|
||||
const held = previous instanceof ColorSpacer ? previous.count : 0;
|
||||
@@ -345,6 +355,7 @@ export const colorGutter = gutter({
|
||||
},
|
||||
lineMarkerChange: (update) =>
|
||||
update.docChanged ||
|
||||
parsedFurther(update) ||
|
||||
update.startState.facet(colorContext) !== update.state.facet(colorContext) ||
|
||||
update.startState.readOnly !== update.state.readOnly ||
|
||||
update.transactions.some((tr) => tr.effects.some((e) => e.is(setVars))),
|
||||
|
||||
Reference in new issue
Заблокувати користувача