-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #143 from wjw020206/master
feat(format painter menu): add format painter menu #141
- Loading branch information
Showing
11 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,4 +97,7 @@ export default { | |
todo: { | ||
todo: 'Todo', | ||
}, | ||
formatPainter: { | ||
title: 'Format Painter', | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,4 +97,7 @@ export default { | |
todo: { | ||
todo: '待办', | ||
}, | ||
formatPainter: { | ||
title: '格式刷', | ||
}, | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/basic-modules/src/modules/format-painter/helper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { IDomEditor } from '@wangeditor-next/core' | ||
import { SlateEditor } from '@wangeditor-next/editor' | ||
|
||
/** 清空所有标记(文本样式) */ | ||
export function clearAllMarks(editor: IDomEditor) { | ||
const marks = SlateEditor.marks(editor) | ||
|
||
if (marks) { | ||
Object.keys(marks).forEach(mark => { | ||
editor.removeMark(mark) | ||
}) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/basic-modules/src/modules/format-painter/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* @description format painter | ||
* @author CodePencil | ||
*/ | ||
|
||
import { IModuleConf } from '@wangeditor-next/core' | ||
import { formatPainterConf } from './menu/index' | ||
import withFormatPainter from './plugin' | ||
|
||
const formatPainter: Partial<IModuleConf> = { | ||
menus: [formatPainterConf], | ||
editorPlugin: withFormatPainter, | ||
} | ||
|
||
export default formatPainter |
67 changes: 67 additions & 0 deletions
67
packages/basic-modules/src/modules/format-painter/menu/FormatPainter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* @description Format Painter | ||
* @author CodePencil | ||
*/ | ||
|
||
import { IButtonMenu, IDomEditor, t } from '@wangeditor-next/core' | ||
import { SlateEditor } from '@wangeditor-next/editor' | ||
import { FORMAT_PAINTER } from '../../../constants/icon-svg' | ||
import { Text } from 'slate' | ||
import { clearAllMarks } from '../helper' | ||
|
||
interface FormatPaintAttributes { | ||
isSelect: boolean | ||
formatStyle: Omit<Text, 'text'> | null | ||
} | ||
|
||
class FormatPainter implements IButtonMenu { | ||
title = t('formatPainter.title') | ||
iconSvg = FORMAT_PAINTER | ||
tag = 'button' | ||
static attrs: FormatPaintAttributes = { | ||
isSelect: false, | ||
formatStyle: null, | ||
} | ||
|
||
getValue(editor: IDomEditor): string | boolean { | ||
return '' | ||
} | ||
|
||
isActive(editor: IDomEditor): boolean { | ||
return FormatPainter.attrs.isSelect | ||
} | ||
|
||
isDisabled(editor: IDomEditor): boolean { | ||
return false | ||
} | ||
|
||
setFormatHtml(editor: IDomEditor) { | ||
if (!editor.getSelectionText().length) return | ||
if (FormatPainter.attrs.formatStyle) { | ||
clearAllMarks(editor) | ||
for (const [key, value] of Object.entries(FormatPainter.attrs.formatStyle)) { | ||
editor.addMark(key, value) | ||
} | ||
} | ||
FormatPainter.attrs.formatStyle = {} | ||
FormatPainter.attrs.isSelect = false | ||
} | ||
|
||
exec(editor: IDomEditor, value: string | boolean) { | ||
// 如果已经选中了格式刷则取消选中,反之保存已经选中文本的样式 | ||
if (FormatPainter.attrs.isSelect) { | ||
FormatPainter.attrs.isSelect = false | ||
} else { | ||
// 判断是否选中文本 | ||
if (editor.getSelectionText().length) { | ||
FormatPainter.attrs.formatStyle = SlateEditor.marks(editor) | ||
FormatPainter.attrs.isSelect = true | ||
} | ||
} | ||
|
||
editor.blur() | ||
editor.focus() | ||
} | ||
} | ||
|
||
export default FormatPainter |
13 changes: 13 additions & 0 deletions
13
packages/basic-modules/src/modules/format-painter/menu/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* @description menu entry | ||
* @author CodePencil | ||
*/ | ||
|
||
import FormatPainter from './FormatPainter' | ||
|
||
export const formatPainterConf = { | ||
key: 'formatPainter', | ||
factory() { | ||
return new FormatPainter() | ||
}, | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/basic-modules/src/modules/format-painter/plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* @description editor 插件,重写 editor API | ||
* @author CodePencil | ||
*/ | ||
|
||
import { IDomEditor } from '@wangeditor-next/core' | ||
import FormatPainter from './menu/FormatPainter' | ||
|
||
function withFormatPainter<T extends IDomEditor>(editor: T): T { | ||
const formatPainter = new FormatPainter() | ||
|
||
const { onChange } = editor | ||
const newEditor = editor | ||
|
||
const handleMouseUp = () => { | ||
formatPainter.setFormatHtml(newEditor) | ||
document.removeEventListener('mouseup', handleMouseUp) | ||
} | ||
|
||
newEditor.onChange = () => { | ||
onChange() | ||
|
||
if (FormatPainter.attrs.isSelect) { | ||
// 避免重复绑定 mouseup 事件 | ||
document.removeEventListener('mouseup', handleMouseUp) | ||
document.addEventListener('mouseup', handleMouseUp) | ||
} | ||
} | ||
|
||
// 返回 editor ,重要! | ||
return newEditor | ||
} | ||
|
||
export default withFormatPainter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,6 +82,7 @@ export function genDefaultToolbarKeys() { | |
'undo', | ||
'redo', | ||
'|', | ||
'formatPainter', | ||
'fullScreen', | ||
] | ||
} | ||
|