diff --git a/src/web/EnrichedMarkdownText.tsx b/src/web/EnrichedMarkdownText.tsx index b0009069..e8d40307 100644 --- a/src/web/EnrichedMarkdownText.tsx +++ b/src/web/EnrichedMarkdownText.tsx @@ -1,10 +1,4 @@ -import { - useState, - useEffect, - useMemo, - Fragment, - type CSSProperties, -} from 'react'; +import { useState, useEffect, useMemo, type CSSProperties } from 'react'; import type { EnrichedMarkdownTextProps } from '../types/MarkdownTextProps.web'; import { normalizeMarkdownStyle } from '../normalizeMarkdownStyle.web'; import { @@ -18,6 +12,7 @@ import type { ASTNode, RendererCallbacks, RenderCapabilities } from './types'; import { indexTaskItems, markInlineImages } from './utils'; import { loadKaTeX } from './katex'; import type { KaTeXInstance } from './katex'; +import { ENRM_TEXT_CLASS, ENRM_SELECTION_BG_VAR } from './globalStyles'; export const EnrichedMarkdownText = ({ markdown, @@ -109,31 +104,17 @@ export const EnrichedMarkdownText = ({ ...(containerStyle as CSSProperties), ...(selectable ? undefined : { userSelect: 'none' }), ...(selectionColor - ? ({ ['--enrm-selection-bg']: selectionColor } as CSSProperties) + ? ({ [ENRM_SELECTION_BG_VAR]: selectionColor } as CSSProperties) : null), }), [containerStyle, selectable, selectionColor] ); - const selectionStyle = selectionColor ? ( - - ) : null; - if (parseError) { return ( - - {selectionStyle} -
-
{markdown}
-
-
+
+
{markdown}
+
); } @@ -143,21 +124,18 @@ export const EnrichedMarkdownText = ({ const lastIdx = children.length - 1; return ( - - {selectionStyle} -
- {children.map((child, index) => ( - - ))} -
-
+
+ {children.map((child, index) => ( + + ))} +
); }; diff --git a/src/web/globalStyles.ts b/src/web/globalStyles.ts new file mode 100644 index 00000000..53acfa4c --- /dev/null +++ b/src/web/globalStyles.ts @@ -0,0 +1,15 @@ +import { injectStyleOnce } from './injectStyle'; + +export const ENRM_TEXT_CLASS = 'enrm-text'; +export const ENRM_SELECTION_BG_VAR = '--enrm-selection-bg'; + +const RULES: ReadonlyArray = [ + [ + 'enrm-selection-style', + `.${ENRM_TEXT_CLASS} ::selection { background-color: var(${ENRM_SELECTION_BG_VAR}); }`, + ], +]; + +for (const [id, css] of RULES) { + injectStyleOnce(id, css); +} diff --git a/src/web/injectStyle.ts b/src/web/injectStyle.ts new file mode 100644 index 00000000..063eef35 --- /dev/null +++ b/src/web/injectStyle.ts @@ -0,0 +1,17 @@ +/// + +/** + * Idempotently injects a `