Skip to content

Commit 8fdcc39

Browse files
build: add snowpack to debug
1 parent 344b5bd commit 8fdcc39

18 files changed

+1588
-148
lines changed

.eslintignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
/dist
2+
/publish
33
/*.js
44
.*.{js,ts}
55

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# compiled output
2-
/dist/
2+
/publish/
33

44
# packages
55
/node_modules/

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# web-components
1+
# web-components

package.json

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cloudpss/web-components",
3-
"version": "0.0.7",
3+
"version": "0.0.8",
44
"main": "index.js",
55
"license": "MIT",
66
"author": {
@@ -12,10 +12,12 @@
1212
"url": "https://github.com/CloudPSS/web-components/issues"
1313
},
1414
"scripts": {
15-
"compile": "tsc",
15+
"start": "snowpack dev",
1616
"clean": "rimraf dist",
17-
"build": "yarn clean && yarn compile",
18-
"postbuild": "node postbuild.js"
17+
"build": "yarn clean && tsc",
18+
"postbuild": "node postbuild.js",
19+
"format": "prettier --write --ignore-path .gitignore .",
20+
"lint": "eslint ."
1921
},
2022
"dependencies": {
2123
"chart.js": "^2.9.4",
@@ -48,6 +50,7 @@
4850
},
4951
"devDependencies": {
5052
"@commitlint/cli": "^11.0.0",
53+
"@snowpack/plugin-typescript": "^1.1.0",
5154
"@types/chart.js": "^2.9.27",
5255
"@types/katex": "^0.11.0",
5356
"@types/linkify-it": "^2.1.0",
@@ -69,6 +72,7 @@
6972
"lint-staged": "^10.5.1",
7073
"prettier": "^2.1.2",
7174
"rimraf": "^3.0.2",
75+
"snowpack": "^2.16.1",
7276
"type-fest": "^0.18.0",
7377
"typescript": "~4.0.5"
7478
}

postbuild.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ async function main() {
1010
const sourceObj = JSON.parse(source);
1111
sourceObj.scripts = undefined;
1212
sourceObj.devDependencies = undefined;
13-
await fs.writeFile(path.resolve(__dirname, './dist/package.json'), JSON.stringify(sourceObj, null, 2), 'utf-8');
13+
await fs.writeFile(path.resolve(__dirname, './publish/package.json'), JSON.stringify(sourceObj, null, 2), 'utf-8');
1414
await copy('./src');
1515
await copy('./README.md');
1616
await copy('./LICENSE');
1717
}
1818

1919
async function copy(file) {
20-
await fs.copy(path.resolve(__dirname, file), path.resolve(__dirname, './dist/', file));
20+
await fs.copy(path.resolve(__dirname, file), path.resolve(__dirname, './publish/', file));
2121
}
2222

2323
main();

snowpack.config.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/** @type {import("snowpack").SnowpackUserConfig } */
2+
module.exports = {
3+
mount: {
4+
stat: '/',
5+
src: '/_dist_',
6+
},
7+
plugins: ['@snowpack/plugin-typescript'],
8+
install: [],
9+
installOptions: {
10+
installTypes: true,
11+
polyfillNode: true,
12+
},
13+
devOptions: {},
14+
buildOptions: {
15+
out: 'publish',
16+
},
17+
proxy: {},
18+
alias: {},
19+
};

src/highlight.ts

