Skip to content

Commit 3441347

Browse files
committed
feat: editing in preview
about: mProjectsCode#44
1 parent 0bd01ce commit 3441347

File tree

3 files changed

+134
-33
lines changed

3 files changed

+134
-33
lines changed

meta.txt

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
{
22
"inputs": {
3+
"src/CodeBlock.ts": {
4+
"bytes": 2293,
5+
"imports": [
6+
{
7+
"path": "obsidian",
8+
"kind": "import-statement",
9+
"external": true
10+
}
11+
],
12+
"format": "esm"
13+
},
314
"src/codemirror/Cm6_Util.ts": {
4-
"bytes": 1334,
15+
"bytes": 1392,
516
"imports": [
617
{
718
"path": "@codemirror/state",
@@ -17,7 +28,7 @@
1728
"format": "esm"
1829
},
1930
"src/codemirror/Cm6_ViewPlugin.ts": {
20-
"bytes": 8053,
31+
"bytes": 8339,
2132
"imports": [
2233
{
2334
"path": "src/main.ts",
@@ -63,12 +74,12 @@
6374
"format": "esm"
6475
},
6576
"src/settings/Settings.ts": {
66-
"bytes": 396,
77+
"bytes": 413,
6778
"imports": [],
6879
"format": "esm"
6980
},
7081
"src/settings/StringSelectModal.ts": {
71-
"bytes": 596,
82+
"bytes": 622,
7283
"imports": [
7384
{
7485
"path": "obsidian",
@@ -5147,7 +5158,7 @@
51475158
"format": "esm"
51485159
},
51495160
"src/settings/SettingsTab.ts": {
5150-
"bytes": 5561,
5161+
"bytes": 5721,
51515162
"imports": [
51525163
{
51535164
"path": "obsidian",
@@ -5168,7 +5179,7 @@
51685179
"format": "esm"
51695180
},
51705181
"src/PrismPlugin.ts": {
5171-
"bytes": 3493,
5182+
"bytes": 3615,
51725183
"imports": [],
51735184
"format": "esm"
51745185
},
@@ -7238,7 +7249,7 @@
72387249
"format": "esm"
72397250
},
72407251
"src/themes/ObsidianTheme.ts": {
7241-
"bytes": 17553,
7252+
"bytes": 18247,
72427253
"imports": [
72437254
{
72447255
"path": "shiki",
@@ -7249,7 +7260,7 @@
72497260
"format": "esm"
72507261
},
72517262
"src/themes/ThemeMapper.ts": {
7252-
"bytes": 3045,
7263+
"bytes": 3151,
72537264
"imports": [
72547265
{
72557266
"path": "node_modules/shiki/dist/index.mjs",
@@ -11785,7 +11796,7 @@
1178511796
"format": "esm"
1178611797
},
1178711798
"src/themes/ECTheme.ts": {
11788-
"bytes": 5138,
11799+
"bytes": 5236,
1178911800
"imports": [
1179011801
{
1179111802
"path": "@expressive-code/core",
@@ -11801,7 +11812,7 @@
1180111812
"format": "esm"
1180211813
},
1180311814
"src/Highlighter.ts": {
11804-
"bytes": 8543,
11815+
"bytes": 8799,
1180511816
"imports": [
1180611817
{
1180711818
"path": "node_modules/@expressive-code/core/dist/index.js",
@@ -11877,17 +11888,17 @@
1187711888
"format": "esm"
1187811889
},
1187911890
"src/main.ts": {
11880-
"bytes": 5831,
11891+
"bytes": 8132,
1188111892
"imports": [
1188211893
{
1188311894
"path": "obsidian",
1188411895
"kind": "import-statement",
1188511896
"external": true
1188611897
},
1188711898
{
11888-
"path": "src/CodeBlock",
11899+
"path": "src/CodeBlock.ts",
1188911900
"kind": "import-statement",
11890-
"external": true
11901+
"original": "src/CodeBlock"
1189111902
},
1189211903
{
1189311904
"path": "src/codemirror/Cm6_ViewPlugin.ts",
@@ -11931,6 +11942,11 @@
1193111942
"outputs": {
1193211943
"main.js": {
1193311944
"imports": [
11945+
{
11946+
"path": "obsidian",
11947+
"kind": "require-call",
11948+
"external": true
11949+
},
1193411950
{
1193511951
"path": "obsidian",
1193611952
"kind": "require-call",
@@ -13954,7 +13970,10 @@
1395413970
"bytesInOutput": 100
1395513971
},
1395613972
"src/main.ts": {
13957-
"bytesInOutput": 2677
13973+
"bytesInOutput": 3745
13974+
},
13975+
"src/CodeBlock.ts": {
13976+
"bytesInOutput": 986
1395813977
},
1395913978
"src/codemirror/Cm6_ViewPlugin.ts": {
1396013979
"bytesInOutput": 2708
@@ -14221,7 +14240,7 @@
1422114240
"bytesInOutput": 32
1422214241
},
1422314242
"node_modules/hast-util-select/lib/pseudo.js": {
14224-
"bytesInOutput": 3796
14243+
"bytesInOutput": 3800
1422514244
},
1422614245
"node_modules/hast-util-select/lib/test.js": {
1422714246
"bytesInOutput": 388
@@ -14335,7 +14354,7 @@
1433514354
"bytesInOutput": 4584
1433614355
}
1433714356
},
14338-
"bytes": 19041508
14357+
"bytes": 19043566
1433914358
}
1434014359
}
1434114360
}

src/main.ts

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,60 @@ export default class ShikiPlugin extends Plugin {
111111
}
112112
}
113113

114-
const html:string = await codeToHtml(source, {
115-
lang: language,
116-
theme: this.settings.theme,
117-
transformers: [
118-
transformerNotationDiff(),
119-
transformerNotationDiff({ matchAlgorithm: 'v3' }),
120-
transformerNotationHighlight(),
121-
transformerNotationFocus(),
122-
transformerNotationErrorLevel(),
123-
transformerMetaHighlight(),
124-
],
125-
})
126-
el.innerHTML = html
127-
128-
// const codeBlock = new CodeBlock(this, el, source, language, ctx);
129-
130-
// ctx.addChild(codeBlock);
114+
let option: 'pre'|'old'|'area' = 'pre' // TODO as a new setting option
115+
116+
// @ts-ignore
117+
if (option === 'area') {
118+
// - div
119+
// - span
120+
// - pre
121+
// - textarea
122+
const div = document.createElement('div'); el.appendChild(div); div.classList.add('obsidian-shiki-plugin')
123+
div.setAttribute('relative', ''); div.setAttribute('min-h-100', ''); div.setAttribute('float-left', ''); div.setAttribute('min-w-full', '');
124+
const span = document.createElement('span'); div.appendChild(span);
125+
const textarea = document.createElement('textarea'); div.appendChild(textarea); textarea.classList.add('line-height-$vp-code-line-height', 'font-$vp-font-family-mono', 'text-size-$vp-code-font-size');
126+
// 这些属性很奇怪,我抄了shiki.style上的属性。但支撑这些的,是许多类似 `[absolute=""]` 这样选择器的css
127+
// 也许是为了方便样式覆盖
128+
const attributes = {
129+
'whitespace-pre': '',
130+
'overflow-auto': '',
131+
'w-full': '',
132+
'h-full': '',
133+
'font-mono': '',
134+
'bg-transparent': '',
135+
'absolute': '',
136+
'inset-0': '',
137+
'py-20px': '',
138+
'px-24px': '',
139+
'text-transparent': '',
140+
'carent-gray': '',
141+
'tab-4': '',
142+
'resize-none': '',
143+
'z-10': '',
144+
'autocomplete': 'off',
145+
'autocorrect': 'off',
146+
'autocapitalize': 'none',
147+
'spellcheck': 'false',
148+
};
149+
Object.entries(attributes).forEach(([key, val]) => {
150+
textarea.setAttribute(key, val);
151+
});
152+
// async part
153+
this.getPre(language, source).then(pre => span.innerHTML = pre);
154+
textarea.value = source;
155+
textarea.oninput = (ev) => {
156+
const newValue = (ev.target as HTMLTextAreaElement).value
157+
this.getPre(language, newValue).then(pre => span.innerHTML = pre);
158+
}
159+
}
160+
// @ts-ignore
161+
else if (option === 'pre') {
162+
this.getPre(language, source).then(pre => el.innerHTML = pre);
163+
}
164+
else {
165+
const codeBlock = new CodeBlock(this, el, source, language, ctx);
166+
ctx.addChild(codeBlock);
167+
}
131168
},
132169
1000,
133170
);
@@ -137,6 +174,22 @@ export default class ShikiPlugin extends Plugin {
137174
}
138175
}
139176

177+
async getPre(language:string, source:string): Promise<string> {
178+
const pre:string = await codeToHtml(source, {
179+
lang: language,
180+
theme: this.settings.theme,
181+
transformers: [
182+
transformerNotationDiff(),
183+
transformerNotationDiff({ matchAlgorithm: 'v3' }),
184+
transformerNotationHighlight(),
185+
transformerNotationFocus(),
186+
transformerNotationErrorLevel(),
187+
transformerMetaHighlight(),
188+
],
189+
})
190+
return pre
191+
}
192+
140193
registerInlineCodeProcessor(): void {
141194
this.registerMarkdownPostProcessor(async (el, ctx) => {
142195
const inlineCodes = el.findAll(':not(pre) > code');

styles.css

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,32 @@ code>span.line.highlighted.warning {
289289
code>span.line {
290290
line-height: 24px;
291291
}
292+
293+
/* shiki attr style */
294+
[absolute=""] { position: absolute; }
295+
[relative=""] { position: relative; }
296+
[min-w-full=""] { min-width: 100%; }
297+
[min-h-100=""] { min-height: 25rem; }
298+
[w-full=""] { width: 100%; }
299+
[h-full=""] { height: 100%; }
300+
[float-left=""] { float: left; }
301+
[overflow-auto=""] { overflow: auto; }
302+
[bg-transparent=""] { background-color: transparent; }
303+
/* [text-transparent=""] { color: transparent; } */
304+
[text-transparent=""] { color: #ffffff33; }
305+
[inset-0=""] { top: 0; right: 0; bottom: 0; left: 0; }
306+
[py-20px=""] { padding-top: 20px; padding-bottom: 20px; }
307+
[px-24px=""] { padding-top: 24px; padding-bottom: 24px; }
308+
[resize-none=""] { resize: none; }
309+
[z-10=""] { z-index: 10; }
310+
[tab-4=""] {}
311+
[carent-gray=""] {}
312+
[font-mono=""] {}
313+
314+
/* keep same: pre & textare */
315+
.obsidian-shiki-plugin code, .obsidian-shiki-plugin textarea {
316+
line-height: 24px;
317+
padding: 12px 16px;
318+
margin: 0;
319+
border: 0;
320+
}

0 commit comments

Comments
 (0)