# Example usage in the framework This is just a simple example and is not a complete answer. If there are any examples or errors in other frameworks, please participate and correct them. - [React](#react-component) - [React class](#react-class) - [Vue](#vue-component) ## React Component ### React class ### 1. Editor.tsx ```typescript import React, { Component, createRef } from "react"; import suneditor from "suneditor"; import { en } from "suneditor/src/lang"; import plugins from "suneditor/src/plugins"; import CodeMirror from "codemirror"; import katex from "katex"; import "suneditor/dist/css/suneditor.min.css"; import "codemirror/mode/htmlmixed/htmlmixed"; import "codemirror/lib/codemirror.css"; import "katex/dist/katex.min.css"; import "./Editor.scss" interface Props { contents?: string; onBlur?: Function; onSave: Function; } interface State { imageList: any[]; selectedImages: any[]; imageSize: string; } class Editor extends Component { txtArea: any; editor: any; constructor(props: any) { super(props); this.txtArea = createRef(); this.state = { imageList: [], selectedImages: [], imageSize: "0KB", }; } componentDidMount() { const editor: any = this.editor = suneditor.create(this.txtArea.current, { plugins: plugins, lang: en, callBackSave: (contents: string) => this.props.onSave(contents), codeMirror: CodeMirror, stickyToolbar: 0, katex: katex, width: '100%', height: 'auto', minHeight: '400px', value: this.props.contents, // imageUploadUrl: `url`, imageMultipleFile: true, previewTemplate: `
{{contents}}
`, buttonList: [ // default ['undo', 'redo'], ['font', 'fontSize', 'formatBlock'], ['paragraphStyle', 'blockquote'], ['bold', 'underline', 'italic', 'strike', 'subscript', 'superscript'], ['fontColor', 'hiliteColor', 'textStyle'], ['removeFormat'], ['outdent', 'indent'], ['align', 'horizontalRule', 'list', 'lineHeight'], ['table', 'link', 'image', 'video'], ['fullScreen', 'showBlocks', 'codeView'], ['preview'], ['save'], // responsive ['%1161', [ ['undo', 'redo'], [':p-Formats-default.more_paragraph', 'font', 'fontSize', 'formatBlock', 'paragraphStyle', 'blockquote'], ['bold', 'underline', 'italic', 'strike', 'subscript', 'superscript'], ['fontColor', 'hiliteColor', 'textStyle'], ['removeFormat'], ['outdent', 'indent'], ['align', 'horizontalRule', 'list', 'lineHeight'], ['-right', 'save'], ['-right', ':i-Etc-default.more_vertical', 'fullScreen', 'showBlocks', 'codeView', 'preview'], ['-right', ':r-Table&Media-default.more_plus', 'table', 'link', 'image', 'video'], ]], ['%893', [ ['undo', 'redo'], [':p-Formats-default.more_paragraph', 'font', 'fontSize', 'formatBlock', 'paragraphStyle', 'blockquote'], ['bold', 'underline', 'italic', 'strike'], [':t-Fonts-default.more_text', 'subscript', 'superscript', 'fontColor', 'hiliteColor', 'textStyle'], ['removeFormat'], ['outdent', 'indent'], ['align', 'horizontalRule', 'list', 'lineHeight'], ['-right', 'save'], ['-right', ':i-Etc-default.more_vertical', 'fullScreen', 'showBlocks', 'codeView', 'preview'], ['-right', ':r-Table&Media-default.more_plus', 'table', 'link', 'image', 'video'], ]], ['%855', [ ['undo', 'redo'], [':p-Formats-default.more_paragraph', 'font', 'fontSize', 'formatBlock', 'paragraphStyle', 'blockquote'], [':t-Fonts-default.more_text', 'bold', 'underline', 'italic', 'strike', 'subscript', 'superscript', 'fontColor', 'hiliteColor', 'textStyle'], ['removeFormat'], ['outdent', 'indent'], ['align', 'horizontalRule', 'list', 'lineHeight'], [':r-Table&Media-default.more_plus', 'table', 'link', 'image', 'video'], ['-right', 'save'], ['-right', ':i-Etc-default.more_vertical', 'fullScreen', 'showBlocks', 'codeView', 'preview'], ]], ['%563', [ ['undo', 'redo'], [':p-Formats-default.more_paragraph', 'font', 'fontSize', 'formatBlock', 'paragraphStyle', 'blockquote'], [':t-Fonts-default.more_text', 'bold', 'underline', 'italic', 'strike', 'subscript', 'superscript', 'fontColor', 'hiliteColor', 'textStyle'], ['removeFormat'], ['outdent', 'indent'], [':e-List&Line-default.more_horizontal', 'align', 'horizontalRule', 'list', 'lineHeight'], [':r-Table&Media-default.more_plus', 'table', 'link', 'image', 'video'], ['-right', 'save'], ['-right', ':i-Etc-default.more_vertical', 'fullScreen', 'showBlocks', 'codeView', 'preview'], ]], ['%458', [ ['undo', 'redo'], [':p-Formats-default.more_paragraph', 'font', 'fontSize', 'formatBlock', 'paragraphStyle', 'blockquote'], [':t-Fonts-default.more_text', 'bold', 'underline', 'italic', 'strike', 'subscript', 'superscript', 'fontColor', 'hiliteColor', 'textStyle', 'removeFormat'], [':e-List&Line-default.more_horizontal', 'outdent', 'indent', 'align', 'horizontalRule', 'list', 'lineHeight'], [':r-Table&Media-default.more_plus', 'table', 'link', 'image', 'video'], ['-right', 'save'], ['-right', ':i-Etc-default.more_vertical', 'fullScreen', 'showBlocks', 'codeView', 'preview'], ]] ] }); editor.onBlur = () => { if (typeof this.props.onBlur === 'function') this.props.onBlur() } editor.onImageUpload = this.imageUpload.bind(this); // editor.onVideoUpload = videoUpload; } componentDidUpdate(prevProps: any) { if (this.props.contents !== prevProps.contents) { this.editor.setContents(this.props.contents); this.editor.core.history.reset(true); } } componentWillUnmount() { if (this.editor) this.editor.destroy(); } // image, video findIndex(arr: any[], index: number) { let idx = -1; arr.some(function (a, i) { if ((typeof a === 'number' ? a : a.index) === index) { idx = i; return true; } return false; }) return idx; } imageUpload(targetElement: Element, index: number, state: string, imageInfo: Record, remainingFilesCount: number) { if (state === 'delete') { this.state.imageList.splice(this.findIndex(this.state.imageList, index), 1) this.setState({ imageList: this.state.imageList }) } else { if (state === 'create') { const imageList = this.state.imageList; imageList.push(imageInfo) this.setState({ imageList: imageList }) } else { // update // } } if (remainingFilesCount === 0) { this.setImageList() } } setImageList() { const imageList = this.state.imageList; let size = 0; for (let i = 0; i < imageList.length; i++) { size += Number((imageList[i].size / 1000).toFixed(1)); } this.setState({ imageSize: size.toFixed(1) + 'KB' }) } selectImage(evt: any, type: string, index: number) { evt.preventDefault(); evt.stopPropagation(); this.state.imageList[this.findIndex(this.state.imageList, index)][type](); } checkImage(index: number) { const selectedImages = this.state.selectedImages; const currentImageIdx = this.findIndex(selectedImages, index) if (currentImageIdx > -1) { selectedImages.splice(currentImageIdx, 1) } else { selectedImages.push(index) } this.setState({ selectedImages: selectedImages }) } deleteCheckedImages() { const iamgesInfo = this.editor.getImagesInfo(); for (let i = 0; i < iamgesInfo.length; i++) { if (this.state.selectedImages.indexOf(iamgesInfo[i].index as number) > -1) { iamgesInfo[i].delete(); i--; } } this.setState({ selectedImages: [], }) } fileUploadToEditor(e: any) { if (e.target.files) { this.editor.insertImage(e.target.files) e.target.value = '' } } render() { return