Skip to content

Commit

Permalink
#616 增加切换语言的回调事件
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsonliu committed Aug 22, 2024
1 parent 1d91434 commit 8f9da9b
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Cherry.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ const defaultConfig = {
focus: ({ e, cherry }) => {},
blur: ({ e, cherry }) => {},
selectionChange: ({ selections, lastSelections, info }) => {},
afterChangeLocale: (locale) => {},
},
previewer: {
dom: false,
Expand Down
2 changes: 1 addition & 1 deletion src/Cherry.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export default class Cherry extends CherryStatic {

on(eventName, callback) {
if (this.$event.Events[eventName]) {
if (/afterInit|afterChange/.test(eventName)) {
if (/^(afterInit|afterChange)$/.test(eventName)) {
// 做特殊处理
return this.$event.on(eventName, (msg) => {
callback(msg.markdownText, msg.html);
Expand Down
6 changes: 6 additions & 0 deletions src/Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export default class Event {
focus: 'focus', // 焦点
blur: 'blur', // 失焦
selectionChange: 'selectionChange', // 选区内容改变时触发
afterChangeLocale: 'afterChangeLocale', // 语言改变
};

/**
Expand Down Expand Up @@ -96,6 +97,11 @@ export default class Event {
options.event.selectionChange(event);
});
}
if (options.event.afterChangeLocale) {
this.on(this.Events.afterChangeLocale, (locale) => {
options.event.afterChangeLocale(locale);
});
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/toolbars/hooks/ChangeLocale.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default class ChangeLocale extends MenuBase {
if (!this.$cherry.locales[shortKey]) {
return;
}
this.$cherry.$event.emit('afterChangeLocale', shortKey);
this.$cherry.options.locale = shortKey;
this.$cherry.locale = this.$cherry.locales[shortKey];
this.$cherry.resetToolbar('toolbar', this.$cherry.options.toolbars.toolbar || []);
Expand Down
3 changes: 3 additions & 0 deletions types/cherry.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ export interface _CherryOptions<T extends CherryCustomOptions = CherryCustomOpti
afterChange?: CherryLifecycle;
/** 编辑器完成初次渲染后触发 */
afterInit?: CherryLifecycle;
/** 编辑器选区变化时触发 */
selectionChange?: ({ selections: [], lastSelections: [], info }) => void;
/** 变更语言时触发 */
afterChangeLocale?: ( locale: string ) => void;
};
/** 预览区域配置 */
previewer: CherryPreviewerOptions;
Expand Down

0 comments on commit 8f9da9b

Please sign in to comment.