Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 分离了核心库的消息输出 #71

Merged
merged 3 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/data/Quick/Quick.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import type { SelectOption } from 'naive-ui'
import { NButton, NInput, NInputGroup, NSelect } from 'naive-ui'
import StaticContent from 'open-data-v/apiView/static/DataView.vue'
import { StaticKey, useEventBus } from 'open-data-v/bus'
import { message } from 'open-data-v/utils/message'
import { onMounted, reactive, ref, watch } from 'vue'

import type { StaticDataDetail } from '@/api/data'
Expand All @@ -42,6 +41,7 @@ import {
getStaticDataListApi,
updateStaticDataApi
} from '@/api/data'
import { message } from '@/utils/message'

import type { StoreStaticOption } from './type'

Expand Down Expand Up @@ -149,7 +149,7 @@ const handleSave = async () => {
formData.title = data.name
formData.originData = data.data
staticDataOptions.data = JSON.stringify(data.data, null, '\t')
Logger.info('数据保存成功')
message.info('数据保存成功')
await loadStaticList()
} else {
message.warning('数据保存失败')
Expand All @@ -166,7 +166,7 @@ const handleUpdate = async () => {
name: formData.title || '未命名'
})
if (resp.status === 200) {
Logger.info('数据更新成功')
message.info('数据更新成功')
await loadStaticList()
} else {
message.warning('数据更新失败')
Expand Down
15 changes: 8 additions & 7 deletions examples/data/Rest/DynamicExtendView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
recordabletoKV,
requestOptionsToStore
} from 'open-data-v/apiView/rest/utils'
import { Logger, uuid } from 'open-data-v/apiView/utils'
import { uuid } from 'open-data-v/apiView/utils'
import { StaticKey, useEventBus } from 'open-data-v/bus'
import { onMounted, reactive, ref } from 'vue'

Expand All @@ -48,6 +48,7 @@ import {
updateRestDataApi
} from '@/api/data'
import type { RestDataDetail } from '@/api/data/type'
import { message } from '@/utils/message'

const getEmptyParams = () => {
return [{ key: '', value: '', disable: false, id: uuid() }]
Expand Down Expand Up @@ -218,12 +219,12 @@ const handleSave = async () => {
const data: RestDataDetail = resp.data
formData.id = data.id!
formData.title = data.name
Logger.info('数据保存成功')
message.info('数据保存成功')
} else {
Logger.warn('数据保存失败')
message.warning('数据保存失败')
}
} catch (err) {
Logger.warn('数据保存失败')
message.warning('数据保存失败')
}
}
const handleUpdate = async () => {
Expand All @@ -240,12 +241,12 @@ const handleUpdate = async () => {
params: params
})
if (resp.status === 200) {
Logger.info('数据更新成功')
message.info('数据更新成功')
} else {
Logger.warn('数据更新失败')
message.warning('数据更新失败')
}
} catch (err) {
Logger.warn('数据更新失败')
message.warning('数据更新失败')
}
}

Expand Down
25 changes: 24 additions & 1 deletion examples/pages/DesigerView/View.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

<script setup lang="ts">
/* eslint-disable-next-line @typescript-eslint/consistent-type-imports */
import { Designer } from 'open-data-v/designer'
import { useEventBus } from 'open-data-v/bus'
import type { Designer } from 'open-data-v/designer'
import useCanvasState from 'open-data-v/designer/state/canvas'
import useDataState from 'open-data-v/designer/state/data'
import { onMounted, ref, watch } from 'vue'
Expand All @@ -15,6 +16,28 @@ import QuickDataPlugin from '@/data/Quick'
import RestDataPlugin from '@/data/Rest'
import useToolBars from '@/pages/DesigerView/toolbars'
import { useProjectSettingStoreWithOut } from '@/store/modules/projectSetting'
import { message } from '@/utils/message'

useEventBus('stdout', (event) => {
const stdout = event as { type: string; from: string; message: any }
if (stdout.from === 'handle') {
let callback = message.info
if (stdout.type === 'error') {
callback = message.error
} else if (stdout.type === 'warn') {
callback = message.warning
}
callback(stdout.message)
} else {
let callback = console.info
if (stdout.type === 'error') {
callback = console.error
} else if (stdout.type === 'warn') {
callback = console.warn
}
callback(stdout.message)
}
})

const canvasState = useCanvasState()
const dataState = useDataState()
Expand Down
4 changes: 2 additions & 2 deletions examples/scriptView/Conetent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const handleSave = async () => {
formData.id = data.id
formData.title = data.name
formData.code = data.code
Logger.info('数据保存成功')
message.info('数据保存成功')
} else {
message.warning('数据保存失败')
}
Expand All @@ -135,7 +135,7 @@ const handleUpdate = async () => {
name: formData.title || '未命名'
})
if (resp.status === 200) {
Logger.info('数据更新成功')
message.info('数据更新成功')
} else {
message.warning('数据更新失败')
}
Expand Down
2 changes: 0 additions & 2 deletions src/apiView/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export const Logger = console