+2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ export class HighlightElement extends UpdatingElement {
173173
* @inheritdoc
174174
*/
175175
async performUpdate(): Promise<unknown> {
176+
this.elCode.textContent = this.srcdoc ?? '';
176177
await init();
177178
return super.performUpdate();
178179
}
@@ -195,6 +196,7 @@ export class HighlightElement extends UpdatingElement {
195196
if (highlighter) {
196197
this.elCode.innerHTML = Prism.highlight(code, Prism.languages[lang], lang);
197198
} else {
199+
this.elCode.textContent = code;
198200
const autoloader = Prism.plugins.autoloader as {
199201
loadLanguages: (name: string, callback: () => void) => void;
200202
};
File renamed without changes.

src/markdown.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { customElement, property, PropertyValues, UpdatingElement } from 'lit-element';
22
import markdownIt from './private/markdown';
3-
import styles from './markdown.css';
3+
import styles from './markdown.styles';
44

55
let fm: string | undefined;
66
let src: URL | undefined;

src/math.ts

+49-72
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as katex from 'katex';
1+
import katex from 'katex';
22
import { escapeHtml } from 'markdown-it/lib/common/utils';
33
import { css, customElement, property, PropertyValues, UpdatingElement } from 'lit-element';
44
import { resolve } from './config';
@@ -13,106 +13,66 @@ type DisplayMode = 'inline' | 'display';
1313
* 一种公式语言
1414
*/
1515
type Language = {
16-
copyDelimiters: Record<MathMode, [string, string]>;
1716
/**
1817
* 渲染公式
1918
*/
20-
render(this: MathElement, source: string, mode: MathMode): void | Promise<void>;
19+
render(this: HTMLElement, source: string, mode: MathMode): void | Promise<void>;
2120
};
2221

2322
/**
2423
* 注入样式
2524
*/
26-
function style(el: HTMLElement): void {
25+
function style(el: HTMLElement, inElement: boolean): void {
2726
const link = document.createElement('link');
2827
link.rel = 'stylesheet';
2928
link.href = resolve('katex', '^0.12', 'dist/katex.css');
30-
const style = document.createElement('style');
31-
style.textContent = css`
32-
cwe-math {
33-
display: inline;
34-
user-select: all;
35-
}
36-
cwe-math[mode='display'] {
37-
display: inline-block;
38-
overflow: auto;
39-
margin: 0.8em 0;
40-
}
41-
`.cssText;
4229
el.appendChild(link);
43-
el.appendChild(style);
30+
if (inElement) {
31+
const style = document.createElement('style');
32+
style.textContent = css`
33+
:host {
34+
display: inline;
35+
user-select: all;
36+
--error-color: red;
37+
}
38+
:host([mode='display']) {
39+
display: block;
40+
overflow: auto;
41+
margin: 0.8em 0;
42+
}
43+
#styles {
44+
display: none;
45+
}
46+
#content {
47+
display: contents;
48+
}
49+
.error {
50+
color: var(--error-color);
51+
}
52+
`.cssText;
53+
el.appendChild(style);
54+
}
4455
}
4556

4657
const languages: Record<string, Language | { aliasOf: string }> = {
4758
tex: {
48-
copyDelimiters: {
49-
inline: ['$', '$'],
50-
display: ['$$ ', ' $$'],
51-
},
5259
render(source: string, mode: 'display' | 'inline'): void {
5360
katex.render(source, this, {
5461
displayMode: mode === 'display',
5562
});
56-
style(this);
5763
},
5864
},
5965
latex: { aliasOf: 'tex' },
6066
katex: { aliasOf: 'tex' },
6167
// asciimath: {
62-
// copyDelimiters: {
63-
// inline: ['\\(', '\\)'],
64-
// display: ['\\[ ', ' \\]'],
65-
// },
6668
// render(source: string, mode: 'display' | 'inline'): void | Promise<void> {
6769
// const tex = asciimathToLatex(source);
6870
// return (languages.tex as Language).render.call(this, tex, mode);
6971
// },
7072
// },
7173
};
7274

73-
/**
74-
* 将 cwe-math 中的内容替换为源
75-
*/
76-
function replaceMathText(fragment: DocumentFragment): void {
77-
const math = fragment.querySelectorAll<MathElement>(`cwe-math[language]`);
78-
math.forEach((el) => {
79-
const source = el.srcdoc ?? '';
80-
if (!source) return;
81-
const lang = el.language;
82-
const mode = el.mode;
83-
if (!lang || !(lang in languages)) {
84-
return;
85-
}
86-
const d = (languages[lang] as Language).copyDelimiters[mode];
87-
el.textContent = `${d[0]}${source}${d[1]}`;
88-
});
89-
}
90-
91-
/**复制 */
92-
function onCopy(event: HTMLElementEventMap['copy']): void {
93-
const selection = window.getSelection();
94-
if (!selection || selection.isCollapsed || !event.clipboardData) {
95-
return;
96-
}
97-
const fragment = selection.getRangeAt(0).cloneContents();
98-
if (!fragment.querySelector('cwe-math')) {
99-
return;
100-
}
101-
// Preserve usual HTML copy/paste behavior.
102-
const html: string[] = [];
103-
fragment.childNodes.forEach((node) => {
104-
html.push((node as Element).outerHTML);
105-
});
106-
event.clipboardData.setData('text/html', html.join(''));
107-
replaceMathText(fragment);
108-
// Rewrite plain-text version.
109-
event.clipboardData.setData('text/plain', fragment.textContent ?? '');
110-
// Prevent normal copy handling.
111-
event.preventDefault();
112-
}
113-
114-
document.addEventListener('copy', onCopy);
115-
style(document.head);
75+
style(document.head, false);
11676

11777
/**
11878
* 公式模式
@@ -124,6 +84,21 @@ type MathMode = 'inline' | 'display';
12484
*/
12585
@customElement('cwe-math')
12686
export class MathElement extends UpdatingElement {
87+
constructor() {
88+
super();
89+
const root = this.attachShadow({ mode: 'open' });
90+
this.elStyles = document.createElement('div');
91+
this.elStyles.id = 'styles';
92+
this.elContent = document.createElement('div');
93+
this.elContent.id = 'content';
94+
root.append(this.elStyles, this.elContent);
95+
style(this.elStyles, true);
96+
}
97+
98+
/** 样式 */
99+
private readonly elStyles: HTMLElement;
100+
/** 渲染结果 */
101+
private readonly elContent: HTMLElementTagNameMap['article'];
127102
/** 语言 */
128103
@property({
129104
reflect: true,
@@ -166,13 +141,15 @@ export class MathElement extends UpdatingElement {
166141
const source = this.srcdoc?.trim() ?? '';
167142

168143
if (!langDef || langDef.render == null) {
169-
this.innerHTML = `<span class="error">Unsupported language ${lang ?? ''}</span>`;
144+
this.elContent.innerHTML = `<span class="error">Unsupported language ${lang ?? ''}</span>`;
170145
return;
171146
}
172147
try {
173-
await langDef.render.call(this, source, this.mode);
148+
await langDef.render.call(this.elContent, source, this.mode);
174149
} catch (ex) {
175-
this.innerHTML = `<span class="error" title="${escapeHtml(String(ex))}">${escapeHtml(source)}</span>`;
150+
this.elContent.innerHTML = `<span class="error" title="${escapeHtml(String(ex))}">${escapeHtml(
151+
source,
152+
)}</span>`;
176153
}
177154
}
178155
}

src/mermaid.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { Subscription, merge } from 'rxjs';
22
import { tap, share, map, distinctUntilChanged } from 'rxjs/operators';
33
import { resizing } from './private/utils';
44
import mermaid from 'mermaid';
5+
import type mermaidAPI from 'mermaid/mermaidAPI';
56
import { customElement, property, PropertyValues, UpdatingElement } from 'lit-element';
67
import { theme } from './config';
7-
import mermaidAPI from 'mermaid/mermaidAPI';
88

99
let t: mermaidAPI.Theme = 'default';
1010

src/private/markdown/imsize/helpers/normalize_reference.js

-9
This file was deleted.

src/private/markdown/imsize/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
'use strict';
55

6-
var parseImageSize = require('./helpers/parse_image_size');
6+
import parseImageSize from './parse_image_size';
77

88
function image_with_size(md) {
99
return function (state, silent) {
@@ -217,6 +217,6 @@ function image_with_size(md) {
217217
};
218218
}
219219

220-
module.exports = function imsize_plugin(md, options) {
220+
export default function imsize_plugin(md, options) {
221221
md.inline.ruler.before('emphasis', 'image', image_with_size(md, options));
222-
};
222+
}

src/private/markdown/imsize/helpers/parse_image_size.js src/private/markdown/imsize/parse_image_size.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function parseNextNumber(str, pos, max) {
2424
return result;
2525
}
2626

27-
module.exports = function parseImageSize(str, pos, max) {
27+
export default function parseImageSize(str, pos, max) {
2828
var code,
2929
result = {
3030
ok: false,
@@ -75,4 +75,4 @@ module.exports = function parseImageSize(str, pos, max) {
7575
result.pos = pos;
7676
result.ok = true;
7777
return result;
78-
};
78+
}

0 commit comments

Comments
 (0)