feat: 点击事件

This commit is contained in:
金建 2022-10-23 23:27:44 +08:00
parent e16863930f
commit 09e8dfedbe
6 changed files with 252 additions and 12 deletions

View File

@ -1,31 +1,255 @@
import { CreateComponentType, EventLife } from '@/packages/index.d' import { CreateComponentType, EventLife } from '@/packages/index.d'
import * as echarts from 'echarts' import * as echarts from 'echarts'
import { BackEndFactory } from '@/backend/ibackend' import { BackEndFactory } from '@/backend/ibackend'
import { reactive, toRef , watch, computed} from 'vue';
/**
*
*
const range = runtime.fn.selectComponents("饼图 柱状图")
const h = runtime.fn.getChartConfig(range, "hide")
runtime.fn.setChartConfig(range, "hide", !h)
001 id 2wolqibrx3c000
runtime.fn.setChartConfig("柱状图001", "dataset", {"dimensions":["product","data1","data2"],"source":[{"product":"Mon","data1":120,"data2":130}]})
runtime.fn.setChartConfig("#2wolqibrx3c000", "dataset", {"dimensions":["product","data1","data2"],"source":[{"product":"Mon","data1":120,"data2":230}]})
const c = runtime.fn.selectOneComponent("分组")
if(c){
console.log(runtime.fn.getChartConfig(c, "isGroup" ))
runtime.fn.setChartConfig(c, "hide", true)
}
exposed
defineExpose({ actionTest:actionTest })
actionTest
runtime.fn.callExposed("柱状图", "actionTest")
A MOUNTED status1 Watch = "0"
watch(()=>runtime.variables.status1, newValue => runtime.fn.setChartConfig(this, "hide", newValue == "0"))
B MOUNTED status1 Watch = "1"
watch(()=>runtime.variables.status1, newValue => runtime.fn.setChartConfig(this, "hide", newValue == "1"))
A B
if(runtime.variables.status1 == "0"){
runtime.variables.status1 = "1"
} else{
runtime.variables.status1 = "0"
}
A MOUNTED data1 Watch
watch(()=>runtime.datasets.data1,
newValue => runtime.fn.setChartConfig(this, "dataset", newValue))
B MOUNTED data1 Watch
watch(()=>runtime.datasets.data1,
newValue => runtime.fn.setChartConfig(this, "dataset", newValue))
datasets.data1A B
runtime.datasets.data1 = {"dimensions":["product","data1","data2"],"source":[{"product":"Mon","data1":120,"data2":230}]}
*
*/
// * 初始化 // * 初始化
export const useSystemInit = async () => { export const useSystemInit = async () => {
const res = await BackEndFactory.init({}) as any; const res = await BackEndFactory.init({}) as any;
} }
const getOneChartConfig = (component:any, configName:string, params?:any)=>{
let root = null
if(component.proxy.chartConfig) root = component.proxy.chartConfig
else if (component.proxy.groupData) root = component.proxy.groupData
// if(!root) return null
switch(configName){
case "hide":
return root.status.hide
break;
case "dataset":
return root.option.dataset
break;
case "isGroup":
return root.isGroup
break;
case "key":
return root.key
break;
case "attr":
return root.attr
break;
case "name":
return root.chartConfig.title
}
}
const setOneChartConfig = (component:any, configName:string, newValue:any, params?:any)=>{
let root = null
if(component.proxy.chartConfig) root = component.proxy.chartConfig
else if (component.proxy.groupData) root = component.proxy.groupData
switch(configName){
case "hide":
root.status.hide = newValue
break;
case "dataset":
root.option.dataset = newValue
break;
}
}
/**
* css selectors
* . #
* [name=] Todo
* #id
* .key Todo
* @param selectors
* @returns []
*/
const getComponentsBySelectors = (selectors:string):any[]=>{
// 返回:数组,可能多个
let rtn:any[] = []
const ar = selectors.split(" ")
for(let a of ar){
rtn = rtn.concat(getComponentsBySelector(a))
}
return rtn
}
const getComponentsBySelector = (selector:string):any[]=>{
// 返回:数组,可能多个
const rtn:any[] = []
if(selector.substring(0,1) == "#")
{
const key = selector.substring(1)
if(key in components){
return [components[key]]
}
return rtn
}
for (let key in components) {
if(getOneChartConfig(components[key], "name") == selector){
rtn.push(components[key])
}
}
return rtn
}
// 所有图表组件集合对象 // 所有图表组件集合对象
const components: { [K in string]?: any } = {} const components: { [K in string]?: any } = {}
const runtime = {
// 变量,管理各种状态
variables:reactive({}),
// 数据集
datasets:reactive({}),
// 组件列表 {}
components:components,
// 帮助类
fn:{
/**
*
* @param selectors string | component | [component]
* @return component null
*/
selectOneComponent:(selectors:any)=>{
const cList = runtime.fn.selectComponents(selectors)
if(cList.length > 0){
return cList[0]
}
return null
},
/**
*
* @param selectors string | component | [component]
* @return [component] []
*/
selectComponents:(selectors:any):any[]=>{
if(!selectors) return []
if(typeof selectors == "string") return getComponentsBySelectors(selectors)
if(Array.isArray(selectors)) return selectors
return [selectors]
},
/**
* 使
* @param selectors string | component | [component]
* @param configName
* @param params
* @returns
*/
getChartConfig:(selectors:any, configName:string, params?:any)=>{
const component:any = runtime.fn.selectOneComponent(selectors)
if(!component && !component.proxy) return null
return getOneChartConfig(component, configName, params)
},
/**
*
* @param selectors string | component | [component]
* @param configName
* @param newValue
* @param params
* @returns
*/
setChartConfig:(selectors:any, configName:string, newValue:any, params?:any)=>{
const cList:any[] = runtime.fn.selectComponents(selectors)
for(let c of cList){
if(!c && !c.proxy) return null
setOneChartConfig(c, configName, newValue, params)
}
},
/**
* 使 defineExpose
* @param selectors string | component | [component]
* @param action defineExpose
* @param params
* @returns
*/
callExposed:(selectors:any, action:string, params?:any)=>{
const cList:any[] = runtime.fn.selectComponents(selectors)
for(let c of cList){
if(!c && !c.exposed) return null
if(typeof c.exposed[action] == "function") c.exposed[action](params)
}
}
}
}
// 项目提供的npm 包变量 // 项目提供的npm 包变量
export const npmPkgs = { echarts } export const npmPkgs = { echarts, toRef , watch, computed, runtime }
export const useLifeHandler = (chartConfig: CreateComponentType) => { export const useLifeHandler = (chartConfig: CreateComponentType) => {
const events = chartConfig.events || {} const events = chartConfig.events || {}
// 生成生命周期事件 // 生成生命周期事件
const lifeEvents = { let lifeEvents = {
[EventLife.BEFORE_MOUNT](e: any) { [EventLife.BEFORE_MOUNT](e: any) {
// 存储组件 // 存储组件
components[chartConfig.id] = e.component components[chartConfig.id] = e.component
const fnStr = (events[EventLife.BEFORE_MOUNT] || '').trim() const fnStr = (events[EventLife.BEFORE_MOUNT] || '').trim()
generateFunc(fnStr, e) generateFunc(fnStr, e, e.component)
}, },
[EventLife.MOUNTED](e: any) { [EventLife.MOUNTED](e: any) {
const fnStr = (events[EventLife.MOUNTED] || '').trim() const fnStr = (events[EventLife.MOUNTED] || '').trim()
generateFunc(fnStr, e) generateFunc(fnStr, e, e.component)
}
}
// 遍历,按需侦听
for(let key in EventLife)
{
if(key != "BEFORE_MOUNT" && key != "MOUNTED"){
const k = EventLife[key]
const fnStr = (events[<EventLife>k] || '').trim()
if(fnStr){
lifeEvents[k] = (e:any) => {
const fnStr = (events[<EventLife>k] || '').trim()
generateFunc(fnStr, e, components[chartConfig.id])
}
}
} }
} }
return lifeEvents return lifeEvents
@ -36,7 +260,8 @@ export const useLifeHandler = (chartConfig: CreateComponentType) => {
* @param fnStr * @param fnStr
* @param e * @param e
*/ */
function generateFunc(fnStr: string, e: any) { function generateFunc(fnStr: string, e: any, component:any) {
if(fnStr == "") return
try { try {
// npmPkgs 便于拷贝 echarts 示例时设置option 的formatter等相关内容 // npmPkgs 便于拷贝 echarts 示例时设置option 的formatter等相关内容
Function(` Function(`
@ -46,7 +271,7 @@ function generateFunc(fnStr: string, e: any) {
const {${Object.keys(npmPkgs).join()}} = node_modules; const {${Object.keys(npmPkgs).join()}} = node_modules;
${fnStr} ${fnStr}
} }
)`)().bind(e?.component)(e, components, npmPkgs) )`)().bind(component)(e, components, npmPkgs)
} catch (error) { } catch (error) {
console.error(error) console.error(error)
} }

View File

@ -96,6 +96,13 @@ export enum EventLife {
MOUNTED = 'vnodeMounted', MOUNTED = 'vnodeMounted',
// 渲染之前 // 渲染之前
BEFORE_MOUNT = 'vnodeBeforeMount', BEFORE_MOUNT = 'vnodeBeforeMount',
// 鼠标事件
MOUSE_CLICK = 'click',
MOUSE_OVER = "mouseover",
MOUSE_LEAVE = "mouseleave",
// 图表事件
ECHART_LEGEND_SELECT_CHANGED = "legendselectchanged",
ECHART_HIGH_LIGHT = "highlight"
} }
// 组件实例类 // 组件实例类

View File

@ -146,7 +146,8 @@ export const openGiteeSourceCode = () => {
* @returns boolean * @returns boolean
*/ */
export const isPreview = () => { export const isPreview = () => {
return document.location.hash.includes('preview') return false
//return document.location.hash.includes('preview')
} }
/** /**

View File

@ -167,7 +167,12 @@ const { DocumentTextIcon, ChevronDownIcon, PencilIcon } = icon.ionicons5
const EventLifeName = { const EventLifeName = {
[EventLife.BEFORE_MOUNT]: '渲染之前', [EventLife.BEFORE_MOUNT]: '渲染之前',
[EventLife.MOUNTED]: '渲染之后' [EventLife.MOUNTED]: '渲染之后',
[EventLife.MOUSE_CLICK] : '点击',
[EventLife.MOUSE_OVER] : "进入",
[EventLife.MOUSE_LEAVE] : "离开",
[EventLife.ECHART_LEGEND_SELECT_CHANGED] : "选择图例",
[EventLife.ECHART_HIGH_LIGHT] : "高亮"
} }
const EventLifeTip = { const EventLifeTip = {

View File

@ -176,7 +176,7 @@ export const useSync = () => {
updateStoreInfo(res.data) updateStoreInfo(res.data)
// 更新全局数据 // 更新全局数据
if(res.data.content && res.data.content != "{}"){ if(res.data.content && res.data.content != "{}"){
await updateComponent(JSON.parse(res.data.content)) await updateComponent(JSON.parse(res.data.content), true)
return return
} }
} }

View File

@ -1,7 +1,7 @@
<template> <template>
<div <div
class="chart-item" class="chart-item"
v-for="(item, index) in localStorageInfo.componentList" v-for="(item, index) in reactiveList"
:class="animationsClass(item.styles.animations)" :class="animationsClass(item.styles.animations)"
:key="item.id" :key="item.id"
:style="{ :style="{
@ -19,6 +19,7 @@
:groupIndex="index" :groupIndex="index"
:themeSetting="themeSetting" :themeSetting="themeSetting"
:themeColor="themeColor" :themeColor="themeColor"
v-on="useLifeHandler(item)"
></preview-render-group> ></preview-render-group>
<!-- 单组件 --> <!-- 单组件 -->
@ -35,7 +36,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { PropType, computed } from 'vue' import { PropType, computed, reactive } from 'vue'
import { ChartEditStorageType } from '../../index.d' import { ChartEditStorageType } from '../../index.d'
import { PreviewRenderGroup } from '../PreviewRenderGroup/index' import { PreviewRenderGroup } from '../PreviewRenderGroup/index'
import { CreateComponentGroupType } from '@/packages/index.d' import { CreateComponentGroupType } from '@/packages/index.d'
@ -50,6 +51,7 @@ const props = defineProps({
} }
}) })
const reactiveList = reactive(props.localStorageInfo.componentList)
// //
const themeSetting = computed(() => { const themeSetting = computed(() => {
const chartThemeSetting = props.localStorageInfo.editCanvasConfig.chartThemeSetting const chartThemeSetting = props.localStorageInfo.editCanvasConfig.chartThemeSetting