UNPKG

12.9 kBJavaScriptView Raw
1var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5 return c > 3 && r && Object.defineProperty(target, key, r), r;
6};
7var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9 if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10 return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11};
12var _DuoyunCodeBlockElement_instances, _DuoyunCodeBlockElement_getRanges, _DuoyunCodeBlockElement_getParts;
13// https://spectrum.adobe.com/page/code/
14import { adoptedStyle, customElement, attribute, refobject } from '@mantou/gem/lib/decorators';
15import { GemElement, html } from '@mantou/gem/lib/element';
16import { createCSSSheet, css, styleMap } from '@mantou/gem/lib/utils';
17import { theme } from '../lib/theme';
18// https://github.com/PrismJS/prism/blob/master/plugins/autoloader/prism-autoloader.js
19const langDependencies = {
20 javascript: 'clike',
21 actionscript: 'javascript',
22 apex: ['clike', 'sql'],
23 arduino: 'cpp',
24 aspnet: ['markup', 'csharp'],
25 birb: 'clike',
26 bison: 'c',
27 c: 'clike',
28 csharp: 'clike',
29 cpp: 'c',
30 cfscript: 'clike',
31 chaiscript: ['clike', 'cpp'],
32 coffeescript: 'javascript',
33 crystal: 'ruby',
34 'css-extras': 'css',
35 d: 'clike',
36 dart: 'clike',
37 django: 'markup-templating',
38 ejs: ['javascript', 'markup-templating'],
39 etlua: ['lua', 'markup-templating'],
40 erb: ['ruby', 'markup-templating'],
41 fsharp: 'clike',
42 'firestore-security-rules': 'clike',
43 flow: 'javascript',
44 ftl: 'markup-templating',
45 gml: 'clike',
46 glsl: 'c',
47 go: 'clike',
48 groovy: 'clike',
49 haml: 'ruby',
50 handlebars: 'markup-templating',
51 haxe: 'clike',
52 hlsl: 'c',
53 idris: 'haskell',
54 java: 'clike',
55 javadoc: ['markup', 'java', 'javadoclike'],
56 jolie: 'clike',
57 jsdoc: ['javascript', 'javadoclike', 'typescript'],
58 'js-extras': 'javascript',
59 json5: 'json',
60 jsonp: 'json',
61 'js-templates': 'javascript',
62 kotlin: 'clike',
63 latte: ['clike', 'markup-templating', 'php'],
64 less: 'css',
65 lilypond: 'scheme',
66 liquid: 'markup-templating',
67 markdown: 'markup',
68 'markup-templating': 'markup',
69 mongodb: 'javascript',
70 n4js: 'javascript',
71 objectivec: 'c',
72 opencl: 'c',
73 parser: 'markup',
74 php: 'markup-templating',
75 phpdoc: ['php', 'javadoclike'],
76 'php-extras': 'php',
77 plsql: 'sql',
78 processing: 'clike',
79 protobuf: 'clike',
80 pug: ['markup', 'javascript'],
81 purebasic: 'clike',
82 purescript: 'haskell',
83 qsharp: 'clike',
84 qml: 'javascript',
85 qore: 'clike',
86 racket: 'scheme',
87 cshtml: ['markup', 'csharp'],
88 jsx: ['markup', 'javascript'],
89 tsx: ['jsx', 'typescript'],
90 reason: 'clike',
91 ruby: 'clike',
92 sass: 'css',
93 scss: 'css',
94 scala: 'java',
95 'shell-session': 'bash',
96 smarty: 'markup-templating',
97 solidity: 'clike',
98 soy: 'markup-templating',
99 sparql: 'turtle',
100 sqf: 'clike',
101 squirrel: 'clike',
102 't4-cs': ['t4-templating', 'csharp'],
103 't4-vb': ['t4-templating', 'vbnet'],
104 tap: 'yaml',
105 tt2: ['clike', 'markup-templating'],
106 textile: 'markup',
107 twig: 'markup-templating',
108 typescript: 'javascript',
109 v: 'clike',
110 vala: 'clike',
111 vbnet: 'basic',
112 velocity: 'markup',
113 wiki: 'markup',
114 xeora: 'markup',
115 'xml-doc': 'markup',
116 xquery: 'markup',
117};
118const langAliases = {
119 html: 'markup',
120 xml: 'markup',
121 svg: 'markup',
122 mathml: 'markup',
123 ssml: 'markup',
124 atom: 'markup',
125 rss: 'markup',
126 js: 'javascript',
127 g4: 'antlr4',
128 ino: 'arduino',
129 adoc: 'asciidoc',
130 avs: 'avisynth',
131 avdl: 'avro-idl',
132 shell: 'bash',
133 shortcode: 'bbcode',
134 rbnf: 'bnf',
135 oscript: 'bsl',
136 cs: 'csharp',
137 dotnet: 'csharp',
138 cfc: 'cfscript',
139 coffee: 'coffeescript',
140 conc: 'concurnas',
141 jinja2: 'django',
142 'dns-zone': 'dns-zone-file',
143 dockerfile: 'docker',
144 gv: 'dot',
145 eta: 'ejs',
146 xlsx: 'excel-formula',
147 xls: 'excel-formula',
148 gamemakerlanguage: 'gml',
149 gni: 'gn',
150 'go-mod': 'go-module',
151 hbs: 'handlebars',
152 hs: 'haskell',
153 idr: 'idris',
154 gitignore: 'ignore',
155 hgignore: 'ignore',
156 npmignore: 'ignore',
157 webmanifest: 'json',
158 kt: 'kotlin',
159 kts: 'kotlin',
160 kum: 'kumir',
161 tex: 'latex',
162 context: 'latex',
163 ly: 'lilypond',
164 emacs: 'lisp',
165 elisp: 'lisp',
166 'emacs-lisp': 'lisp',
167 md: 'markdown',
168 moon: 'moonscript',
169 n4jsd: 'n4js',
170 nani: 'naniscript',
171 objc: 'objectivec',
172 qasm: 'openqasm',
173 objectpascal: 'pascal',
174 px: 'pcaxis',
175 pcode: 'peoplecode',
176 pq: 'powerquery',
177 mscript: 'powerquery',
178 pbfasm: 'purebasic',
179 purs: 'purescript',
180 py: 'python',
181 qs: 'qsharp',
182 rkt: 'racket',
183 razor: 'cshtml',
184 rpy: 'renpy',
185 robot: 'robotframework',
186 rb: 'ruby',
187 'sh-session': 'shell-session',
188 shellsession: 'shell-session',
189 smlnj: 'sml',
190 sol: 'solidity',
191 sln: 'solution-file',
192 rq: 'sparql',
193 t4: 't4-cs',
194 trickle: 'tremor',
195 troy: 'tremor',
196 trig: 'turtle',
197 ts: 'typescript',
198 tsconfig: 'typoscript',
199 uscript: 'unrealscript',
200 uc: 'unrealscript',
201 url: 'uri',
202 vb: 'visual-basic',
203 vba: 'visual-basic',
204 webidl: 'web-idl',
205 mathematica: 'wolfram',
206 nb: 'wolfram',
207 wl: 'wolfram',
208 xeoracube: 'xeora',
209 yml: 'yaml',
210};
211const style = createCSSSheet(css `
212 :host(:where(:not([hidden]))) {
213 position: relative;
214 display: block;
215 font-size: 0.875em;
216 background: ${theme.lightBackgroundColor};
217 border-radius: ${theme.normalRound};
218 --comment-color: var(--code-comment-color, #6e6e6e);
219 --section-color: var(--code-section-color, #c9252d);
220 --title-color: var(--code-title-color, #4646c6);
221 --variable-color: var(--code-variable-color, #ae0e66);
222 --literal-color: var(--code-literal-color, #6f38b1);
223 --string-color: var(--code-string-color, #12805c);
224 --function-color: var(--code-function-color, #0d66d0);
225 --keyword-color: var(--code-keyword-color, #93219e);
226 --attribute-color: var(--code-attribute-color, #4646c6);
227 }
228 .highlight {
229 display: block;
230 position: absolute;
231 pointer-events: none;
232 background: ${theme.highlightColor};
233 opacity: 0.1;
234 width: 100%;
235 }
236 .code {
237 display: block;
238 font-family: ${theme.codeFont};
239 text-align: left;
240 white-space: pre;
241 tab-size: 2;
242 hyphens: none;
243 overflow: auto;
244 overflow-clip-box: content-box;
245 box-shadow: none;
246 border: none;
247 background: transparent;
248 scrollbar-width: thin;
249 }
250 .code::-webkit-scrollbar {
251 height: 0.5em;
252 }
253 .code::-webkit-scrollbar-thumb {
254 border-radius: inherit;
255 }
256 .token.comment,
257 .token.prolog,
258 .token.doctype,
259 .token.cdata {
260 color: var(--comment-color);
261 font-style: italic;
262 }
263 .token.punctuation {
264 color: var(--title-color);
265 }
266 .token.tag .punctuation {
267 color: ${theme.textColor};
268 }
269 .token.tag,
270 .token.tag .class-name,
271 .token.property,
272 .token.constant,
273 .token.symbol,
274 .token.deleted,
275 .token.operator,
276 .token.entity {
277 color: var(--section-color);
278 }
279 .token.url,
280 .language-css .token.string,
281 .style .token.string,
282 .token.variable {
283 color: var(--variable-color);
284 }
285 .token.boolean,
286 .token.number {
287 color: var(--literal-color);
288 }
289 .token.attr-value,
290 .token.string,
291 .token.char,
292 .token.builtin,
293 .token.inserted {
294 color: var(--string-color);
295 }
296 .token.atrule,
297 .token.function,
298 .token.class-name {
299 color: var(--function-color);
300 }
301 .token.keyword {
302 color: var(--keyword-color);
303 }
304 .token.selector,
305 .token.attr-name,
306 .token.regex,
307 .token.important {
308 color: var(--attribute-color);
309 }
310 .token.important,
311 .token.bold {
312 font-weight: bold;
313 }
314 .token.italic {
315 font-style: italic;
316 }
317 .token.entity {
318 cursor: help;
319 }
320 @media print {
321 code {
322 border-left: 5px solid ${theme.borderColor};
323 white-space: pre-wrap;
324 word-break: break-word;
325 }
326 .highlight {
327 display: none;
328 }
329 }
330`);
331/**
332 * @customElement dy-code-block
333 */
334let DuoyunCodeBlockElement = class DuoyunCodeBlockElement extends GemElement {
335 constructor() {
336 super(...arguments);
337 _DuoyunCodeBlockElement_instances.add(this);
338 }
339 mounted() {
340 this.effect(async () => {
341 if (!this.codeRef.element)
342 return;
343 const esmHost = 'https://cdn.skypack.dev/prismjs@v1.26.0';
344 await import(/* @vite-ignore */ /* webpackIgnore: true */ `${esmHost}?min`);
345 const { Prism } = window;
346 if (this.codelang && !Prism.languages[this.codelang]) {
347 const lang = langAliases[this.codelang] || this.codelang;
348 const langDeps = [].concat(langDependencies[lang] || []);
349 try {
350 await Promise.all(langDeps.map((langDep) => {
351 if (!Prism.languages[langDep]) {
352 return import(
353 /* @vite-ignore */ /* webpackIgnore: true */ `${esmHost}/components/prism-${langDep}.min.js`);
354 }
355 }));
356 await import(/* @vite-ignore */ /* webpackIgnore: true */ `${esmHost}/components/prism-${lang}.min.js`);
357 }
358 catch (_a) {
359 //
360 }
361 }
362 const content = Prism.languages[this.codelang]
363 ? Prism.highlight(this.textContent || '', Prism.languages[this.codelang], this.codelang)
364 : this.innerHTML;
365 this.codeRef.element.innerHTML = __classPrivateFieldGet(this, _DuoyunCodeBlockElement_instances, "m", _DuoyunCodeBlockElement_getParts).call(this, content);
366 }, () => []);
367 }
368 render() {
369 const lineHeight = 1.5;
370 const padding = 1;
371 return html `
372 ${this.highlight
373 ? __classPrivateFieldGet(this, _DuoyunCodeBlockElement_instances, "m", _DuoyunCodeBlockElement_getRanges).call(this, this.highlight).map(([start, end]) => html `
374 <span
375 class="highlight"
376 style=${styleMap({
377 top: `${(start - 1) * lineHeight + padding}em`,
378 height: `${(end - start + 1) * lineHeight}em`,
379 })}
380 ></span>
381 `)
382 : ''}
383 <code ref=${this.codeRef.ref} class="code">${__classPrivateFieldGet(this, _DuoyunCodeBlockElement_instances, "m", _DuoyunCodeBlockElement_getParts).call(this, this.textContent || '')}</code>
384 <style>
385 code {
386 padding: ${padding}em;
387 line-height: ${lineHeight};
388 }
389 </style>
390 `;
391 }
392};
393_DuoyunCodeBlockElement_instances = new WeakSet(), _DuoyunCodeBlockElement_getRanges = function _DuoyunCodeBlockElement_getRanges(range) {
394 const ranges = range.split(/,\s*/);
395 return ranges.map((range) => {
396 const [start, end = start] = range.split('-');
397 return [parseInt(start) || 1, parseInt(end) || 0];
398 });
399}, _DuoyunCodeBlockElement_getParts = function _DuoyunCodeBlockElement_getParts(s) {
400 const lines = s.split(/\n|\r\n/);
401 const parts = this.range
402 ? __classPrivateFieldGet(this, _DuoyunCodeBlockElement_instances, "m", _DuoyunCodeBlockElement_getRanges).call(this, this.range).map(([start, end]) => {
403 let result = '';
404 for (let i = start - 1; i < (end || lines.length); i++) {
405 result += lines[i] + '\n';
406 }
407 return result;
408 })
409 : [s];
410 return parts.join('\n...\n\n');
411};
412__decorate([
413 attribute
414], DuoyunCodeBlockElement.prototype, "codelang", void 0);
415__decorate([
416 attribute
417], DuoyunCodeBlockElement.prototype, "range", void 0);
418__decorate([
419 attribute
420], DuoyunCodeBlockElement.prototype, "highlight", void 0);
421__decorate([
422 refobject
423], DuoyunCodeBlockElement.prototype, "codeRef", void 0);
424DuoyunCodeBlockElement = __decorate([
425 customElement('dy-code-block'),
426 adoptedStyle(style)
427], DuoyunCodeBlockElement);
428export { DuoyunCodeBlockElement };
429//# sourceMappingURL=code-block.js.map
\No newline at end of file