Skip to content

Commit b8effed

Browse files
committed
feat: editor support firefox (text/html). Auto converts html to md.
1 parent a2a9934 commit b8effed

File tree

3 files changed

+91
-52
lines changed

3 files changed

+91
-52
lines changed

package-lock.json

Lines changed: 30 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"markmap-common": "^0.15.6",
2525
"markmap-lib": "^0.15.8",
2626
"markmap-view": "^0.15.8",
27-
"node-emoji": "^1.11.0"
27+
"node-emoji": "^1.11.0",
28+
"turndown": "^7.2.0"
2829
}
2930
}

src/lib/Editor.svelte

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,61 +9,84 @@
99
</script>
1010

1111
<script>
12-
import {
13-
onMount,
14-
} from 'svelte';
15-
16-
import {
17-
show,
18-
markdownSource
19-
} from './stores.js'
12+
import { onMount } from 'svelte';
13+
import { show, markdownSource } from './stores.js';
2014
import url from './url.js';
15+
import Turndown from 'turndown';
2116
2217
let textArea;
2318
let editor;
2419
2520
const my = editor => {
2621
let code = editor.textContent;
27-
// code = code.replace(/\n##\s/g,'\n@hash@hash ')
28-
code = code.replace(/:(.*)_(.*?):/g,':$1@underscore$2:')
29-
code = code.replace(/:(.*)_(.*?):/g,':$1@underscore$2:')
30-
code = code.replace(/:(.*)_(.*?):/g,':$1@underscore$2:')
22+
code = code.replace(/:(.*)_(.*?):/g, ':$1@underscore$2:');
23+
code = code.replace(/:(.*)_(.*?):/g, ':$1@underscore$2:');
24+
code = code.replace(/:(.*)_(.*?):/g, ':$1@underscore$2:');
3125
code = hljs.highlight(code, {
3226
language: 'markdown',
3327
ignoreUnescapedHTML: false
3428
}).value;
35-
code = code.replace(/\\\\/g, '<span class="language-xml"><span class="hljs-tag">\\\\</span></span>').replace(/&lt;!--(.*?)--&gt;/g,'<span class="hljs-comment">&lt;!--$1--&gt;</span>').replace(/&lt;!--(\s*?)fold(\s*?)--&gt;/g, '<span class="language-xml"><span class="hljs-special-tag">&lt;!--$1fold$2--&gt;</span></span>');
36-
// .replace(/@hash@hash\s(.*?)\n/g,'<span class="hljs-section hljs-header-2">## $1</span>\n')
37-
code = code.replace(/@underscore/g,'_')
29+
code = code.replace(/\\\\/g, '<span class="language-xml"><span class="hljs-tag">\\\\</span></span>')
30+
.replace(/<!--(.*?)-->/g, '<span class="hljs-comment"><!--$1--></span>')
31+
.replace(/<!--(\s*?)fold(\s*?)-->/g, '<span class="language-xml"><span class="hljs-special-tag"><!--$1fold$2--></span></span>');
32+
code = code.replace(/@underscore/g, '_');
3833
editor.innerHTML = code;
3934
};
4035
4136
let jar;
42-
43-
44-
let hidden
37+
let hidden;
4538
$: $show ? hidden = "" : hidden = "hidden";
4639
47-
4840
let CodeJar;
4941
5042
onMount(async () => {
51-
({
52-
CodeJar
53-
} = await import("codejar"))
54-
jar = await CodeJar(editor, my, {history:true});
55-
})
43+
const codeJarModule = await import("codejar");
44+
CodeJar = codeJarModule.CodeJar;
45+
46+
jar = await CodeJar(editor, my, { history: true });
47+
editor.addEventListener('paste', handlePaste);
48+
});
5649
57-
$: if ($show == true) {
50+
$: if ($show == true) {
5851
setTimeout(function () {
5952
textArea.firstChild.focus();
6053
}, 0);
6154
}
62-
63-
$: if(jar) {jar.onUpdate(code=>
64-
{if (jar.toString() != $markdownSource) {markdownSource.update(n=>code)}}
65-
)}
6655
56+
$: if (jar) {
57+
jar.onUpdate(code => {
58+
if (jar.toString() != $markdownSource) {
59+
markdownSource.update(n => code);
60+
}
61+
});
62+
}
63+
64+
function handlePaste(event) {
65+
event.preventDefault();
66+
const clipboardData = event.clipboardData || window.clipboardData;
67+
let pastedText = '';
68+
69+
if (clipboardData.types.includes('text/plain')) {
70+
pastedText = clipboardData.getData('text/plain');
71+
} else if (clipboardData.types.includes('text/html')) {
72+
const htmlContent = clipboardData.getData('text/html');
73+
const turndownService = new Turndown({
74+
headingStyle: 'atx',
75+
hr: '---',
76+
bulletListMarker: '-',
77+
codeBlockStyle: 'fenced',
78+
emDelimiter: '*'
79+
});
80+
pastedText = turndownService.turndown(htmlContent);
81+
}
82+
83+
if (pastedText) {
84+
markdownSource.update(() => pastedText);
85+
if (jar) {
86+
jar.updateCode(pastedText);
87+
}
88+
}
89+
}
6790
</script>
6891

6992
<div bind:this={textArea}>
@@ -90,8 +113,8 @@
90113
background-color: var(--editor-bg);
91114
}
92115
@media screen and (max-width:768px) {
93-
textarea,:global(.editor) {
94-
width:85%;
116+
textarea, :global(.editor) {
117+
width: 85%;
95118
height: 35vh;
96119
bottom: 3em;
97120
}
@@ -112,14 +135,14 @@
112135
line-height: 20px;
113136
padding: 10px;
114137
tab-size: 2;
115-
resize: horizontal!important;
138+
resize: horizontal !important;
116139
}
117140
118141
:global(.language-xml *) {
119142
color: green !important;
120143
font-weight: 300 !important;
121144
}
122-
145+
123146
:global(.hljs-section) {
124147
color: var(--text-color);
125148
font-weight: inherit;
@@ -159,22 +182,8 @@
159182
}
160183
161184
:global(.hljs-comment) {
162-
color: #777!important;
163-
font-weight:100;
164-
font-size:0.96em;
165-
166-
}
167-
168-
/* :global(.hljs-header-2) {
169-
display:inline-block;
170-
width:100%;
171-
background-color:#F8F8FF;
185+
color: #777 !important;
186+
font-weight: 100;
187+
font-size: 0.96em;
172188
}
173-
174-
:global(.hljs-header-3) {
175-
display:inline-block;
176-
width:100%;
177-
background-color:#F8F8FF;
178-
} */
179-
180189
</style>

0 commit comments

Comments
 (0)