-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
276 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1 @@ | ||
/node_modules/ | ||
/public/build/ | ||
|
||
.DS_Store | ||
|
||
*.js | ||
*.d.ts | ||
!src/index.js | ||
!src/index.d.ts | ||
/svelte/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"module": "dist/index.mjs", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts" | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/bytemd/react/index.tsx → packages/bytemd/react/src/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
declare module '*.svg' { | ||
let x: string; | ||
export default x; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import unified from 'unified'; | ||
import markdown from 'remark-parse'; | ||
import rehype from 'remark-rehype'; | ||
import raw from 'rehype-raw'; | ||
import { Plugin } from 'bytemd'; | ||
|
||
export function getParser(plugins: Plugin[]) { | ||
let parser = unified() | ||
.use(markdown) | ||
.use(rehype, { allowDangerousHTML: true }); | ||
|
||
plugins.forEach((p) => { | ||
if (Array.isArray(p.transformer)) { | ||
parser = parser.use(...p.transformer); | ||
} else if (p.transformer) { | ||
parser = parser.use(p.transformer); | ||
} | ||
}); | ||
|
||
return parser.use(raw); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { createEventDispatcher } from 'svelte'; | ||
import codemirror from 'codemirror'; | ||
import 'codemirror/mode/markdown/markdown.js'; | ||
import { EditorProps } from 'bytemd'; | ||
|
||
export function initEditor( | ||
textarea: HTMLTextAreaElement, | ||
editorConfig: any, | ||
value: string, | ||
viewer: HTMLElement, | ||
fileHandler: NonNullable<EditorProps['fileHandler']> | ||
) { | ||
const dispatch = createEventDispatcher(); | ||
|
||
const cm = codemirror.fromTextArea(textarea, { | ||
mode: 'markdown', | ||
lineNumbers: true, | ||
lineWrapping: true, | ||
...editorConfig, | ||
}); | ||
cm.setValue(value); | ||
cm.on('change', (doc, change) => { | ||
if (change.origin !== 'setValue') { | ||
dispatch('change', { value: cm.getValue() }); | ||
} | ||
}); | ||
cm.on('scroll', (cm) => { | ||
requestAnimationFrame(() => { | ||
const editorInfo = cm.getScrollInfo(); | ||
const ratio = | ||
editorInfo.top / (editorInfo.height - editorInfo.clientHeight); | ||
viewer.scrollTo(0, ratio * (viewer.scrollHeight - viewer.clientHeight)); | ||
}); | ||
}); | ||
cm.on('paste', async (_, e) => { | ||
const { items } = e.clipboardData; | ||
for (let i = 0; i < items.length; i++) { | ||
// console.log(items[i]); | ||
if (!items[i].type.startsWith('image/')) continue; | ||
|
||
e.preventDefault(); | ||
const url = await fileHandler(items[i].getAsFile()); | ||
const text = cm.getSelection(); | ||
cm.replaceSelection(`![${text}](${url})`); | ||
cm.focus(); | ||
return; | ||
} | ||
}); | ||
cm.on('drop', async (_, e) => { | ||
const { items } = e.dataTransfer; | ||
for (let i = 0; i < items.length; i++) { | ||
if (!items[i].type.startsWith('image/')) continue; | ||
|
||
e.preventDefault(); | ||
const url = await fileHandler(items[i].getAsFile()); | ||
const text = cm.getSelection(); | ||
cm.replaceSelection(`![${text}](${url})`); | ||
cm.focus(); | ||
return; | ||
} | ||
}); | ||
|
||
return cm; | ||
} | ||
|
||
export const dataUrlFileHandler: Exclude< | ||
EditorProps['fileHandler'], | ||
undefined | ||
> = async (file) => { | ||
return new Promise((resolve, reject) => { | ||
const reader = new FileReader(); | ||
reader.addEventListener('load', (e) => { | ||
resolve(e.target!.result as string); | ||
}); | ||
reader.addEventListener('error', (e) => { | ||
reject(new Error('readAsDataURL error')); | ||
}); | ||
reader.readAsDataURL(file); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Plugin } from 'bytemd'; | ||
import { HastNode } from '../../helpers'; | ||
|
||
export function findPlugin(node: HastNode, plugins: Plugin[]) { | ||
for (let i = 0; i < plugins.length; i++) { | ||
const { renderNode } = plugins[i]; | ||
if (renderNode) { | ||
return renderNode(node); | ||
} | ||
} | ||
} | ||
|
||
export const santitizeHref = (href?: string) => { | ||
if (href && href.trim().toLowerCase().startsWith('javascript')) { | ||
return; | ||
} else { | ||
return href; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
export { getParser } from './common'; | ||
export { initEditor, dataUrlFileHandler } from './editor'; | ||
export { findPlugin, santitizeHref } from './element'; | ||
export { | ||
handleDec, | ||
handleTag, | ||
handleBlockquote, | ||
handleLink, | ||
handleImage, | ||
handleTable, | ||
handleHeading, | ||
handleOl, | ||
handleUl, | ||
handleTask, | ||
covertToHtml, | ||
} from './toolbar'; | ||
|
||
import heading from 'icons/heading-16.svg'; | ||
import bold from 'icons/bold-16.svg'; | ||
import italic from 'icons/italic-16.svg'; | ||
import quote from 'icons/quote-16.svg'; | ||
import image from 'icons/image-16.svg'; | ||
import server from 'icons/server-16.svg'; | ||
import link from 'icons/link-16.svg'; | ||
import iconOl from 'icons/list-ordered-16.svg'; | ||
import iconUl from 'icons/list-unordered-16.svg'; | ||
import task from 'icons/tasklist-16.svg'; | ||
import html from 'icons/code-review-16.svg'; | ||
export { | ||
heading, | ||
bold, | ||
italic, | ||
quote, | ||
image, | ||
server, | ||
link, | ||
iconOl, | ||
iconUl, | ||
task, | ||
html, | ||
}; |
Oops, something went wrong.