From 518b7642a013dacf2cafbb984359eaba94919b6e Mon Sep 17 00:00:00 2001 From: wanglin2 <1013335014@qq.com> Date: Fri, 22 Sep 2023 10:57:32 +0800 Subject: [PATCH] =?UTF-8?q?Demo=EF=BC=9A=E4=BF=AE=E5=A4=8D=E5=A4=8D?= =?UTF-8?q?=E5=88=B6=E7=9F=A5=E7=8A=80=E6=80=9D=E7=BB=B4=E5=AF=BC=E5=9B=BE?= =?UTF-8?q?=E5=A4=9A=E4=B8=AA=E8=8A=82=E7=82=B9=E6=97=B6=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E7=B2=98=E8=B4=B4=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/utils/handleClipboardText.js | 112 ++++++++++++++++----------- 1 file changed, 66 insertions(+), 46 deletions(-) diff --git a/web/src/utils/handleClipboardText.js b/web/src/utils/handleClipboardText.js index 4a9f5623..35671c72 100644 --- a/web/src/utils/handleClipboardText.js +++ b/web/src/utils/handleClipboardText.js @@ -1,57 +1,69 @@ import { imgToDataUrl } from 'simple-mind-map/src/utils/index' // 处理知犀 -const handleZHIXI = async text => { - text = text.replace('￿', '') +const handleZHIXI = async data => { try { - // 只处理一项 - const node = JSON.parse(text)[0] - const newNode = {} - const waitLoadImageList = [] - const walk = async (root, newRoot) => { - newRoot.data = { - text: root.data.text, - hyperlink: root.data.hyperlink, - hyperlinkTitle: root.data.hyperlinkTitle, - note: root.data.note - } - // 图片 - if (root.data.image) { - let resolve = null - let promise = new Promise(_resolve => { - resolve = _resolve - }) - waitLoadImageList.push(promise) - try { - newRoot.data.image = await imgToDataUrl(root.data.image) - newRoot.data.imageSize = root.data.imageSize - resolve() - } catch (error) { - resolve() - } - } - // 子节点 - newRoot.children = [] - if (root.children && root.children.length > 0) { - root.children.forEach(item => { - // 概要 - if (item.data.type === 'generalize') { - newRoot.data.generalization = { - text: item.data.text - } - return - } - let newChild = {} - newRoot.children.push(newChild) - walk(item, newChild) - }) + try { + if (!Array.isArray(data)) { + data = String(data).replace('￿', '') + data = JSON.parse(data) } + } catch (error) { + console.log(error) } - walk(node, newNode) + if (!Array.isArray(data)) { + data = [] + } + const newNodeList = [] + const waitLoadImageList = [] + const walk = (list, newList) => { + list.forEach(async item => { + let newRoot = {} + newList.push(newRoot) + newRoot.data = { + text: item.data.text, + hyperlink: item.data.hyperlink, + hyperlinkTitle: item.data.hyperlinkTitle, + note: item.data.note + } + // 图片 + if (item.data.image) { + let resolve = null + let promise = new Promise(_resolve => { + resolve = _resolve + }) + waitLoadImageList.push(promise) + try { + newRoot.data.image = await imgToDataUrl(item.data.image) + newRoot.data.imageSize = item.data.imageSize + resolve() + } catch (error) { + resolve() + } + } + // 子节点 + newRoot.children = [] + if (item.children && item.children.length > 0) { + const children = [] + item.children.forEach(item2 => { + // 概要 + if (item2.data.type === 'generalize') { + newRoot.data.generalization = { + text: item2.data.text + } + } else { + children.push(item2) + } + }) + walk(children, newRoot.children) + } + }) + } + walk(data, newNodeList) await Promise.all(waitLoadImageList) return { simpleMindMap: true, - data: newNode + data: newNodeList } } catch (error) { return '' @@ -59,7 +71,15 @@ const handleZHIXI = async text => { } const handleClipboardText = async text => { - // 处理知犀数据 + // 知犀数据格式1 + try { + let parsedData = JSON.parse(text) + if (parsedData.__c_zx_v !== undefined) { + const res = await handleZHIXI(parsedData.children) + return res + } + } catch (error) {} + // 知犀数据格式2 if (text.includes('￿')) { const res = await handleZHIXI(text) return res