Skip to content

Commit

Permalink
style: refactor by new eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
cycleccc committed Oct 23, 2024
1 parent c2abdb2 commit c4dcfe2
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions packages/core/src/create/create-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@

import { createEditor, Descendant } from 'slate'
import { withHistory } from 'slate-history'
import { withDOM } from '../editor/plugins/with-dom'

import { genEditorConfig } from '../config/index'
import { IEditorConfig } from '../config/interface'
import { DomEditor } from '../editor/dom-editor'
import { IDomEditor } from '../editor/interface'
import { withConfig } from '../editor/plugins/with-config'
import { withContent } from '../editor/plugins/with-content'
import { withEventData } from '../editor/plugins/with-event-data'
import { withDOM } from '../editor/plugins/with-dom'
import { withEmitter } from '../editor/plugins/with-emitter'
import { withSelection } from '../editor/plugins/with-selection'
import { withEventData } from '../editor/plugins/with-event-data'
import { withMaxLength } from '../editor/plugins/with-max-length'
import TextArea from '../text-area/TextArea'
import { withSelection } from '../editor/plugins/with-selection'
import HoverBar from '../menus/bar/HoverBar'
import { genEditorConfig } from '../config/index'
import { IDomEditor } from '../editor/interface'
import { DomEditor } from '../editor/dom-editor'
import { IEditorConfig } from '../config/interface'
import { promiseResolveThen } from '../utils/util'
import { isRepeatedCreateTextarea, genDefaultContent, htmlToContent } from './helper'
import TextArea from '../text-area/TextArea'
import type { DOMElement } from '../utils/dom'
import { promiseResolveThen } from '../utils/util'
import {
EDITOR_TO_TEXTAREA,
TEXTAREA_TO_EDITOR,
EDITOR_TO_CONFIG,
HOVER_BAR_TO_EDITOR,
EDITOR_TO_HOVER_BAR,
EDITOR_TO_TEXTAREA,
HOVER_BAR_TO_EDITOR,
TEXTAREA_TO_EDITOR,
} from '../utils/weak-maps'
import bindNodeRelation from './bind-node-relation'
import $ from '../utils/dom'
import { genDefaultContent, htmlToContent, isRepeatedCreateTextarea } from './helper'

type PluginFnType = <T extends IDomEditor>(editor: T) => T

Expand All @@ -45,14 +45,17 @@ interface ICreateOption {
* 创建编辑器
*/
export default function (option: Partial<ICreateOption>) {
const { selector = '', config = {}, content, html, plugins = [] } = option
const {
selector = '', config = {}, content, html, plugins = [],
} = option

// 创建实例 - 使用插件
let editor = withHistory(
withMaxLength(
withEmitter(withSelection(withContent(withConfig(withDOM(withEventData(createEditor()))))))
)
withEmitter(withSelection(withContent(withConfig(withDOM(withEventData(createEditor())))))),
),
)

if (selector) {
// 检查是否对同一个 DOM 重复创建
if (isRepeatedCreateTextarea(editor, selector)) {
Expand All @@ -62,6 +65,7 @@ export default function (option: Partial<ICreateOption>) {

// 处理配置
const editorConfig = genEditorConfig(config)

EDITOR_TO_CONFIG.set(editor, editorConfig)
const { hoverbarKeys = {} } = editorConfig

Expand All @@ -86,23 +90,27 @@ export default function (option: Partial<ICreateOption>) {
if (selector) {
// 传入了 selector ,则创建 textarea DOM
const textarea = new TextArea(selector)

EDITOR_TO_TEXTAREA.set(editor, textarea)
TEXTAREA_TO_EDITOR.set(textarea, editor)
textarea.changeViewState() // 初始化时触发一次,以便能初始化 textarea DOM 和 selection

// 判断 textarea 最小高度,并给出提示
promiseResolveThen(() => {
const $scroll = textarea.$scroll
if ($scroll == null) return

if ($scroll == null) { return }
if ($scroll.height() < 300) {
let info = '编辑区域高度 < 300px 这可能会导致 modal hoverbar 定位异常'

info += '\nTextarea height < 300px . This may be cause modal and hoverbar position error'
console.warn(info, $scroll)
}
})

// 创建 hoverbar DOM
let hoverbar: HoverBar | null

if (Object.keys(hoverbarKeys).length > 0) {
hoverbar = new HoverBar()
HOVER_BAR_TO_EDITOR.set(hoverbar, editor)
Expand All @@ -123,6 +131,7 @@ export default function (option: Partial<ICreateOption>) {

// 触发生命周期
const { onCreated, onDestroyed } = editorConfig

if (onCreated) {
editor.on('created', () => onCreated(editor))
}
Expand Down

0 comments on commit c4dcfe2

Please sign in to comment.