Skip to content

Commit 88a250f

Browse files
committed
feat: upload image button
1 parent c6033e0 commit 88a250f

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

packages/bytemd/src/Toolbar.svelte

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script>
2-
import { handleDec, handleBlockquote, handleLink } from './editor.js'
2+
import { handleDec, handleBlockquote, handleLink, handleImage } from './editor.js'
33
export let cm;
4+
let fileInput;
45
</script>
56

67
<style>
@@ -25,6 +26,9 @@
2526
width: 20px;
2627
height: 20px;
2728
}
29+
input {
30+
display: none;
31+
}
2832
</style>
2933

3034
<div>
@@ -33,4 +37,6 @@
3337
<span on:click={() => handleDec(cm, '~~')}><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M10 19h4v-3h-4v3zM5 4v3h5v3h4V7h5V4H5zM3 14h18v-2H3v2z"/></svg></span>
3438
<span on:click={() => handleBlockquote(cm)}><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M6 17h3l2-4V7H5v6h3zm8 0h3l2-4V7h-6v6h3z"/></svg></span>
3539
<span on:click={() => handleLink(cm)}><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71 0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71 0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76 0 5-2.24 5-5s-2.24-5-5-5z"/></svg></span>
40+
<span on:click={() => fileInput.click()}><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M21 19V5c0-1.1-.9-2-2-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2zM8.5 13.5l2.5 3.01L14.5 12l4.5 6H5l3.5-4.5z"/></svg></span>
41+
<input bind:this={fileInput} type="file" accept="image/png, image/jpeg" on:change={(e) => handleImage(cm, e)} />
3642
</div>

packages/bytemd/src/editor.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,16 @@ export function handleLink(cm: Editor) {
3939
}
4040
cm.focus();
4141
}
42+
43+
export function handleImage(cm: Editor, e: InputEvent) {
44+
const $ = e.target as HTMLInputElement;
45+
if (!$.files) return;
46+
const file = $.files[0];
47+
const reader = new FileReader();
48+
reader.readAsDataURL(file);
49+
reader.addEventListener('load', e => {
50+
const dataUrl = e.target!.result;
51+
cm.replaceSelection(`![](${dataUrl})`);
52+
cm.focus();
53+
});
54+
}

0 commit comments

Comments
 (0)