-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(font-type): ✨ add new extension FontType
- Loading branch information
Showing
15 changed files
with
259 additions
and
3 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
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<template> | ||
<el-dropdown | ||
placement="bottom" | ||
trigger="click" | ||
@command="toggleFontType" | ||
> | ||
<command-button | ||
:tooltip="t('editor.extensions.FontType.tooltip')" | ||
icon="font" | ||
/> | ||
|
||
<el-dropdown-menu | ||
slot="dropdown" | ||
class="el-tiptap-dropdown-menu" | ||
> | ||
<el-dropdown-item | ||
v-for="name in fontTypes" | ||
:key="name" | ||
:command="name" | ||
:class="{ | ||
'el-tiptap-dropdown-menu__item--active': name === activeFontType, | ||
}" | ||
class="el-tiptap-dropdown-menu__item" | ||
> | ||
<span | ||
:data-font="name" | ||
:style="{ 'font-family': name }" | ||
>{{ name }}</span> | ||
</el-dropdown-item> | ||
</el-dropdown-menu> | ||
</el-dropdown> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { Component, Prop, Mixins } from 'vue-property-decorator'; | ||
import { MenuData } from 'tiptap'; | ||
import { Dropdown, DropdownMenu, DropdownItem } from 'element-ui'; | ||
import i18nMixin from '@/mixins/i18nMixin'; | ||
import { DEFAULT_FONT_TYPE_MAP, findActiveFontType } from '@/utils/font_type'; | ||
import { isPlainObject } from '@/utils/shared'; | ||
import Logger from '@/utils/logger'; | ||
import CommandButton from './CommandButton.vue'; | ||
@Component({ | ||
components: { | ||
[Dropdown.name]: Dropdown, | ||
[DropdownMenu.name]: DropdownMenu, | ||
[DropdownItem.name]: DropdownItem, | ||
CommandButton, | ||
}, | ||
}) | ||
export default class FontTypeDropdown extends Mixins(i18nMixin) { | ||
@Prop({ | ||
type: Object, | ||
required: true, | ||
}) | ||
readonly editorContext!: MenuData; | ||
private get editor () { | ||
return this.editorContext.editor; | ||
} | ||
private get fontTypes () { | ||
const { fontTypes } = this.editor.extensions.options.font_type; | ||
if (!isPlainObject(fontTypes)) { | ||
Logger.error('\'fontTypes\' should be an object'); | ||
return DEFAULT_FONT_TYPE_MAP; | ||
} | ||
return fontTypes; | ||
} | ||
private get activeFontType (): string { | ||
return findActiveFontType(this.editor.state); | ||
} | ||
private toggleFontType (name: string) { | ||
if (name === this.activeFontType) { | ||
this.editorContext.commands.font_type(''); | ||
} else { | ||
this.editorContext.commands.font_type(name); | ||
} | ||
} | ||
}; | ||
</script> |
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,73 @@ | ||
// @ts-nocheck | ||
import { Mark, MenuData } from 'tiptap'; | ||
import { CommandFunction } from 'tiptap-commands'; | ||
import { Node as ProsemirrorNode, MarkType } from 'prosemirror-model'; | ||
import { MenuBtnView } from '@/../types'; | ||
import FontTypeDropdown from '@/components/MenuCommands/FontTypeDropdown.vue'; | ||
import { DEFAULT_FONT_TYPE_MAP, setFontType } from '@/utils/font_type'; | ||
|
||
export default class FontType extends Mark implements MenuBtnView { | ||
get name () { | ||
return 'font_type'; | ||
} | ||
|
||
get defaultOptions () { | ||
return { | ||
fontTypes: DEFAULT_FONT_TYPE_MAP, | ||
}; | ||
} | ||
|
||
get schema () { | ||
return { | ||
attrs: { | ||
name: '', | ||
}, | ||
inline: true, | ||
group: 'inline', | ||
parseDOM: [ | ||
{ | ||
style: 'font-family', | ||
getAttrs: (name: string) => { | ||
return { | ||
name: name ? name.replace(/["']/g, '') : '', | ||
}; | ||
}, | ||
}, | ||
], | ||
toDOM (node: ProsemirrorNode) { | ||
const { name } = node.attrs; | ||
const attrs: { style?: string } = {}; | ||
|
||
if (name) { | ||
attrs.style = `font-family: ${name}`; | ||
} | ||
return ['span', attrs, 0]; | ||
}, | ||
}; | ||
} | ||
|
||
commands ({ type }: { type: MarkType }) { | ||
return (name: string): CommandFunction => (state, dispatch) => { | ||
let { tr } = state; | ||
tr = setFontType( | ||
state.tr.setSelection(state.selection), | ||
type, | ||
name, | ||
); | ||
if (tr.docChanged || tr.storedMarksSet) { | ||
dispatch && dispatch(tr); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
} | ||
|
||
menuBtnView (editorContext: MenuData) { | ||
return { | ||
component: FontTypeDropdown, | ||
componentProps: { | ||
editorContext, | ||
}, | ||
}; | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -132,6 +132,9 @@ export default { | |
}, | ||
}, | ||
}, | ||
FontType: { | ||
tooltip: '字体', | ||
}, | ||
TextColor: { | ||
tooltip: '文本颜色', | ||
}, | ||
|
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,68 @@ | ||
// @ts-ignore | ||
import { getMarkAttrs } from 'tiptap-utils'; | ||
import { Transaction, TextSelection, AllSelection, EditorState } from 'prosemirror-state'; | ||
import { Mark as ProsemirrorMark, MarkType } from 'prosemirror-model'; | ||
import applyMark from './apply_mark'; | ||
|
||
const DEFAULT_FONT_TYPE_NAMES = [ | ||
'Arial', | ||
'Arial Black', | ||
'Georgia', | ||
'Impact', | ||
'Tahoma', | ||
'Times New Roman', | ||
'Verdana', | ||
'Courier New', | ||
'Lucida Console', | ||
'Monaco', | ||
'monospace', | ||
]; | ||
|
||
export const DEFAULT_FONT_TYPE_MAP = DEFAULT_FONT_TYPE_NAMES.reduce((obj: { [key: string]: string }, type: string) => { | ||
obj[type] = type; | ||
return obj; | ||
}, {}); | ||
|
||
export function setFontType (tr: Transaction, type: MarkType, name: string): Transaction { | ||
const { selection } = tr; | ||
|
||
if (!(selection instanceof TextSelection || selection instanceof AllSelection)) { | ||
return tr; | ||
} | ||
const attrs = name ? { name } : null; | ||
tr = applyMark(tr, type, attrs); | ||
return tr; | ||
} | ||
|
||
const DEFAULT_FONT_TYPE = ''; | ||
|
||
export function findActiveFontType (state: EditorState): string { | ||
const { schema, selection, tr } = state; | ||
const markType = schema.marks.font_type; | ||
|
||
if (!markType) return DEFAULT_FONT_TYPE; | ||
|
||
const { empty } = selection; | ||
|
||
if (empty) { | ||
const storedMarks = tr.storedMarks || | ||
state.storedMarks || | ||
( | ||
selection instanceof TextSelection && | ||
selection.$cursor && | ||
selection.$cursor.marks && | ||
selection.$cursor.marks() | ||
) || | ||
[]; | ||
|
||
const sm = storedMarks.find((m: ProsemirrorMark) => m.type === markType); | ||
return (sm && sm.attrs.name) || DEFAULT_FONT_TYPE; | ||
} | ||
|
||
const attrs = getMarkAttrs(state, markType); | ||
const fontName = attrs.name; | ||
|
||
if (!fontName) return DEFAULT_FONT_TYPE; | ||
|
||
return fontName; | ||
} |
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