Skip to content

Commit

Permalink
feat(editor): use alt color for inline code sigils
Browse files Browse the repository at this point in the history
  • Loading branch information
polyrainbow committed Dec 15, 2024
1 parent bbad898 commit 0228279
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 1 deletion.
6 changes: 5 additions & 1 deletion public/assets/css/03-editor-view.css
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,10 @@ div[data-lexical-editor] {
.inline-code, .code-block {
font-family: var(--code-font);
background-color: var(--accent-color-interactible);

&::highlight(inline-code-sigil) {
color: var(--alt-content-color);
}
}

.quote-block {
Expand All @@ -595,7 +599,7 @@ div[data-lexical-editor] {
margin-inline: 0;

::highlight(quote-block-sigil) {
color: var(--alt-content-color)
color: var(--alt-content-color);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib/editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import AutoFocusPlugin from "./plugins/AutoFocusPlugin";
import { TransclusionContentGetter } from "./types/TransclusionContentGetter";
import {
highlightHeadingSigils,
highlightInlineCodeSigils,
highlightQuoteBlockSigils,
} from "./utils/highlight";
import { KeyValueNode } from "./nodes/KeyValueNode";
Expand Down Expand Up @@ -88,6 +89,7 @@ export const Editor = ({
editorState.read(() => {
highlightHeadingSigils();
highlightQuoteBlockSigils();
highlightInlineCodeSigils();
const root = $getRoot();
onChange(getSubtextFromEditor(root));
});
Expand Down
30 changes: 30 additions & 0 deletions src/lib/editor/utils/highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,33 @@ export const highlightQuoteBlockSigils = () => {
// @ts-ignore
CSS.highlights.set("quote-block-sigil", highlight);
};


export const highlightInlineCodeSigils = () => {
const inlineCodeSpans = document.querySelectorAll(
"div[data-lexical-editor] .inline-code",
);

const ranges: Range[] = [];
Array.from(inlineCodeSpans).forEach((span) => {
const textNode = span.childNodes[0];
const rangeStart = new Range();
rangeStart.setStart(textNode, 0);
rangeStart.setEnd(textNode, 1);
ranges.push(rangeStart);

const textContent = textNode.textContent;

if (textContent) {
const rangeEnd = new Range();
rangeEnd.setStart(textNode, textContent.length - 1);
rangeEnd.setEnd(textNode, textContent.length);
ranges.push(rangeEnd);
}
});

// @ts-ignore
const highlight = new Highlight(...ranges);
// @ts-ignore
CSS.highlights.set("inline-code-sigil", highlight);
};
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0228279

Please sign in to comment.