Skip to content

Commit

Permalink
feat: #683 增加主题缓存的命名空间机制
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsonliu committed Jan 21, 2024
1 parent 03f7a99 commit f4bf980
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/Cherry.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,8 @@ const defaultConfig = {
{ className: 'violet', label: '淡雅' },
{ className: 'blue', label: '清幽' },
],
// 定义主题的作用范围,相同themeNameSpace的实例共享主题配置
themeNameSpace: 'cherry',
// 预览页面不需要绑定事件
isPreviewOnly: false,
// 预览区域跟随编辑器光标自动滚动
Expand Down
2 changes: 1 addition & 1 deletion src/Cherry.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ export default class Cherry extends CherryStatic {
'cherry-previewer cherry-markdown',
className || '',
autonumberClass,
getThemeFromLocal(true),
getThemeFromLocal(true, this.options.themeNameSpace),
].join(' ');
if (dom) {
previewer = dom;
Expand Down
13 changes: 7 additions & 6 deletions src/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,20 @@ export function getIsClassicBrFromLocal() {
* 保存当前主题
* @param {string} theme
*/
function saveThemeToLocal(theme) {
function saveThemeToLocal(nameSpace, theme) {
if (typeof localStorage !== 'undefined') {
localStorage.setItem('cherry-theme', theme);
localStorage.setItem(`${nameSpace}-theme`, theme);
}
}

/**
* 获取当前主题
* @returns {string} 主题名
*/
export function getThemeFromLocal(fullClass = false) {
export function getThemeFromLocal(fullClass = false, nameSpace = 'cherry') {
let ret = 'default';
if (typeof localStorage !== 'undefined') {
const localTheme = localStorage.getItem('cherry-theme');
const localTheme = localStorage.getItem(`${nameSpace}-theme`);
if (localTheme) {
ret = localTheme;
}
Expand All @@ -89,10 +89,11 @@ export function getThemeFromLocal(fullClass = false) {
* @param {string} theme 如果没有传theme,则从本地缓存里取
*/
export function changeTheme($cherry, theme = '') {
const newTheme = (theme ? theme : getThemeFromLocal()).replace(/^.*theme__/, '');
const themeNameSpace = $cherry.options.themeNameSpace || 'cherry';
const newTheme = (theme ? theme : getThemeFromLocal(false, themeNameSpace)).replace(/^.*theme__/, '');
const newClass = ` theme__${newTheme}`;
$cherry.wrapperDom.className = $cherry.wrapperDom.className.replace(/ theme__[^ $]+?( |$)/g, ' ') + newClass;
$cherry.previewer.getDomContainer().className =
$cherry.previewer.getDomContainer().className.replace(/ theme__[^ $]+?( |$)/g, ' ') + newClass;
saveThemeToLocal(newTheme);
saveThemeToLocal(themeNameSpace, newTheme);
}
2 changes: 2 additions & 0 deletions types/cherry.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export interface CherryOptions {
};
/** 有哪些主题 */
theme: {className: string, label: string}[];
/** 定义主题的作用范围,相同themeNameSpace的实例共享主题配置 */
themeNameSpace: string,
callback: {
/** 编辑器内容改变并完成渲染后触发 */
afterChange: CherryLifecycle;
Expand Down

0 comments on commit f4bf980

Please sign in to comment.