Skip to content

Commit

Permalink
Showing 6 changed files with 26 additions and 3 deletions.
5 changes: 5 additions & 0 deletions examples/scripts/index-demo.js
Original file line number Diff line number Diff line change
@@ -196,6 +196,11 @@ var basicConfig = {
//extensions: [],
callback: {
changeString2Pinyin: pinyin,
},
editor: {
id: 'cherry-text',
name: 'cherry-text',
autoSave2Textarea: true,
}
};

3 changes: 3 additions & 0 deletions src/Cherry.config.js
Original file line number Diff line number Diff line change
@@ -191,6 +191,9 @@ const defaultConfig = {
},
},
editor: {
id: 'code', // textarea 的id属性值
name: 'code', // textarea 的name属性值
autoSave2Textarea: false, // 是否自动将编辑区的内容回写到textarea里
theme: 'default', // depend on codemirror theme name: https://codemirror.net/demo/theme.htm
// 编辑器的高度,默认100%,如果挂载点存在内联设置的height则以内联样式为主
height: '100%',
5 changes: 4 additions & 1 deletion src/Cherry.js
Original file line number Diff line number Diff line change
@@ -453,7 +453,10 @@ export default class Cherry extends CherryStatic {
* @returns {import('@/Editor').default}
*/
createEditor() {
const textArea = createElement('textarea', '', { id: 'code', name: 'code' });
const textArea = createElement('textarea', '', {
id: this.options.editor.id ?? 'code',
name: this.options.editor.name ?? 'code',
});
textArea.textContent = this.options.value;
const editor = createElement('div', 'cherry-editor');
editor.appendChild(textArea);
9 changes: 8 additions & 1 deletion src/Editor.js
Original file line number Diff line number Diff line change
@@ -59,7 +59,9 @@ export default class Editor {
* @type {EditorConfiguration}
*/
this.options = {
id: 'code',
id: 'code', // textarea 的id属性值
name: 'code', // textarea 的name属性值
autoSave2Textarea: false,
editorDom: document.createElement('div'),
wrapperDom: null,
autoScrollByCursor: true,
@@ -332,6 +334,11 @@ export default class Editor {
editor.on('change', (codemirror, evt) => {
this.options.onChange(evt, codemirror);
this.dealBigData();
if (this.options.autoSave2Textarea) {
// @ts-ignore
// 将codemirror里的内容回写到textarea里
codemirror.save();
}
});

editor.on('keydown', (codemirror, evt) => {
3 changes: 3 additions & 0 deletions types/cherry.d.ts
Original file line number Diff line number Diff line change
@@ -116,6 +116,9 @@ export type EditorMode =
| 'edit&preview';

export interface CherryEditorOptions {
id?: string; // textarea 的id属性值
name?: string; // textarea 的name属性值
autoSave2Textarea?: boolean; // 是否自动将编辑区的内容回写到textarea里
/** depends on codemirror theme name: https://codemirror.net/demo/theme.htm */
theme?: string;
/** 编辑器的高度,默认100%,如果挂载点存在内联设置的height则以内联样式为主 */
4 changes: 3 additions & 1 deletion types/editor.d.ts
Original file line number Diff line number Diff line change
@@ -38,7 +38,9 @@ type EditorPasteEventHandler = (
) => void;

export type EditorConfiguration = {
id: string;
id?: string; // textarea 的id属性值
name?: string; // textarea 的name属性值
autoSave2Textarea?: boolean; // 是否自动将编辑区的内容回写到textarea里
editorDom: HTMLElement;
wrapperDom: HTMLElement;
toolbars: any;

0 comments on commit b20c260

Please sign in to comment.