import React, {MouseEvent, useRef, useState} from "react"; import './plugins/index.ts' import {Editor} from '@wangeditor/editor-for-react' import {IDomEditor} from "@wangeditor/editor"; import {Descendant} from "slate"; import '@wangeditor/editor/dist/css/style.css' import './style.less' import {PROOFREAD_TYPE_DESC, WangEditorContent, WangEditorProofreadElement} from "../../types/editor.ts"; import {useMount} from "ahooks"; import {Button} from "antd"; import * as classNames from "classnames"; import IconArrowRight from "../../components/icons/IconArrowRight.tsx"; import IconCheckFill from "../../components/icons/IconCheckFill.tsx"; import IconCancelFill from "../../components/icons/IconCancelFill.tsx"; import {ProofreadPopover} from "./components/ProofreadPopover.tsx"; // { // "attributes": { // replace: { // origin: '湖北省张家界市', // text: '湖南省张家界市', // type: 'address', // }, // // "bold":true, // }, // "insert": "湖北省张家界市", // } const defaultData: WangEditorContent[] = [ { "type": "paragraph", "children": [ {"text": "《", "fontFamily": "宋体"}, { "text": "皇帝的新装", "bold": true, "color": "rgb(106, 57, 201)" }, {"text": "》的作者是", "fontFamily": "宋体"}, { "text": "闻名世界", "bold": true, "italic": true, "underline": true, "through": true, "fontSize": "18pt" }, {"text": "的", "fontFamily": "宋体"}, { type: 'proofread', proofread: { origin: '湖北省张家界市', text: '湖南省张家界市', type: 'address', }, children: [ {"text": "湖北省张家界市"}, ] }, { "text": "丹麦作家", "bold": true, "color": "rgb(54, 88, 226)" }, { type: 'proofread', proofread: { origin: '安徒声', text: '安徒生', type: 'words', }, children: [ {"text": "安徒声"}, ] }, {"text": "的作品。", "fontFamily": "宋体"} ] }, { "type": "paragraph", "children": [ { "text": "故事里有一个愚蠢的笨国王。他罕少关心国家,一昧追求的就是衣着入时。", }, { "text": "有一天,王国里来了俩个骗子。", "fontFamily": "宋体" } ] } ] const WangEditor: React.FC = () => { const [html, setHtml] = useState() const [content, setContent] = useState() const [editor, setEditor] = useState() // 存储 editor 实例 const [ops, setOps] = useState([]) const [currentIndex, setCurrentIndex] = useState(-1); const editorConfig = { placeholder: '请输入内容...', } const onMounted = (editor: IDomEditor) => { setEditor(editor) editor.children = defaultData editor.on('select', (a, b) => { console.log('select=》a,b', a, b) }) } const parseDesc = (data: WangEditorProofreadElement) => { const {type, origin, text} = data.proofread const desc = PROOFREAD_TYPE_DESC[type]; return desc.replace(/%origin%/ig, origin).replace(/%text%/ig, text) } const divRef = useRef(null) const handleDivClick = (e: MouseEvent) => { if (divRef.current) { const target = e.target as HTMLElement; if (target.classList.contains('data-proofread-item')) { target.classList.add('data-proofread-item-selected') return; } const selectArr = divRef.current.querySelectorAll('.data-proofread-item-selected') Array.from(selectArr).forEach(it => it.classList.remove('data-proofread-item-selected')) setCurrentIndex(-1) } } useMount(() => { const _arr: WangEditorProofreadElement[] = []; let index = 0; defaultData.forEach((it) => { if (it.type == 'paragraph' && it.children && it.children.length > 0) { it.children.forEach(cit => { if (Object.hasOwn(cit, 'type')) { const data = cit as WangEditorProofreadElement; if (data.type && data.type == 'proofread') { data.proofread.id = index++; data.proofread.description = parseDesc(data) _arr.push(data) } } }) } }) setOps(_arr) if (divRef.current) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore divRef.current.addEventListener('proofread-click', handleDivClick) } }) const setSelectIndex = (index: number) => { setCurrentIndex(index) const span = document.querySelector(`.data-proofread-item-${index}`) if (span) { if (span.classList.contains('data-proofread-item-selected')) return; document.querySelector(`.data-proofread-item-selected`)?.classList.remove('data-proofread-item-selected') span.classList.add('data-proofread-item-selected') } } return (
{ const op = editor.operations[0] if (op?.type == 'set_selection') { // console.log('onchange->', JSON.stringify(editor.selection)) } else { setHtml(editor.getHtml()) setContent(editor.children) // editor.getFragment } }} mode="default" />
{ops.map((op, key) => (
setSelectIndex(Number(op.proofread.id))} >
{op.proofread.origin} {op.proofread.text}
{op.proofread.description}
选用}> 忽略}>
))}
{JSON.stringify(content)}
) } export default WangEditor;