Skip to content

Commit

Permalink
feat: #714 增加粘贴的回调
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsonliu committed Mar 13, 2024
1 parent 0570a6d commit e9154f6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions examples/scripts/preview-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ var cherryConfig = {
},
toolbars: {
toolbar: false,
// 配置目录
toc: {
updateLocationHash: false, // 要不要更新URL的hash
defaultModel: 'full', // pure: 精简模式/缩略模式,只有一排小点; full: 完整模式,会展示所有标题
},
},
editor: {
defaultModel: 'previewOnly',
Expand Down
18 changes: 18 additions & 0 deletions src/Cherry.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ const callbacks = {
afterInit: (text, html) => {},
beforeImageMounted: (srcProp, src) => ({ srcProp, src }),
onClickPreview: (event) => {},
/**
* 粘贴时触发
* @param {ClipboardEvent['clipboardData']} clipboardData
* @returns
* false: 走cherry粘贴的默认逻辑
* string: 直接粘贴的内容
*/
onPaste: (clipboardData) => {
return false;
},
onCopyCode: (event, code) => {
// 阻止默认的粘贴事件
// return false;
Expand Down Expand Up @@ -346,6 +356,14 @@ const defaultConfig = {
onCopyCode: callbacks.onCopyCode,
// 把中文变成拼音的回调,当然也可以把中文变成英文、英文变成中文
changeString2Pinyin: callbacks.changeString2Pinyin,
/**
* 粘贴时触发
* @param {ClipboardEvent['clipboardData']} clipboardData
* @returns
* false: 走cherry粘贴的默认逻辑
* string: 直接粘贴的内容
*/
onPaste: callbacks.onPaste,
},
previewer: {
dom: false,
Expand Down
6 changes: 6 additions & 0 deletions src/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,12 @@ export default class Editor {
* @returns {boolean | void}
*/
handlePaste(event, clipboardData, codemirror) {
const onPasteRet = this.$cherry.options.callback.onPaste(clipboardData);
if (onPasteRet !== false && typeof onPasteRet === 'string') {
event.preventDefault();
codemirror.replaceSelection(onPasteRet);
return;
}
let html = clipboardData.getData('Text/Html');
const { items } = clipboardData;
// 清空注释
Expand Down
1 change: 1 addition & 0 deletions types/cherry.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface CherryOptions {
onClickPreview: (e: MouseEvent) => void;
onCopyCode: (e: ClipboardEvent, code: string) => string|false;
changeString2Pinyin: (str: string) => string;
onPaste: (clipboardData: ClipboardEvent['clipboardData']) => string|boolean;
};
/** 预览区域配置 */
previewer: CherryPreviewerOptions;
Expand Down

0 comments on commit e9154f6

Please sign in to comment.