am-editor11212/examples/react/components/editor/config.tsx

410 lines
11 KiB
TypeScript
Raw Normal View History

2021-12-24 00:45:49 +08:00
import { $ } from '@aomao/engine';
import type {
2021-11-03 19:58:08 +08:00
PluginEntry,
CardEntry,
PluginOptions,
NodeInterface,
} from '@aomao/engine';
//引入插件 begin
import Redo from '@aomao/plugin-redo';
2021-12-28 16:15:28 +08:00
// import type { RedoOptions } from '@aomao/plugin-redo';
2021-11-03 19:58:08 +08:00
import Undo from '@aomao/plugin-undo';
2021-12-28 16:15:28 +08:00
// import type { UndoOptions } from '@aomao/plugin-undo';
2021-11-03 19:58:08 +08:00
import Bold from '@aomao/plugin-bold';
2021-12-28 16:15:28 +08:00
// import type { BoldOptions } from '@aomao/plugin-bold';
2021-11-03 19:58:08 +08:00
import Code from '@aomao/plugin-code';
2021-12-28 16:15:28 +08:00
// import type { CodeOptions } from '@aomao/plugin-code';
2021-11-03 19:58:08 +08:00
import Backcolor from '@aomao/plugin-backcolor';
2021-12-28 16:15:28 +08:00
// import type { BackcolorOptions } from '@aomao/plugin-backcolor';
2021-11-03 19:58:08 +08:00
import Fontcolor from '@aomao/plugin-fontcolor';
2021-12-28 16:15:28 +08:00
// import type { FontcolorOptions } from '@aomao/plugin-fontcolor';
2021-11-03 19:58:08 +08:00
import Fontsize from '@aomao/plugin-fontsize';
2021-12-24 00:45:49 +08:00
import type { FontsizeOptions } from '@aomao/plugin-fontsize';
2021-11-03 19:58:08 +08:00
import Italic from '@aomao/plugin-italic';
2021-12-24 00:45:49 +08:00
import type { ItalicOptions } from '@aomao/plugin-italic';
2021-11-03 19:58:08 +08:00
import Underline from '@aomao/plugin-underline';
2021-12-28 16:15:28 +08:00
// import type { UnderlineOptions } from '@aomao/plugin-underline';
2021-11-03 19:58:08 +08:00
import Hr, { HrComponent } from '@aomao/plugin-hr';
2021-12-28 16:15:28 +08:00
// import type { HrOptions } from '@aomao/plugin-hr';
2021-11-03 19:58:08 +08:00
import Tasklist, { CheckboxComponent } from '@aomao/plugin-tasklist';
2021-12-28 16:15:28 +08:00
// import type { TasklistOptions } from '@aomao/plugin-tasklist';
2021-11-03 19:58:08 +08:00
import Orderedlist from '@aomao/plugin-orderedlist';
2021-12-28 16:15:28 +08:00
// import type { OrderedlistOptions } from '@aomao/plugin-orderedlist';
2021-11-03 19:58:08 +08:00
import Unorderedlist from '@aomao/plugin-unorderedlist';
2021-12-28 16:15:28 +08:00
// import type { UnorderedlistOptions } from '@aomao/plugin-unorderedlist';
2021-11-03 19:58:08 +08:00
import Indent from '@aomao/plugin-indent';
2021-12-28 16:15:28 +08:00
// import type { IndentOptions } from '@aomao/plugin-indent';
2021-11-03 19:58:08 +08:00
import Heading from '@aomao/plugin-heading';
2021-12-28 16:15:28 +08:00
// import type { HeadingOptions } from '@aomao/plugin-heading';
2021-11-03 19:58:08 +08:00
import Strikethrough from '@aomao/plugin-strikethrough';
2021-12-28 16:15:28 +08:00
// import type { StrikethroughOptions } from '@aomao/plugin-strikethrough';
2021-11-03 19:58:08 +08:00
import Sub from '@aomao/plugin-sub';
2021-12-28 16:15:28 +08:00
// import type { SubOptions } from '@aomao/plugin-sub';
2021-11-03 19:58:08 +08:00
import Sup from '@aomao/plugin-sup';
2021-12-28 16:15:28 +08:00
// import type { SupOptions } from '@aomao/plugin-sup';
2021-11-03 19:58:08 +08:00
import Alignment from '@aomao/plugin-alignment';
2021-12-28 16:15:28 +08:00
// import type { AlignmentOptions } from '@aomao/plugin-alignment';
2021-11-03 19:58:08 +08:00
import Mark from '@aomao/plugin-mark';
2021-12-28 16:15:28 +08:00
// import type { MarkOptions } from '@aomao/plugin-mark';
2021-11-03 19:58:08 +08:00
import Quote from '@aomao/plugin-quote';
2021-12-28 16:15:28 +08:00
// import type { QuoteOptions } from '@aomao/plugin-quote';
2021-11-03 19:58:08 +08:00
import PaintFormat from '@aomao/plugin-paintformat';
2021-12-28 16:15:28 +08:00
// import type { PaintformatOptions } from '@aomao/plugin-paintformat';
2021-11-03 19:58:08 +08:00
import RemoveFormat from '@aomao/plugin-removeformat';
2021-12-28 16:15:28 +08:00
// import type { RemoveformatOptions } from '@aomao/plugin-removeformat';
2021-11-03 19:58:08 +08:00
import SelectAll from '@aomao/plugin-selectall';
2021-12-28 16:15:28 +08:00
// import type { SelectAllOptions } from '@aomao/plugin-selectall';
2021-11-03 19:58:08 +08:00
import Link from '@aomao/plugin-link';
2021-12-28 16:15:28 +08:00
// import type { LinkOptions } from '@aomao/plugin-link';
2021-11-03 19:58:08 +08:00
import Codeblock, { CodeBlockComponent } from '@aomao/plugin-codeblock';
2021-12-28 16:15:28 +08:00
// import type { CodeblockOptions } from '@aomao/plugin-codeblock';
2021-11-03 19:58:08 +08:00
import Image, { ImageComponent, ImageUploader } from '@aomao/plugin-image';
2021-12-28 16:15:28 +08:00
import type { ImageOptions } from '@aomao/plugin-image';
2021-11-03 19:58:08 +08:00
import Table, { TableComponent } from '@aomao/plugin-table';
2021-12-28 16:15:28 +08:00
import type { TableOptions } from '@aomao/plugin-table';
2021-11-03 19:58:08 +08:00
import MarkRange from '@aomao/plugin-mark-range';
2021-12-28 16:15:28 +08:00
import type { MarkRangeOptions } from '@aomao/plugin-mark-range';
2021-11-03 19:58:08 +08:00
import File, { FileComponent, FileUploader } from '@aomao/plugin-file';
2021-12-28 16:15:28 +08:00
import type { FileOptions } from '@aomao/plugin-file';
2021-11-03 19:58:08 +08:00
import Video, { VideoComponent, VideoUploader } from '@aomao/plugin-video';
2021-12-28 16:15:28 +08:00
import type { VideoOptions, VideoUploaderOptions } from '@aomao/plugin-video';
2021-11-03 19:58:08 +08:00
import Math, { MathComponent } from '@aomao/plugin-math';
2021-12-28 16:15:28 +08:00
import type { MathOptions } from '@aomao/plugin-math';
2021-11-03 19:58:08 +08:00
import Fontfamily from '@aomao/plugin-fontfamily';
2021-12-28 16:15:28 +08:00
import type { FontfamilyOptions } from '@aomao/plugin-fontfamily';
2021-11-03 19:58:08 +08:00
import Status, { StatusComponent } from '@aomao/plugin-status';
2021-12-28 16:15:28 +08:00
// import type { StatusOptions } from '@aomao/plugin-status';
2021-11-03 19:58:08 +08:00
import LineHeight from '@aomao/plugin-line-height';
2021-12-28 16:15:28 +08:00
import type { LineHeightOptions } from '@aomao/plugin-line-height';
2021-11-03 19:58:08 +08:00
import Mention, { MentionComponent } from '@aomao/plugin-mention';
2021-12-28 16:15:28 +08:00
import type { MentionOptions } from '@aomao/plugin-mention';
2021-11-29 00:40:28 +08:00
import Embed, { EmbedComponent } from '@aomao/plugin-embed';
2021-12-28 16:15:28 +08:00
// import type { EmbedOptions } from '@aomao/plugin-embed'
2021-11-03 19:58:08 +08:00
import Test, { TestComponent } from './plugins/test';
import {
ToolbarPlugin,
ToolbarComponent,
fontFamilyDefaultData,
} from '@aomao/toolbar';
2021-12-31 01:14:24 +08:00
import type { ToolbarOptions } from '@aomao/toolbar';
2021-12-29 11:39:11 +08:00
2021-11-03 19:58:08 +08:00
import ReactDOM from 'react-dom';
import Loading from '../loading';
import Empty from 'antd/es/empty';
import 'antd/es/empty/style';
2021-12-28 16:15:28 +08:00
import { ImageUploaderOptions } from 'plugins/image/dist/uploader';
2021-11-03 19:58:08 +08:00
export const plugins: Array<PluginEntry> = [
Redo,
Undo,
Bold,
Code,
Backcolor,
Fontcolor,
Fontsize,
Italic,
Underline,
Hr,
Tasklist,
Orderedlist,
Unorderedlist,
Indent,
Heading,
Strikethrough,
Sub,
Sup,
Alignment,
Mark,
Quote,
PaintFormat,
RemoveFormat,
SelectAll,
Link,
Codeblock,
Image,
ImageUploader,
Table,
MarkRange,
File,
FileUploader,
Video,
VideoUploader,
Math,
ToolbarPlugin,
Fontfamily,
Status,
LineHeight,
Mention,
2021-11-29 00:40:28 +08:00
Embed,
2021-11-03 19:58:08 +08:00
Test,
];
2021-12-28 16:15:28 +08:00
export const cards: CardEntry[] = [
2021-11-03 19:58:08 +08:00
HrComponent,
CheckboxComponent,
CodeBlockComponent,
ImageComponent,
TableComponent,
FileComponent,
VideoComponent,
MathComponent,
ToolbarComponent,
StatusComponent,
MentionComponent,
TestComponent,
2021-11-29 00:40:28 +08:00
EmbedComponent,
2021-11-03 19:58:08 +08:00
];
2021-12-28 16:15:28 +08:00
export const tableOptions: TableOptions = {
overflow: {
maxLeftWidth: () => {
// 编辑区域位置
const rect = $('.am-engine')
.get<HTMLElement>()
?.getBoundingClientRect();
const editorLeft = rect?.left || 0;
// 减去大纲的宽度
const width = editorLeft - $('.data-toc-wrapper').width();
// 留 16px 的间隔
return width <= 0 ? 0 : width - 16;
},
maxRightWidth: () => {
// 编辑区域位置
const rect = $('.am-engine')
.get<HTMLElement>()
?.getBoundingClientRect();
const editorRigth = (rect?.right || 0) - (rect?.width || 0);
// 减去评论区域的宽度
const width = editorRigth - $('.doc-comment-layer').width();
// 留 16px 的间隔
return width <= 0 ? 0 : width - 16;
},
},
2021-12-28 16:15:28 +08:00
};
export const markRangeOptions: MarkRangeOptions = {
//标记类型集合
keys: ['comment'],
};
export const italicOptions: ItalicOptions = {
// 默认为 _ 下划线,这里修改为单个 * 号
markdown: '*',
};
export const imageOptions: ImageOptions = {
onBeforeRender: (status: string, url: string) => {
if (!url || url.indexOf('http') === 0) return url;
return url + `?token=12323`;
},
2021-12-28 16:15:28 +08:00
};
export const imageUploaderOptions: ImageUploaderOptions = {
file: {
action: '/api/upload/image',
headers: { Authorization: '213434' },
2021-11-03 19:58:08 +08:00
},
2021-12-28 16:15:28 +08:00
remote: {
action: '/api/upload/image',
2021-11-03 19:58:08 +08:00
},
2021-12-28 16:15:28 +08:00
isRemote: (src: string) => false,
};
export const fileOptions: FileOptions = {
action: '/api/upload/file',
};
export const videoOptions: VideoOptions = {
onBeforeRender: (status: string, url: string) => {
return url;
2021-11-03 19:58:08 +08:00
},
2021-12-28 16:15:28 +08:00
};
export const videoUploaderOptions: VideoUploaderOptions = {
action: '/api/upload/video',
limitSize: 1024 * 1024 * 50,
};
export const mathOptions: MathOptions = {
action: '/api/latex',
parse: (res: any) => {
if (res.success) return { result: true, data: res.svg };
return { result: false, data: '' };
2021-11-03 19:58:08 +08:00
},
2021-12-28 16:15:28 +08:00
};
export const mentionOptions: MentionOptions = {
action: '/api/user/search',
onLoading: (root: NodeInterface) => {
return ReactDOM.render(<Loading />, root.get<HTMLElement>()!);
2021-11-03 19:58:08 +08:00
},
2021-12-28 16:15:28 +08:00
onEmpty: (root: NodeInterface) => {
return ReactDOM.render(<Empty />, root.get<HTMLElement>()!);
2021-11-03 19:58:08 +08:00
},
2021-12-28 16:15:28 +08:00
onClick: (root: NodeInterface, { key, name }) => {
console.log('mention click:', key, '-', name);
2021-11-03 19:58:08 +08:00
},
2021-12-28 16:15:28 +08:00
onMouseEnter: (layout: NodeInterface, { name }) => {
ReactDOM.render(
<div style={{ padding: 5 }}>
<p>This is name: {name}</p>
<p> mention onMouseEnter </p>
<p>使 ReactDOM.render </p>
<p>Use ReactDOM.render to customize rendering here</p>
</div>,
layout.get<HTMLElement>()!,
);
2021-11-03 19:58:08 +08:00
},
2021-12-28 16:15:28 +08:00
};
export const fontsizeOptions: FontsizeOptions = {
//配置粘贴后需要过滤的字体大小
filter: (fontSize: string) => {
return (
[
'12px',
'13px',
'14px',
'15px',
'16px',
'19px',
'22px',
'24px',
'29px',
'32px',
'40px',
'48px',
].indexOf(fontSize) > -1
);
2021-11-03 19:58:08 +08:00
},
2021-12-28 16:15:28 +08:00
};
export const fontfamilyOptions: FontfamilyOptions = {
//配置粘贴后需要过滤的字体
filter: (fontfamily: string) => {
const item = fontFamilyDefaultData.find((item) =>
fontfamily
.split(',')
.some(
(name) =>
item.value
.toLowerCase()
.indexOf(name.replace(/"/, '').toLowerCase()) > -1,
),
);
return item ? item.value : false;
2021-11-03 19:58:08 +08:00
},
2021-12-28 16:15:28 +08:00
};
export const lineHeightOptions: LineHeightOptions = {
//配置粘贴后需要过滤的行高
filter: (lineHeight: string) => {
if (lineHeight === '14px') return '1';
if (lineHeight === '16px') return '1.15';
if (lineHeight === '21px') return '1.5';
if (lineHeight === '28px') return '2';
if (lineHeight === '35px') return '2.5';
if (lineHeight === '42px') return '3';
// 不满足条件就移除掉
return ['1', '1.15', '1.5', '2', '2.5', '3'].indexOf(lineHeight) > -1;
2021-11-03 19:58:08 +08:00
},
};
2021-12-28 16:15:28 +08:00
2021-12-31 01:14:24 +08:00
export const toolbarOptions: ToolbarOptions = {
// popup: {
// items: [
// ['undo', 'redo'],
// {
// icon: 'text',
// items: [
// 'bold',
// 'italic',
// 'strikethrough',
// 'underline',
// 'fontsize',
// 'fontcolor',
// 'backcolor',
// 'moremark',
// ],
// },
// [
// {
// type: 'button',
// name: 'image-uploader',
// icon: 'image',
// },
// 'link',
// 'tasklist',
// 'heading',
// ],
// {
// icon: 'more',
// items: [
// {
// type: 'button',
// name: 'video-uploader',
// icon: 'video',
// },
// {
// type: 'button',
// name: 'file-uploader',
// icon: 'attachment',
// },
// {
// type: 'button',
// name: 'table',
// icon: 'table',
// },
// {
// type: 'button',
// name: 'math',
// icon: 'math',
// },
// {
// type: 'button',
// name: 'codeblock',
// icon: 'codeblock',
// },
// {
// type: 'button',
// name: 'orderedlist',
// icon: 'ordered-list',
2021-12-31 01:14:24 +08:00
// },
// {
// type: 'button',
// name: 'unordered-list',
// icon: 'unordered-list',
2021-12-31 01:14:24 +08:00
// },
// {
// type: 'button',
// name: 'hr',
// icon: 'hr',
// },
// {
// type: 'button',
// name: 'quote',
// icon: 'quote',
// },
// ],
// },
// ]
// }
};
2021-12-28 16:15:28 +08:00
export const pluginConfig: Record<string, PluginOptions> = {
2021-12-31 01:14:24 +08:00
[ToolbarPlugin.pluginName]: toolbarOptions,
2021-12-28 16:15:28 +08:00
[Table.pluginName]: tableOptions,
[MarkRange.pluginName]: markRangeOptions,
[Italic.pluginName]: italicOptions,
[Image.pluginName]: imageOptions,
[ImageUploader.pluginName]: imageUploaderOptions,
[FileUploader.pluginName]: fileOptions,
[VideoUploader.pluginName]: videoUploaderOptions,
[Video.pluginName]: videoOptions,
[Math.pluginName]: mathOptions,
[Mention.pluginName]: mentionOptions,
[Fontsize.pluginName]: fontsizeOptions,
[Fontfamily.pluginName]: fontfamilyOptions,
[LineHeight.pluginName]: lineHeightOptions,
};