export const uuid = (hasHyphen?: string) => {
return (
hasHyphen ? 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' : 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'
Expand Down
6 changes: 3 additions & 3 deletions src/apiView/websocket/WebsocketView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
</template>
<script setup lang="ts">
import { NButton, NButtonGroup, NCard, NInput, NSpace, NTabPane, NTabs } from 'naive-ui'
import { eventBus } from 'open-data-v/bus'
import { onUnmounted, reactive, ref } from 'vue'

import { Logger } from '../utils'
import type { WebsocketOption } from './type'

const props = withDefaults(
Expand Down Expand Up @@ -77,13 +77,13 @@ const connect = () => {
close()
wsInstance = new WebSocket(formData.url)
wsInstance.onopen = () => {
Logger.info('wsOpen')
eventBus.emit('stdout', { type: 'info', message: 'wsOpen', form: 'data' })
}
wsInstance.onmessage = (message) => {
response.value.data = message.data
}
wsInstance.onerror = (err) => {
Logger.error(err)
eventBus.emit('stdout', { type: 'error', message: err, from: 'data' })
close()
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/apiView/websocket/handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cloneDeep } from 'lodash-es'
import type { DataAcceptor, DataInstance, Response } from 'open-data-v/apiView/type'
import { Logger } from 'open-data-v/apiView/utils'
import { eventBus } from 'open-data-v/bus'

import type { WebsocketOption } from './type'

Expand Down Expand Up @@ -35,7 +35,7 @@ class WebsocketData implements DataInstance {
private async wsconnect() {
this.wsInstance = new WebSocket(this.options.url)
this.wsInstance.addEventListener('open', () => {
Logger.info('wsOpen')
eventBus.emit('stdout', { type: 'info', message: 'wsOpen', from: 'data' })
})
const handlerData = (message) => {
const response: Response = {
Expand All @@ -56,7 +56,7 @@ class WebsocketData implements DataInstance {
}
this.wsInstance.addEventListener('message', handlerData)
this.wsInstance.addEventListener('error', (err) => {
Logger.error(err)
eventBus.emit('stdout', { type: 'info', message: err, from: 'data' })
if (!this.options.isRetry) {
return
}
Expand Down
6 changes: 3 additions & 3 deletions src/designer/Editor/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
</template>

<script setup lang="ts">
import { eventBus } from 'open-data-v/bus'
import { EditMode } from 'open-data-v/designer/const'
import Area from 'open-data-v/designer/Editor/Area.vue'
import Grid from 'open-data-v/designer/Editor/Grid.vue'
Expand All @@ -71,7 +72,6 @@ import {
createComponent,
filterStyle,
getComponentShapeStyle,
Logger,
uuid
} from '../utils'

Expand Down Expand Up @@ -110,14 +110,14 @@ const contextmenus = (): ContextmenuItem[] => {
}

onMounted(() => {
Logger.log('进入编辑模式')
eventBus.emit('stdout', { type: 'info', message: '进入编辑模式', from: 'system' })
canvasState.setEditMode(EditMode.EDIT)
document.addEventListener('paste', pasteComponent)
document.addEventListener('copy', copyComponent)
})

onUnmounted(() => {
Logger.log('进入预览模式')
eventBus.emit('stdout', { type: 'info', message: '进入预览模式', from: 'system' })
canvasState.setEditMode(EditMode.PREVIEW)
document.removeEventListener('paste', pasteComponent)
document.removeEventListener('copy', copyComponent)
Expand Down
4 changes: 2 additions & 2 deletions src/designer/Editor/Shape/Shape.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { ComponentPublicInstance, PropType } from 'vue'
import { computed, defineComponent, onErrorCaptured, onMounted, ref, watch } from 'vue'

import { stretchedComponents } from '../../component'
import { copyText, Logger, mod360, throttleFrame } from '../../utils'
import { copyText, mod360, throttleFrame } from '../../utils'
import styles from './shape.module.less'

export default defineComponent({
Expand Down Expand Up @@ -135,7 +135,7 @@ export default defineComponent({
const errorInfo = ref<string>('')

onErrorCaptured((err: Error, instance: ComponentPublicInstance | null, info: string) => {
Logger.log(err)
eventBus.emit('stdout', { type: 'error', message: err, from: 'system' })
if (info === 'render function') {
if (canvasState.isEditMode) {
if (instance) {
Expand Down
10 changes: 6 additions & 4 deletions src/designer/load.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { NSpin } from 'naive-ui'
import { eventBus } from 'open-data-v/bus'
import type { CustomComponent } from 'open-data-v/models'
import type { App } from 'vue'
import { defineAsyncComponent } from 'vue'

import Group from './components/Group'

const Logger = console

// 编辑器左侧组件列表
const componentList: Record<string, any> = {}

Expand Down Expand Up @@ -45,7 +43,11 @@ const useLoadComponent = () => {
})
app.component(componentOptions.componentName, asyncComp)
} else {
Logger.error(`${key} is not a valid component`)
eventBus.emit('stdout', {
type: 'error',
message: `${key} is not a valid component`,
from: 'sysem'
})
}
})
}
Expand Down
4 changes: 0 additions & 4 deletions src/designer/modules/arrayItem/ArrayItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
<script lang="ts" setup>
import { isNumber } from 'lodash-es'
import { NInput, NSpace } from 'naive-ui'
import { message } from 'open-data-v/utils/message'
import { nextTick, reactive, ref } from 'vue'

const props = withDefaults(
Expand Down Expand Up @@ -62,13 +61,11 @@ arrayValue.splice(0, props.value.length, ...props.value)

const handleAdd = () => {
if (!newValue.value.trim()) {
message.warning('请输入数据')
return
}

const { maxItem } = props
if (isNumber(maxItem) && arrayValue.length >= maxItem) {
message.warning(`最多 ${maxItem} 个输入框,添加失败`)
return
}

Expand All @@ -83,7 +80,6 @@ const handleAdd = () => {
const handleDelete = (index: number) => {
// minItem 有默认值,默认值为数值,不用再判断
if (arrayValue.length <= props.minItem) {
message.warning(`最少 ${props.minItem} 个输入框,删除失败`)
return
}
arrayValue.splice(index, 1)
Expand Down
9 changes: 4 additions & 5 deletions src/designer/state/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import PixelEnum from 'open-data-v/enum/pixel'
import type { CustomComponent } from 'open-data-v/models'
import type { DataInstance } from 'open-data-v/models/type'
import { buildModeValue, updateModeValue } from 'open-data-v/models/utils'
import { message } from 'open-data-v/utils/message'
import { reactive } from 'vue'

import {
Expand Down Expand Up @@ -583,7 +582,7 @@ class CanvasState {
swap(componentData, index, index - 1)
this.saveComponentData()
} else {
message.info('图层已经到底了')
eventBus.emit('stdout', { type: 'info', message: '图层已经到底了', from: 'handle' })
}
}
/**
Expand All @@ -602,7 +601,7 @@ class CanvasState {
swap(componentData, index, index + 1)
this.saveComponentData()
} else {
message.info('图层已经到顶了')
eventBus.emit('stdout', { type: 'info', message: '图层已经到顶了', from: 'handle' })
}
}

Expand All @@ -622,7 +621,7 @@ class CanvasState {
componentData.push(myComponments[0])
this.saveComponentData()
} else {
message.info('图层已经到顶了')
eventBus.emit('stdout', { type: 'info', message: '图层已经到顶了', from: 'handle' })
}
}
/**
Expand All @@ -641,7 +640,7 @@ class CanvasState {
componentData.unshift(myComponments[0])
this.saveComponentData()
} else {
message.info('图层已经到底了')
eventBus.emit('stdout', { type: 'info', message: '图层已经到底了', from: 'handle' })
}
}
/**
Expand Down
6 changes: 3 additions & 3 deletions src/designer/toolbars.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { eventBus } from 'open-data-v/bus'
import useCanvasState from 'open-data-v/designer/state/canvas'
import useSnapShotState from 'open-data-v/designer/state/snapshot'
import type { ComponentDataType } from 'open-data-v/designer/type'
import { message } from 'open-data-v/utils/message'

import type { StoreComponentData } from './db'
import type { CanvasStyleData } from './state/type'
Expand All @@ -19,7 +19,7 @@ const undo = async () => {
dataSlotters: snapshot.dataSlotters
})
} else {
message.warning('没有快照了')
eventBus.emit('stdout', { type: 'warn', message: '没有快照了', form: 'handle' })
}
}

Expand All @@ -32,7 +32,7 @@ const recoveryDraft = async () => {
dataSlotters: snapshot.dataSlotters
})
} else {
message.warning('没有快照了')
eventBus.emit('stdout', { type: 'warn', message: '没有快照了', from: 'handle' })
}
}
const setShowEm = () => {
Expand Down
1 change: 0 additions & 1 deletion src/designer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,4 +590,3 @@ export const diffIndex = (fromIndex: string, toIndex: string): boolean => {
return false
}
}
export const Logger = console
Loading