-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c96bc83
commit 6413135
Showing
10 changed files
with
219 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* @description header helper | ||
* @author wangfupeng | ||
*/ | ||
|
||
import { Editor, Transforms } from 'slate' | ||
import { IDomEditor, DomEditor } from '@wangeditor/core' | ||
|
||
/** | ||
* 获取 node type('header1' 'header2' 等),未匹配则返回 'paragraph' | ||
*/ | ||
export function getHeaderType(editor: IDomEditor): string { | ||
const [match] = Editor.nodes(editor, { | ||
match: n => { | ||
const type = DomEditor.getNodeType(n) | ||
return type.startsWith('header') // 匹配 node.type 是 header 开头的 node | ||
}, | ||
universal: true, | ||
}) | ||
|
||
// 未匹配到 header | ||
if (match == null) return 'paragraph' | ||
|
||
// 匹配到 header | ||
const [n] = match | ||
|
||
return DomEditor.getNodeType(n) | ||
} | ||
|
||
export function isMenuDisabled(editor: IDomEditor): boolean { | ||
if (editor.selection == null) return true | ||
|
||
const [nodeEntry] = Editor.nodes(editor, { | ||
match: n => { | ||
const type = DomEditor.getNodeType(n) | ||
|
||
// 只可用于 p 和 header | ||
if (type === 'paragraph') return true | ||
if (type.startsWith('header')) return true | ||
|
||
return false | ||
}, | ||
universal: true, | ||
mode: 'highest', // 匹配最高层级 | ||
}) | ||
|
||
// 匹配到 p header ,不禁用 | ||
if (nodeEntry) { | ||
return false | ||
} | ||
// 未匹配到 p header ,则禁用 | ||
return true | ||
} | ||
|
||
/** | ||
* 设置 node type ('header1' 'header2' 'paragraph' 等) | ||
*/ | ||
export function setHeaderType(editor: IDomEditor, type: string) { | ||
if (!type) return | ||
|
||
// 执行命令 | ||
Transforms.setNodes(editor, { | ||
type: type, | ||
}) | ||
} |
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
13 changes: 13 additions & 0 deletions
13
packages/basic-modules/src/modules/header/menu/Header1ButtonMenu.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 header1 button menu | ||
* @author wangfupeng | ||
*/ | ||
|
||
import HeaderButtonMenuBase from './HeaderButtonMenuBase' | ||
|
||
class Header1ButtonMenu extends HeaderButtonMenuBase { | ||
title = 'H1' | ||
type = 'header1' | ||
} | ||
|
||
export default Header1ButtonMenu |
13 changes: 13 additions & 0 deletions
13
packages/basic-modules/src/modules/header/menu/Header2ButtonMenu.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 header2 button menu | ||
* @author wangfupeng | ||
*/ | ||
|
||
import HeaderButtonMenuBase from './HeaderButtonMenuBase' | ||
|
||
class Header2ButtonMenu extends HeaderButtonMenuBase { | ||
title = 'H2' | ||
type = 'header2' | ||
} | ||
|
||
export default Header2ButtonMenu |
13 changes: 13 additions & 0 deletions
13
packages/basic-modules/src/modules/header/menu/Header3ButtonMenu.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 header3 button menu | ||
* @author wangfupeng | ||
*/ | ||
|
||
import HeaderButtonMenuBase from './HeaderButtonMenuBase' | ||
|
||
class Header3ButtonMenu extends HeaderButtonMenuBase { | ||
title = 'H3' | ||
type = 'header3' | ||
} | ||
|
||
export default Header3ButtonMenu |
45 changes: 45 additions & 0 deletions
45
packages/basic-modules/src/modules/header/menu/HeaderButtonMenuBase.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,45 @@ | ||
/** | ||
* @description button menu base | ||
* @author wangfupeng | ||
*/ | ||
|
||
import { IButtonMenu, IDomEditor } from '@wangeditor/core' | ||
import { getHeaderType, isMenuDisabled, setHeaderType } from '../helper' | ||
|
||
abstract class HeaderButtonMenuBase implements IButtonMenu { | ||
abstract readonly title: string | ||
abstract readonly type: string // 'header1' 'header2' 等 | ||
readonly tag = 'button' | ||
|
||
/** | ||
* 获取选中节点的 node.type | ||
* @param editor editor | ||
*/ | ||
getValue(editor: IDomEditor): string | boolean { | ||
return getHeaderType(editor) | ||
} | ||
|
||
isActive(editor: IDomEditor): boolean { | ||
return this.getValue(editor) === this.type | ||
} | ||
|
||
isDisabled(editor: IDomEditor): boolean { | ||
return isMenuDisabled(editor) | ||
} | ||
|
||
exec(editor: IDomEditor, value: string | boolean) { | ||
const { type } = this | ||
let newType | ||
if (value === type) { | ||
// 选中的 node.type 和当前 type 一样,则取消 | ||
newType = 'paragraph' | ||
} else { | ||
// 否则,则设置 | ||
newType = type | ||
} | ||
|
||
setHeaderType(editor, newType) | ||
} | ||
} | ||
|
||
export default HeaderButtonMenuBase |
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
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