Skip to content

Commit

Permalink
feat: #769 代码块主题缓存 (#781)
Browse files Browse the repository at this point in the history
* chore(release): 0.8.42

* feat: #769 代码块主题缓存

* fix: 修改代码主题支持多实例场景

---------

Co-authored-by: sunsonliu(刘阳) <[email protected]>
Co-authored-by: saraph1nes <[email protected]>
Co-authored-by: yuxuanxia <[email protected]>
  • Loading branch information
4 people committed Jun 24, 2024
1 parent 5bb2b26 commit 912c25e
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 12 deletions.
4 changes: 2 additions & 2 deletions dist/cherry-markdown.js
Git LFS file not shown
2 changes: 1 addition & 1 deletion dist/fonts/ch-icon.eot
Git LFS file not shown
2 changes: 1 addition & 1 deletion dist/fonts/ch-icon.ttf
Git LFS file not shown
2 changes: 1 addition & 1 deletion dist/fonts/ch-icon.woff
Git LFS file not shown
4 changes: 2 additions & 2 deletions dist/fonts/ch-icon.woff2
Git LFS file not shown
5 changes: 4 additions & 1 deletion src/Cherry.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import ToolbarRight from './toolbars/ToolbarRight';
import Toc from './toolbars/Toc';
import { createElement } from './utils/dom';
import Sidebar from './toolbars/Sidebar';
import { customizer, getThemeFromLocal, changeTheme } from './utils/config';
import { customizer, getThemeFromLocal, changeTheme, getCodeThemeFromLocal } from './utils/config';
import NestedError, { $expectTarget } from './utils/error';
import getPosBydiffs from './utils/recount-pos';
import defaultConfig from './Cherry.config';
Expand Down Expand Up @@ -492,6 +492,9 @@ export default class Cherry extends CherryStatic {
'data-codeBlockTheme': codeBlockTheme,
},
);

wrapperDom.setAttribute('data-code-block-theme', getCodeThemeFromLocal(this.options.themeNameSpace));

this.wrapperDom = wrapperDom;
return wrapperDom;
}
Expand Down
8 changes: 4 additions & 4 deletions src/toolbars/hooks/CodeTheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/
import MenuBase from '@/toolbars/MenuBase';
import { changeCodeTheme } from '@/utils/config';
/**
* 设置代码块的主题
* 本功能依赖[prism组件](https://github.com/PrismJS/prism)
Expand All @@ -39,10 +40,9 @@ export default class CodeTheme extends MenuBase {
/**
* 响应点击事件
* @param {string} shortKey 快捷键参数,本函数不处理这个参数
* @param {string} theme 具体的代码块主题
* @returns 回填到编辑器光标位置/选中文本区域的内容
* @param {string} codeTheme 具体的代码块主题
*/
onClick(shortKey = '', theme) {
document.querySelector('.cherry').setAttribute('data-code-block-theme', theme);
onClick(shortKey = '', codeTheme) {
changeCodeTheme(this.$cherry, codeTheme);
}
}
43 changes: 43 additions & 0 deletions src/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,46 @@ export function changeTheme($cherry, theme = '') {
$cherry.previewer.getDomContainer().className.replace(/ theme__[^ $]+?( |$)/g, ' ') + newClass;
saveThemeToLocal(themeNameSpace, newTheme);
}

/**
* 保存当前代码主题
* @param {string} nameSpace
* @param {string} codeTheme
*/
function saveCodeThemeToLocal(nameSpace, codeTheme) {
if (typeof localStorage !== 'undefined') {
localStorage.setItem(`${nameSpace}-codeTheme`, codeTheme);
}
}

/**
* 获取当前代码主题
* @param {string} nameSpace
* @returns {string} 主题名
*/
export function getCodeThemeFromLocal(nameSpace = 'cherry') {
let res = 'default';
if (typeof localStorage !== 'undefined') {
const localTheme = localStorage.getItem(`${nameSpace}-codeTheme`);
if (localTheme) {
res = localTheme;
}
}
return res;
}

/**
* 修改代码主题
* 相同themeNameSpace的实例有一样的代码主题配置
* @param {object} $cherry
* @param {string} codeTheme 如果没有传codeTheme,则从本地缓存里取
*/
export function changeCodeTheme($cherry, codeTheme) {
const themeNameSpace = $cherry.options.themeNameSpace || 'cherry';

const newTheme = codeTheme ? codeTheme : getCodeThemeFromLocal(themeNameSpace);

$cherry.wrapperDom.setAttribute('data-code-block-theme', newTheme);

saveCodeThemeToLocal(themeNameSpace, newTheme);
}

0 comments on commit 912c25e

Please sign in to comment.