Skip to content

Commit 9359976

Browse files
committed
feat: blockquote button
1 parent b6e22b6 commit 9359976

File tree

5 files changed

+41
-30
lines changed

5 files changed

+41
-30
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
node_modules
44
dist
55

6-
packages/plugin-*/src/index.js
7-
packages/plugin-*/src/index.d.ts
6+
packages/plugin-*/src/*.js
7+
packages/plugin-*/src/*.d.ts

packages/bytemd/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
/public/build/
33

44
.DS_Store
5+
6+
src/editor.js
7+
src/editor.d.ts

packages/bytemd/src/Toolbar.svelte

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,6 @@
11
<script>
2+
import { handleDec, handleBlockquote } from './editor.js'
23
export let cm;
3-
4-
function handleDec(e, decorator) {
5-
e.preventDefault()
6-
7-
if (cm.somethingSelected()) {
8-
cm.replaceSelection(decorator + cm.getSelection() + decorator);
9-
} else {
10-
const { anchor, head } = cm.findWordAt(cm.getCursor());
11-
const word = cm.getRange(anchor, head);
12-
cm.replaceRange(decorator + word + decorator, anchor, head);
13-
}
14-
cm.focus();
15-
}
16-
function handleBold(e) {
17-
return handleDec(e, '**');
18-
}
19-
function handleItalic(e) {
20-
return handleDec(e, '*');
21-
}
22-
function handleStrikethrough(e) {
23-
return handleDec(e, '~~');
24-
}
25-
function handleBlockquote() {}
264
</script>
275

286
<style>
@@ -48,8 +26,8 @@
4826
</style>
4927

5028
<div>
51-
<span on:click={handleBold}><svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><path d="M15.6 10.79c.97-.67 1.65-1.77 1.65-2.79 0-2.26-1.75-4-4-4H7v14h7.04c2.09 0 3.71-1.7 3.71-3.79 0-1.52-.86-2.82-2.15-3.42zM10 6.5h3c.83 0 1.5.67 1.5 1.5s-.67 1.5-1.5 1.5h-3v-3zm3.5 9H10v-3h3.5c.83 0 1.5.67 1.5 1.5s-.67 1.5-1.5 1.5z"/></svg></span>
52-
<span on:click={handleItalic}><svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><path d="M10 4v3h2.21l-3.42 8H6v3h8v-3h-2.21l3.42-8H18V4z"/></svg></span>
53-
<span on:click={handleStrikethrough}><svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><path d="M10 19h4v-3h-4v3zM5 4v3h5v3h4V7h5V4H5zM3 14h18v-2H3v2z"/></svg></span>
54-
<span on:click={handleBlockquote}><svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><path d="M6 17h3l2-4V7H5v6h3zm8 0h3l2-4V7h-6v6h3z"/></svg></span>
29+
<span on:click={() => handleDec(cm, '**')}><svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><path d="M15.6 10.79c.97-.67 1.65-1.77 1.65-2.79 0-2.26-1.75-4-4-4H7v14h7.04c2.09 0 3.71-1.7 3.71-3.79 0-1.52-.86-2.82-2.15-3.42zM10 6.5h3c.83 0 1.5.67 1.5 1.5s-.67 1.5-1.5 1.5h-3v-3zm3.5 9H10v-3h3.5c.83 0 1.5.67 1.5 1.5s-.67 1.5-1.5 1.5z"/></svg></span>
30+
<span on:click={() => handleDec(cm, '*')}><svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><path d="M10 4v3h2.21l-3.42 8H6v3h8v-3h-2.21l3.42-8H18V4z"/></svg></span>
31+
<span on:click={() => handleDec(cm, '~~')}><svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><path d="M10 19h4v-3h-4v3zM5 4v3h5v3h4V7h5V4H5zM3 14h18v-2H3v2z"/></svg></span>
32+
<span on:click={() => handleBlockquote(cm)}><svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><path d="M6 17h3l2-4V7H5v6h3zm8 0h3l2-4V7h-6v6h3z"/></svg></span>
5533
</div>

packages/bytemd/src/editor.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Editor } from 'codemirror';
2+
3+
export function handleDec(cm: Editor, decorator: string) {
4+
if (cm.somethingSelected()) {
5+
cm.replaceSelection(decorator + cm.getSelection() + decorator);
6+
} else {
7+
const { anchor, head } = cm.findWordAt(cm.getCursor());
8+
const word = cm.getRange(anchor, head);
9+
cm.replaceRange(decorator + word + decorator, anchor, head);
10+
}
11+
cm.focus();
12+
}
13+
14+
export function handleBlockquote(cm: Editor) {
15+
if (cm.somethingSelected()) {
16+
const [selection] = cm.listSelections();
17+
for (let i = selection.anchor.line; i <= selection.head.line; i++) {
18+
cm.replaceRange('> ' + cm.getLine(i), { line: i, ch: 0 }, { line: i });
19+
}
20+
} else {
21+
const { line } = cm.getCursor();
22+
const content = cm.getLine(line);
23+
if (content.startsWith('> ')) {
24+
cm.replaceRange(content.slice(2), { line, ch: 0 }, { line });
25+
} else {
26+
cm.replaceRange('> ' + content, { line, ch: 0 }, { line });
27+
}
28+
}
29+
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"target": "esnext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
77
"module": "ESNext" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
88
"lib": [
9-
"ESNext"
9+
"ESNext",
10+
"DOM"
1011
] /* Specify library files to be included in the compilation. */,
1112
// "allowJs": true, /* Allow javascript files to be compiled. */
1213
// "checkJs": true, /* Report errors in .js files. */

0 commit comments

Comments
 (0)