Commit cdfc4481 by pangchong

feat: 选中编辑器内容,可以定位菜单

parent 433e1346
...@@ -17,6 +17,7 @@ declare module 'vue' { ...@@ -17,6 +17,7 @@ declare module 'vue' {
NLayoutFooter: typeof import('naive-ui')['NLayoutFooter'] NLayoutFooter: typeof import('naive-ui')['NLayoutFooter']
NLayoutHeader: typeof import('naive-ui')['NLayoutHeader'] NLayoutHeader: typeof import('naive-ui')['NLayoutHeader']
NMessageProvider: typeof import('naive-ui')['NMessageProvider'] NMessageProvider: typeof import('naive-ui')['NMessageProvider']
NPopover: typeof import('naive-ui')['NPopover']
NResult: typeof import('naive-ui')['NResult'] NResult: typeof import('naive-ui')['NResult']
NSpace: typeof import('naive-ui')['NSpace'] NSpace: typeof import('naive-ui')['NSpace']
NSpin: typeof import('naive-ui')['NSpin'] NSpin: typeof import('naive-ui')['NSpin']
......
...@@ -19,14 +19,17 @@ ...@@ -19,14 +19,17 @@
:style="editorStyle" :style="editorStyle"
@on-change="handleChange" @on-change="handleChange"
@on-created="handleCreated" @on-created="handleCreated"
@click="handleClick"
@contextmenu.prevent="handleContextMenu"
/> />
<!-- <n-popover :show="showPopover" :x="x" :y="y" trigger="manual">厉害!</n-popover> -->
</n-card> </n-card>
</div> </div>
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { IDomEditor, IEditorConfig } from '@wangeditor/editor' import { DomEditor, IDomEditor, IEditorConfig } from '@wangeditor/editor'
// @ts-ignore // @ts-ignore
import { Editor, Toolbar } from '@wangeditor/editor-for-vue' import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { isNumber } from 'lodash' import { isNumber } from 'lodash'
...@@ -97,7 +100,7 @@ const ps = defineProps({ ...@@ -97,7 +100,7 @@ const ps = defineProps({
default: false default: false
} }
}) })
const es = defineEmits(['change', 'created', 'update:modelValue']) const es = defineEmits(['change', 'created', 'update:modelValue', 'handleClick'])
// 编辑器实例,必须用 shallowRef // 编辑器实例,必须用 shallowRef
const editorRef = shallowRef<IDomEditor>() const editorRef = shallowRef<IDomEditor>()
const valueHtml = ref('') const valueHtml = ref('')
...@@ -122,6 +125,24 @@ const handleCreated = (editor: IDomEditor) => { ...@@ -122,6 +125,24 @@ const handleCreated = (editor: IDomEditor) => {
editorRef.value = editor editorRef.value = editor
es('created', editor) es('created', editor)
} }
//点击
const handleClick = (event: any) => {
const key = getKey(event.target)
es('handleClick', key)
}
//获取key
const getKey = (elem: any): string => {
const key = elem.getAttribute('data-key')
if (key && elem != null) {
return key
} else {
elem = elem.parentElement
return getKey(elem)
}
}
const handleContextMenu = () => {
console.log('右键菜单')
}
// 编辑器配置 // 编辑器配置
const message = useMessage() const message = useMessage()
const editorConfig = computed((): IEditorConfig => { const editorConfig = computed((): IEditorConfig => {
......
...@@ -12,20 +12,20 @@ ...@@ -12,20 +12,20 @@
v-model:selected-keys="treeSelectedKeys" v-model:selected-keys="treeSelectedKeys"
:pattern="searchKey" :pattern="searchKey"
block-line block-line
virtual-scroll
/> />
</div> </div>
</div> </div>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { realComposableData, searchKey, treeData, xmlContent, xmlDOM, treeSelectedKeys } from '../constants' import { realComposableData, searchKey, treeData, xmlContent, xmlDOM, treeSelectedKeys, treeRef } from '../constants'
import { nodeSet } from '@/views/editor/constants/nodeParsed.ts' import { nodeSet } from '@/views/editor/constants/nodeParsed.ts'
import FileXML from '@/assets/file/CES-QEC-V250-A.xml?raw' import FileXML from '@/assets/file/CES-QEC-V250-A.xml?raw'
import type { TreeOption } from 'naive-ui' import type { TreeOption } from 'naive-ui'
import { nodeProps } from '../functions' import { nodeProps } from '../functions'
const xmlProcessing = useXMLProcessing() const xmlProcessing = useXMLProcessing()
const treeRef = ref()
const expandedKeys = ref<string[]>([]) const expandedKeys = ref<string[]>([])
onMounted(function () { onMounted(function () {
// const res = xmlProcessing.processXML(FileXML, nodeSet) // const res = xmlProcessing.processXML(FileXML, nodeSet)
......
...@@ -14,6 +14,7 @@ export const editorRef = ref() ...@@ -14,6 +14,7 @@ export const editorRef = ref()
export const formData = reactive({ export const formData = reactive({
html: '' html: ''
}) })
export const treeRef = ref()
// 递归函数:检查树中是否存在满足条件的节点 // 递归函数:检查树中是否存在满足条件的节点
// 递归函数:检查树中是否存在满足条件的节点,并构建新的树结构 // 递归函数:检查树中是否存在满足条件的节点,并构建新的树结构
function buildFilteredTree(tree: TreeRenderResult, searchString: string): TreeRenderResult | null { function buildFilteredTree(tree: TreeRenderResult, searchString: string): TreeRenderResult | null {
......
import { IDomEditor } from '@wangeditor/editor' import { IDomEditor } from '@wangeditor/editor'
import { editorRef, showLoading, treeData, xmlContent, xmlDOM } from '../constants' import { editorRef, showLoading, treeData, treeRef, treeSelectedKeys, xmlContent, xmlDOM } from '../constants'
import { TreeOption } from 'naive-ui' import { TreeOption } from 'naive-ui'
import { nodeSet } from '../constants/nodeParsed' import { nodeSet } from '../constants/nodeParsed'
...@@ -12,12 +12,18 @@ export const handleEditor = (editor: IDomEditor) => { ...@@ -12,12 +12,18 @@ export const handleEditor = (editor: IDomEditor) => {
// xmlContent.value = res.xmlContent // xmlContent.value = res.xmlContent
} }
export const handleClick = (key: string) => {
treeRef.value?.scrollTo({ key })
treeSelectedKeys.value = [key]
}
let lastFocusedId = '' let lastFocusedId = ''
export const nodeProps = ({ option }: { option: TreeOption }) => { export const nodeProps = ({ option }: { option: TreeOption }) => {
return { return {
onClick() { onClick() {
editorRef.value?.editorRef.scrollToElem([option.key]) const container = editorRef.value?.editorRef.getEditableContainer()
let container = editorRef.value?.editorRef?.getEditableContainer() const id = container.querySelector(`[data-key="${option.key}"]`)?.getAttribute('id')
editorRef.value?.editorRef.scrollToElem([id])
if (container) { if (container) {
if (lastFocusedId) { if (lastFocusedId) {
container.querySelector(`#${lastFocusedId}`)?.classList.remove('focus') container.querySelector(`#${lastFocusedId}`)?.classList.remove('focus')
......
...@@ -9,7 +9,7 @@ const renderElem = (type: string, style = { display: 'block' }) => { ...@@ -9,7 +9,7 @@ const renderElem = (type: string, style = { display: 'block' }) => {
// @ts-ignore // @ts-ignore
paddingLeft: Number(elem.dataIndent) * 5 + 'px' paddingLeft: Number(elem.dataIndent) * 5 + 'px'
}) })
return h(type, { style, attrs: { dataKey: dataKey, id: dataKey } }, children) return h(type, { style, attrs: { 'data-key': dataKey } }, children)
} }
} }
const createRenderConfig = (types: string | string[]) => { const createRenderConfig = (types: string | string[]) => {
......
<template> <template>
<n-spin class="h-full w-full" content-class="h-full w-full" :show="showLoading" description="加载中..."> <n-spin class="h-full w-full" content-class="h-full w-full" :show="showLoading" description="加载中...">
<ContentEditor ref="editorRef" v-model="formData.html" @change="handleEditor" @created="handleEditor"> <ContentEditor ref="editorRef" v-model="formData.html" @change="handleEditor" @created="handleEditor" @handleClick="handleClick">
<template #left> <template #left>
<n-card class="h-full min-w-[300px] max-w-[300px] mr-[15px]" content-class="bg-baseColor !p-0 overflow-hidden"> <n-card class="h-full min-w-[300px] max-w-[300px] mr-[15px]" content-class="bg-baseColor !p-0 overflow-hidden">
<ContentTree></ContentTree> <ContentTree></ContentTree>
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
import ContentEditor from './components/ContentEditor.vue' import ContentEditor from './components/ContentEditor.vue'
import ContentTree from './components/ContentTree.vue' import ContentTree from './components/ContentTree.vue'
import { editorRef, formData, showLoading, xmlContent } from './constants' import { editorRef, formData, showLoading, xmlContent } from './constants'
import { handleEditor } from './functions' import { handleClick, handleEditor } from './functions'
onMounted(() => { onMounted(() => {
// nextTick(() => { // nextTick(() => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment