import { createEditor , defaultPlugins , withHistory , Transforms , Range , Editor , EditorWithLink , EditorWithList , EditorWithBlock , EditorWithMark , EditorWithVoid , EditorWithContainer , } from 'editure' ; import { withPaste , withReact , ReactEditor } from 'editure-react' ; import * as F from 'editure-constants' ; import { IS_FIREFOX } from './environment' ; import { withImages } from './image' ; import { EXPLAIN , DIFF_BLOCK } from '../utils/constants' ; const withCommitHeaderLayout = ( editor ) => { const { normalizeNode } = editor ; editor . normalizeNode = ( [ node , path ] ) => { if ( path . length === 1 && node . fixed ) { if ( node . type === F . PARAGRAPH ) { const title = { type : F . H2 , children : [ { text : '' } ] } ; Transforms . setNodes ( editor , title ) ; } } return normalizeNode ( [ node , path ] ) ; } ; return editor ; } ; const withExplainLayout = ( editor ) => { const { deleteBackward } = editor ; editor . deleteBackward = ( unit ) => { const { selection } = editor ; if ( selection && Range . isCollapsed ( selection ) ) { const parent = Editor . parent ( editor , selection ) ; const grandparent = Editor . parent ( editor , parent [ 1 ] ) ; if ( grandparent [ 0 ] . type === EXPLAIN && Editor . isStart ( editor , selection . anchor , parent [ 1 ] ) && Editor . isStart ( editor , selection . anchor , grandparent [ 1 ] ) ) { return ; } } deleteBackward ( unit ) ; } ; return editor ; } ; const withDiffBlockVoid = ( editor ) => { const { isVoid } = editor ; editor . isVoid = ( element ) => { return element . type === DIFF_BLOCK ? true : isVoid ( element ) ; } ; return editor ; } ; const plugins = [ withReact , ... defaultPlugins , withImages , withExplainLayout , withCommitHeaderLayout , withDiffBlockVoid , withPaste , withHistory , ] ; export type IEditor = Editor & EditorWithList & EditorWithLink & EditorWithVoid & EditorWithBlock & EditorWithMark & EditorWithContainer & ReactEditor ; export const initializeEditor = ( ) => plugins . reduce ( ( augmentedEditor , plugin ) => plugin ( augmentedEditor ) , createEditor ( ) as IEditor ) as IEditor ; export const syncDOMSelection = ( editor ) => { const { selection } = editor ; const domSelection = window . getSelection ( ) ; if ( ! domSelection ) { return ; } const newDomRange = selection && ReactEditor . toDOMRange ( editor , selection ) ; const el = ReactEditor . toDOMNode ( editor , editor ) ; domSelection . removeAllRanges ( ) ; if ( newDomRange ) { domSelection . addRange ( newDomRange ! ) ; } setTimeout ( ( ) => { if ( newDomRange && IS_FIREFOX ) { el . focus ( ) ; } } ) ; } ;