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

fix(type): type error in config #839

Merged
merged 10 commits into from
Aug 14, 2024
Merged
6 changes: 3 additions & 3 deletions src/Cherry.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ export default class Cherry extends CherryStatic {
* @readonly
*/
static config = {
/** @type {Partial<CherryOptions>} */
/** @type {CherryOptions} */
defaults: defaultConfig,
};

/**
* @param {Partial<CherryOptions>} options
* @param {CherryOptions} options
*/
constructor(options) {
super();
Expand All @@ -63,7 +63,7 @@ export default class Cherry extends CherryStatic {
$expectTarget(options, Object);
/**
* @property
* @type {Partial<CherryOptions>}
* @type {CherryOptions}
*/
this.options = mergeWith({}, defaultConfigCopy, options, customizer);

Expand Down
8 changes: 4 additions & 4 deletions src/core/HookCenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default class HookCenter {
/**
*
* @param {(typeof SyntaxBase)[]} hooksConfig
* @param {Partial<CherryOptions>} editorConfig
* @param {CherryOptions} editorConfig
*/
constructor(hooksConfig, editorConfig, cherry) {
this.$locale = cherry.locale;
Expand All @@ -129,7 +129,7 @@ export default class HookCenter {
/**
* 注册系统默认的语法hook
* @param {any[]} hooksConfig 在hookconfig.js里定义的配置
* @param {Partial<CherryOptions>} editorConfig 编辑器配置
* @param {CherryOptions} editorConfig 编辑器配置
*/
registerInternalHooks(hooksConfig, editorConfig) {
hooksConfig.forEach(
Expand All @@ -148,7 +148,7 @@ export default class HookCenter {
/**
* 注册第三方的语法hook
* @param {CherryEngineOptions['customSyntax']} customHooks 用户传入的配置
* @param {Partial<CherryOptions>} editorConfig 编辑器配置
* @param {CherryOptions} editorConfig 编辑器配置
*/
registerCustomHooks(customHooks, editorConfig) {
if (!customHooks) {
Expand Down Expand Up @@ -202,7 +202,7 @@ export default class HookCenter {
/**
*
* @param {((...args: any[]) => any) | typeof SyntaxBase} HookClass
* @param {Partial<CherryOptions>} editorConfig
* @param {CherryOptions} editorConfig
* @param {Omit<CustomSyntaxRegConfig, 'syntaxClass'>} [customHookConfig]
* @returns
*/
Expand Down
138 changes: 90 additions & 48 deletions types/cherry.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@ import CodeMirror from 'codemirror';
import SyntaxBase from '../src/core/SyntaxBase';
import { FormulaMenu } from '@/toolbars/BubbleFormula';

export interface Cherry {
options: CherryOptions;

export interface CherryExternalsOptions {
[key: string]: any;
}

type CherryToolbarsCustomType = {
CustomMenuType: CherryExternalsOptions
}

type CherryCustomOptions = {
CustomToolbar: CherryToolbarsCustomType
}

export interface CherryOptions {
export interface Cherry<T extends CherryCustomOptions = CherryCustomOptions> {
options: CherryOptions<T>;
}

export type CherryOptions<T extends CherryCustomOptions = CherryCustomOptions> = Partial<_CherryOptions<T>>;

export interface _CherryOptions<T extends CherryCustomOptions = CherryCustomOptions> {
openai: any;
/** 第三方依赖 */
externals: CherryExternalsOptions;
Expand All @@ -15,7 +30,7 @@ export interface CherryOptions {
/** 编辑区域配置 */
editor: CherryEditorOptions;
/** 工具栏区域配置 */
toolbars: CherryToolbarOptions;
toolbars: CherryToolbarsOptions<T['CustomToolbar']> | undefined;
// 打开draw.io编辑页的url,如果为空则drawio按钮失效
drawioIframeUrl: string;
// drawio iframe的样式
Expand All @@ -41,7 +56,7 @@ export interface CherryOptions {
file: boolean;
};
/** 有哪些主题 */
theme: {className: string, label: string}[];
theme: { className: string, label: string }[];
/** 定义主题的作用范围,相同themeNameSpace的实例共享主题配置 */
themeNameSpace: string,
callback: {
Expand All @@ -62,11 +77,11 @@ export interface CherryOptions {
/** img 标签挂载前触发,可用于懒加载等场景 */
beforeImageMounted?: (srcProp: string, src: string) => { srcProp: string; src: string };
onClickPreview?: (e: MouseEvent) => void;
onCopyCode?: (e: ClipboardEvent, code: string) => string|false;
onExpandCode?: (e: ClipboardEvent, code: string) => string|false;
onUnExpandCode?: (e: ClipboardEvent, code: string) => string|false;
onCopyCode?: (e: ClipboardEvent, code: string) => string | false;
changeString2Pinyin?: (str: string) => string;
onPaste?: (clipboardData: ClipboardEvent['clipboardData']) => string|boolean;
onPaste?: (clipboardData: ClipboardEvent['clipboardData']) => string | boolean;
onExpandCode?: (e: MouseEvent, code: string) => string;
onUnExpandCode?: (e: MouseEvent, code: string) => string;
};
event: {
focus?: ({ e: MouseEvent, cherry: Cherry }) => void;
Expand All @@ -75,7 +90,7 @@ export interface CherryOptions {
afterChange?: CherryLifecycle;
/** 编辑器完成初次渲染后触发 */
afterInit?: CherryLifecycle;
selectionChange?: ({ selections: [], lastSelections: [], info} ) => void;
selectionChange?: ({ selections: [], lastSelections: [], info }) => void;
};
/** 预览区域配置 */
previewer: CherryPreviewerOptions;
Expand All @@ -101,10 +116,6 @@ export interface CherryOptions {
}
}

export interface CherryExternalsOptions {
[key: string]: any;
}

/**
* 自定义语法注册配置
*/
Expand Down Expand Up @@ -193,7 +204,7 @@ export interface CherryEditorOptions {
showSuggestList?: boolean;
}

export type CherryLifecycle = (text: String, html: String) => void;
export type CherryLifecycle = (text: string, html: string) => void;

export interface CherryPreviewerOptions {
dom: HTMLDivElement | false;
Expand Down Expand Up @@ -228,32 +239,60 @@ export type CherryToolbarSeparator = '|';
export type CherryCustomToolbar = string;

export type CherryDefaultToolbar =
| CherryInsertToolbar
| CherryToolbarSeparator
// | CherryToolbarSeparator
| 'audio'
| 'barTable'
| 'bold'
| 'italic'
| 'strikethrough'
| 'br'
| 'checklist'
| 'code'
| 'codeTheme'
| 'color'
| 'copy'
| 'detail'
| 'drawIo'
| 'export'
| 'file'
| 'fullScreen'
| 'formula'
| 'graph'
| 'h1'
| 'h2'
| 'h3'
| 'header'
| 'list'
| 'hr'
| 'image'
| 'audio'
| 'video'
| 'insert'
| 'italic'
| 'justify'
| 'lineTable'
| 'link'
| 'hr'
| 'br'
| 'code'
| 'formula'
| 'toc'
| 'table'
| 'list'
| 'mobilePreview'
| 'ol'
| 'panel'
| 'pdf'
| 'publish'
| 'quickTable'
| 'quote'
| 'redo'
| 'ruby'
| 'settings'
| 'size'
| 'strikethrough'
| 'sub'
| 'sup'
| 'switchModel'
| 'table'
| 'theme'
| 'toc'
| 'togglePreview'
| 'underline'
| 'undo'
| 'ul'
| 'video'
| 'word'
| 'graph'
| 'settings';

export type CherryInsertToolbar = {
insert: string[];
};
| 'wordCount';

export type CherryDefaultBubbleToolbar =
| CherryToolbarSeparator
Expand Down Expand Up @@ -306,21 +345,24 @@ export interface CherryToolbarConfig {
formula?: CherryFormulaToolbarOption
}

export interface CherryToolbarOptions {
export interface CherryToolbarsOptions<F extends CherryToolbarsCustomType = CherryToolbarsCustomType> {
theme: 'light' | 'dark';
toolbar?:
| (CherryCustomToolbar | CherryDefaultBubbleToolbar | CherryDefaultBubbleToolbar | CherryDefaultToolbar)[]
| false;
| (CherryCustomToolbar |
CherryDefaultBubbleToolbar |
CherryDefaultToolbar |
{ [K in keyof Partial<F['CustomMenuType']> | CherryDefaultToolbar]?: (keyof F['CustomMenuType'] | CherryDefaultToolbar)[] })[]
| false;
toolbarRight?:
| (CherryCustomToolbar | CherryDefaultBubbleToolbar | CherryDefaultBubbleToolbar | CherryDefaultToolbar)[]
| false;
| (CherryCustomToolbar | CherryDefaultBubbleToolbar | CherryDefaultToolbar)[]
| false;
/** 是否展示悬浮目录 */
toc?: false | {
updateLocationHash: boolean, // 要不要更新URL的hash
defaultModel: 'pure' | 'full', // pure: 精简模式/缩略模式,只有一排小点; full: 完整模式,会展示所有标题
showAutoNumber: boolean, // 是否显示自增序号
position: 'absolute' | 'fixed', // 悬浮目录的悬浮方式。当滚动条在cherry内部时,用absolute;当滚动条在cherry外部时,用fixed
cssText: string, // 额外样式
updateLocationHash?: boolean, // 要不要更新URL的hash
defaultModel?: 'pure' | 'full', // pure: 精简模式/缩略模式,只有一排小点; full: 完整模式,会展示所有标题
showAutoNumber?: boolean, // 是否显示自增序号
position?: 'absolute' | 'fixed', // 悬浮目录的悬浮方式。当滚动条在cherry内部时,用absolute;当滚动条在cherry外部时,用fixed
cssText?: string, // 额外样式
};
/** 不展示在编辑器中的工具栏,只使用工具栏的api和快捷键功能 */
hiddenToolbar?: any[];
Expand All @@ -335,7 +377,7 @@ export interface CherryToolbarOptions {
customMenu?: Record<string, any>;
/** 自定义快捷键 */
shortcutKey?: Object | false;
/** 一些按钮的配置信息 */
/** 一些按钮的配置信息 */
config?: CherryToolbarConfig;
}

Expand All @@ -354,8 +396,8 @@ export interface CherryFileUploadHandler {
* @param params.width 设置宽度,可以是像素、也可以是百分比(图片、视频场景下生效)
* @param params.height 设置高度,可以是像素、也可以是百分比(图片、视频场景下生效)
*/
callback: (url: string, params?: {name?: string, poster?: string, isBorder?: boolean, isShadow?: boolean, isRadius?: boolean; width?: string, height?: string}
) => void): void;
callback: (url: string, params?: { name?: string, poster?: string, isBorder?: boolean, isShadow?: boolean, isRadius?: boolean; width?: string, height?: string }
) => void): void;
}


Expand All @@ -374,4 +416,4 @@ type ShortcutKeyMapStruct = {
* @summary 切记不要使用驼峰,因为dataset 会全部转成全小写,除非你在取值的时候能记住,否则永远不要这么做
*/
[x: string]: string | number;
}
}
Loading