|
1 | | -import { config } from './config'; |
| 1 | +/** @import { Config } from './config.js' */ |
| 2 | + |
| 3 | +import { configDefaults } from './config'; |
| 4 | +import { NAMESPACE } from './constants'; |
2 | 5 | import { setupTokenHighlights } from './utils'; |
3 | 6 |
|
4 | | -export class SyntaxHighlightElement extends HTMLElement { |
| 7 | +export default class Component extends HTMLElement { |
5 | 8 | static async define(tagName = 'syntax-highlight', registry = customElements) { |
6 | 9 | if (!CSS.highlights) { |
7 | 10 | console.info('The CSS Custom Highlight API is not supported in this browser.'); |
8 | 11 | return; |
9 | 12 | } |
10 | 13 |
|
11 | 14 | if (!registry.get(tagName)) { |
12 | | - typeof config?.setup === 'function' && (await config.setup()); |
13 | | - setupTokenHighlights(config.tokenTypes, { languageTokens: config.languageTokens }); |
14 | | - registry.define(tagName, SyntaxHighlightElement); |
15 | | - return SyntaxHighlightElement; |
| 15 | + // Setup custom element |
| 16 | + typeof Component.#config?.setup === 'function' && (await Component.#config.setup()); |
| 17 | + setupTokenHighlights(Component.#config.tokenTypes, { |
| 18 | + languageTokens: Component.#config.languageTokens, |
| 19 | + }); |
| 20 | + registry.define(tagName, Component); |
| 21 | + return Component; |
16 | 22 | } |
17 | 23 | } |
18 | 24 |
|
| 25 | + /** |
| 26 | + * Configuration object with default values. |
| 27 | + * Merge defaults with global namespace config. |
| 28 | + * @type {Config} |
| 29 | + * */ |
| 30 | + static #config = Object.assign(configDefaults, window[NAMESPACE]?.config || {}); |
| 31 | + |
| 32 | + /** |
| 33 | + * Configuration object |
| 34 | + * @type {Config} |
| 35 | + */ |
| 36 | + static get config() { |
| 37 | + return Component.#config; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Modify configuration object |
| 42 | + * @param {Config} properties |
| 43 | + */ |
| 44 | + static set config(properties) { |
| 45 | + Component.#config = Object.assign(Component.#config, properties); |
| 46 | + } |
| 47 | + |
19 | 48 | #internals; |
20 | 49 | #highlights = new Set(); |
21 | 50 |
|
@@ -56,8 +85,8 @@ export class SyntaxHighlightElement extends HTMLElement { |
56 | 85 | */ |
57 | 86 | paintTokenHighlights() { |
58 | 87 | // Tokenize the text |
59 | | - const tokens = config.tokenize(this.contentElement.innerText, this.language) || []; |
60 | | - const languageTokenTypes = config.languageTokens?.[this.language] || []; |
| 88 | + const tokens = Component.#config.tokenize(this.contentElement.innerText, this.language) || []; |
| 89 | + const languageTokenTypes = Component.#config.languageTokens?.[this.language] || []; |
61 | 90 |
|
62 | 91 | // Paint highlights |
63 | 92 | let pos = 0; |
|
0 commit comments