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

feat:add theme switching plugin #1070

Open
wants to merge 6 commits into
base: refactor/develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion designer-demo/engine.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default {
id: 'engine.config',
theme: import.meta.env.VITE_THEME || 'light',
theme: 'light',
material: ['/mock/bundle.json'],
scripts: [],
styles: []
Expand Down
7 changes: 5 additions & 2 deletions designer-demo/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
Redoundo,
Save,
Clean,
Theme,
Preview,
GenerateCode,
Refresh,
Expand Down Expand Up @@ -64,15 +65,16 @@ export default {
left: ['engine.toolbars.breadcrumb', 'engine.toolbars.lock', 'engine.toolbars.logo'],
center: ['engine.toolbars.media'],
right: [
['engine.toolbars.redoundo', 'engine.toolbars.clean'],
['engine.toolbars.theme', 'engine.toolbars.redoundo', 'engine.toolbars.clean'],
['engine.toolbars.preview'],
['engine.toolbars.generate-code', 'engine.toolbars.save']
],
collapse: [
['engine.toolbars.collaboration'],
['engine.toolbars.refresh', 'engine.toolbars.fullscreen'],
['engine.toolbars.lang'],
['engine.toolbars.viewSetting']
['engine.toolbars.viewSetting'],
['engine.toolbars.theme']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

插件名看看换成:engine.toolbars.themeSwitch 是不是好一点?engine.toolbars.theme 可能会容易误解成工具栏的主题。

]
}
}
Expand All @@ -86,6 +88,7 @@ export default {
}
],
toolbars: [
Theme,
Logo,
Breadcrumb,
Lock,
Expand Down
2 changes: 2 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@opentiny/tiny-engine-toolbar-preview": ["packages/toolbars/preview/index"],
"@opentiny/tiny-engine-toolbar-generate-code": ["packages/toolbars/generate-code/index"],
"@opentiny/tiny-engine-toolbar-clean": ["packages/toolbars/clean/index"],
"@opentiny/tiny-engine-toolbar-theme": ["packages/toolbars/theme/index"],
"@opentiny/tiny-engine-toolbar-save": ["packages/toolbars/save/index"],
"tiny-engine-canvas": ["packages/canvas/index"],
"@opentiny/tiny-engine-theme-dark": ["packages/theme/dark/index.less"],
Expand Down Expand Up @@ -59,6 +60,7 @@
"@opentiny/tiny-engine-toolbar-media/*": ["packages/toolbars/media/*"],
"@opentiny/tiny-engine-toolbar-preview/*": ["packages/toolbars/preview/*"],
"@opentiny/tiny-engine-toolbar-clean/*": ["packages/toolbars/clean/*"],
"@opentiny/tiny-engine-toolbar-theme/*": ["packages/toolbars/theme/*"],
"@opentiny/tiny-engine-toolbar-save/*": ["packages/toolbars/save/*"],
"@opentiny/tiny-engine-theme-dark/*": ["packages/theme/dark/*"],
"@opentiny/tiny-engine-theme-light/*": ["packages/theme/light/*"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const getDevAlias = (useSourceAlias) => {
'@opentiny/tiny-engine-toolbar-refresh': path.resolve(basePath, 'packages/toolbars/refresh/index.js'),
'@opentiny/tiny-engine-toolbar-redoundo': path.resolve(basePath, 'packages/toolbars/redoundo/index.js'),
'@opentiny/tiny-engine-toolbar-clean': path.resolve(basePath, 'packages/toolbars/clean/index.js'),
'@opentiny/tiny-engine-toolbar-theme': path.resolve(basePath, 'packages/toolbars/theme/index.js'),
'@opentiny/tiny-engine-toolbar-save': path.resolve(basePath, 'packages/toolbars/save/index.js'),
'@opentiny/tiny-engine-toolbar-setting': path.resolve(basePath, 'packages/toolbars/setting/index.js'),
'@opentiny/tiny-engine-toolbar-collaboration': path.resolve(basePath, 'packages/toolbars/collaboration/index.js'),
Expand Down
31 changes: 22 additions & 9 deletions packages/common/component/ToolbarBase.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
</template>
</component>
<slot></slot>
<span v-if="state.options?.collapsed">{{ state.content }}</span>
<span v-if="isHideMulti()">{{ state.content }}</span>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<span v-if="state.options?.collapsed && state.content">{{ state.content }}</span>

</span>
<span> </span>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的空 span 有什么用?

<slot name="radio"></slot>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要增加 radio 的插槽。使用默认的插槽

</template>

<script>
Expand All @@ -32,6 +34,10 @@ export default {
options: {
type: Object,
default: () => {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

默认值应该是: () => ({})

},
position: {
type: String,
default: 'right'
}
},
emits: ['click-api'],
Expand All @@ -46,21 +52,28 @@ export default {
emit('click-api')
}

const isShowMulti = () => (state.options?.collapsed || props.position === 'collapse') && state.options?.multiType
chilingling marked this conversation as resolved.
Show resolved Hide resolved
const isHideMulti = () => (state.options?.collapsed || props.position === 'collapse') && !state.options?.multiType

const getRender = () => {
switch (props.options.renderType) {
case 'button':
return ToolbarBaseButton
case 'icon':
return ToolbarBaseIcon
default:
return false
if (isShowMulti()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里 renderType 不传参是不是就可以不渲染了?

return false
}
if (props.options.renderType === 'button') {
return ToolbarBaseButton
}
if (props.options.renderType === 'icon') {
return ToolbarBaseIcon
}
return false
}

return {
state,
click,
getRender
getRender,
isShowMulti,
isHideMulti
}
}
}
Expand Down
31 changes: 11 additions & 20 deletions packages/design-core/assets/cn.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions packages/design-core/assets/dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 10 additions & 27 deletions packages/design-core/assets/en.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions packages/design-core/assets/light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions packages/design-core/assets/nested-page.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions packages/design-core/assets/single-page.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/design-core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export { default as Media } from '@opentiny/tiny-engine-toolbar-media'
export { default as Redoundo, HistoryService } from '@opentiny/tiny-engine-toolbar-redoundo'
export { default as Save } from '@opentiny/tiny-engine-toolbar-save'
export { default as Clean } from '@opentiny/tiny-engine-toolbar-clean'
export { default as Theme } from '@opentiny/tiny-engine-toolbar-theme'
export { default as Preview } from '@opentiny/tiny-engine-toolbar-preview'
export { default as GenerateCode, SaveLocalService } from '@opentiny/tiny-engine-toolbar-generate-code'
export { default as Refresh } from '@opentiny/tiny-engine-toolbar-refresh'
Expand Down
1 change: 1 addition & 0 deletions packages/design-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"@opentiny/tiny-engine-theme-light": "workspace:*",
"@opentiny/tiny-engine-toolbar-breadcrumb": "workspace:*",
"@opentiny/tiny-engine-toolbar-clean": "workspace:*",
"@opentiny/tiny-engine-toolbar-theme": "workspace:*",
"@opentiny/tiny-engine-toolbar-collaboration": "workspace:*",
"@opentiny/tiny-engine-toolbar-fullscreen": "workspace:*",
"@opentiny/tiny-engine-toolbar-generate-code": "workspace:*",
Expand Down
11 changes: 8 additions & 3 deletions packages/design-core/src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
defineEntry,
mergeRegistry,
getMergeMeta,
getMetaApi,
META_SERVICE,
initServices,
initHook,
HOOK_NAME,
Expand All @@ -37,6 +39,7 @@ const defaultLifeCycles = {
beforeAppCreate: ({ registry }) => {
// 合并用户自定义注册表
const newRegistry = mergeRegistry(registry, defaultRegistry)
const appId = getMetaApi(META_SERVICE.GlobalService).getBaseInfo().id
if (process.env.NODE_ENV === 'development') {
console.log('default registry:', defaultRegistry) // eslint-disable-line
console.log('merged registry:', registry) // eslint-disable-line
Expand All @@ -55,7 +58,7 @@ const defaultLifeCycles = {
// 加载主题样式,尽早加载
// import(`./theme/${newRegistry.config.theme}.js`)

const theme = newRegistry.config.theme || 'light'
const theme = localStorage.getItem(`tiny-engine-theme-${appId}`) || newRegistry.config.theme || 'light'
// eslint-disable-next-line no-new
new TinyThemeTool(defaultThemeList[theme], defaultThemeList[theme]?.id)
document.documentElement?.setAttribute?.('data-theme', theme)
Expand All @@ -72,8 +75,10 @@ const defaultLifeCycles = {
window.lowcodeI18n = i18n
app.use(i18n).use(injectGlobalComponents)

const theme = getMergeMeta('engine.config').theme?.includes('dark') ? 'vs-dark' : 'vs'
setGlobalMonacoEditorTheme(theme)
const appId = getMetaApi(META_SERVICE.GlobalService).getBaseInfo().id
const theme = localStorage.getItem(`tiny-engine-theme-${appId}`) || getMergeMeta('engine.config').theme
const editorTheme = theme?.includes('dark') ? 'vs-dark' : 'vs'
setGlobalMonacoEditorTheme(editorTheme)
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/engine-cli/src/commands/generateConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const generateConfig = (options = {}) => {
const configContent = `
export default {
id: 'engine.config',
theme: import.meta.env.VITE_THEME || '${theme}',
theme: '${theme}',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import.meta.env.VITE_THEME 的环境变量删除了,构建相关的逻辑判断也要一并删除。
关联逻辑:

'@opentiny/tiny-engine-theme': getThemePath(env.VITE_THEME, useSourceAlias)

'@opentiny/tiny-engine-theme': getThemePath(env.VITE_THEME, false)

const getThemePath = (theme, useSourceAlias) => {
if (!['light', 'dark'].includes(theme)) {
return ''
}
if (useSourceAlias) {
const basePath = useSourceAlias.basePath || path.resolve(process.cwd(), '..')
return path.resolve(basePath, `packages/theme/${theme}/index.less`)
}
return path.resolve(process.cwd(), `./node_modules/@opentiny/tiny-engine-theme-${theme}/dist/style.css`)
}

material: ${JSON.stringify(material)},
scripts: ${JSON.stringify(scripts)},
styles: ${JSON.stringify(styles)},
Expand Down
2 changes: 1 addition & 1 deletion packages/engine-cli/template/designer/engine.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default {
id: 'engine.config',
theme: import.meta.env.VITE_THEME || 'light',
theme: 'light',
material: ['/mock/bundle.json'],
scripts: [],
styles: []
Expand Down
7 changes: 5 additions & 2 deletions packages/engine-cli/template/designer/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
Redoundo,
Save,
Clean,
Theme,
Preview,
GenerateCode,
Refresh,
Expand Down Expand Up @@ -64,15 +65,16 @@ export default {
left: ['engine.toolbars.breadcrumb', 'engine.toolbars.lock', 'engine.toolbars.logo'],
center: ['engine.toolbars.media'],
right: [
['engine.toolbars.redoundo', 'engine.toolbars.clean'],
['engine.toolbars.theme', 'engine.toolbars.redoundo', 'engine.toolbars.clean'],
['engine.toolbars.preview'],
['engine.toolbars.generate-code', 'engine.toolbars.save']
],
collapse: [
['engine.toolbars.collaboration'],
['engine.toolbars.refresh', 'engine.toolbars.fullscreen'],
['engine.toolbars.lang'],
['engine.toolbars.viewSetting']
['engine.toolbars.viewSetting'],
['engine.toolbars.theme']
]
}
}
Expand All @@ -86,6 +88,7 @@ export default {
}
],
toolbars: [
Theme,
Logo,
Breadcrumb,
Lock,
Expand Down
5 changes: 3 additions & 2 deletions packages/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ export default {
left: ['engine.toolbars.breadcrumb', 'engine.toolbars.lock', 'engine.toolbars.logo'],
center: ['engine.toolbars.media'],
right: [
['engine.toolbars.redoundo', 'engine.toolbars.clean'],
['engine.toolbars.theme', 'engine.toolbars.redoundo', 'engine.toolbars.clean'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议工具栏和收缩下拉菜单放一个主题切换的工具就好了

['engine.toolbars.preview'],
['engine.toolbars.generate-code', 'engine.toolbars.save']
],
collapse: [
['engine.toolbars.collaboration'],
['engine.toolbars.refresh', 'engine.toolbars.fullscreen'],
['engine.toolbars.lang'],
['engine.toolbars.viewSetting']
['engine.toolbars.viewSetting'],
['engine.toolbars.theme']
]
}
},
Expand Down
Loading
Loading