@@ -4,7 +4,7 @@ import keyDownTypesEnum from "./keyDownTypesEnum"
44import noopTextContent from "./noopTextContent"
55import React from "react"
66import ReactDOM from "react-dom"
7- import toReact from "./toReact "
7+ import ReactRenderer from "./ReactRenderer "
88import useEditor from "./useEditor"
99import uuidv4 from "uuid/v4"
1010import { computeCursors } from "./cursors"
@@ -27,81 +27,6 @@ import "./Editor.css"
2727 noopTextContent ( )
2828} ) ( )
2929
30- // Computes an array of types and type map for a pseudo-
31- // React element.
32- function computeTypeInfo ( element ) {
33- const types = [ ]
34- const typeMap = { }
35- if ( typeof element === "string" ) {
36- return [ types , typeMap ]
37- }
38- let ref = element . type !== undefined && // NOTE: "type" can be 0
39- element
40- while ( ref ) {
41- types . push ( ref . type )
42- typeMap [ ref . type ] = ref
43- ref = ref . props . children . type !== undefined && // NOTE: "type" can be 0
44- ref . props . children
45- }
46- return [ types , typeMap ]
47- }
48-
49- // Decorates pseudo-React elements; sets element.pos to
50- // "at-start", "at-center", or "at-end".
51- function decoratePos ( elements ) {
52- for ( let x = 0 ; x < elements . length ; x ++ ) {
53- if ( ! x || typeof elements [ x - 1 ] === "string" || typeof elements [ x ] === "string" ) {
54- // No-op
55- continue
56- }
57- const [ types1 , typeMap1 ] = computeTypeInfo ( elements [ x - 1 ] )
58- const [ types2 , typeMap2 ] = computeTypeInfo ( elements [ x ] )
59- const common = types1 . filter ( a => types2 . some ( b => a === b ) )
60- for ( const type of common ) {
61- typeMap1 [ type ] . props . pos = ! typeMap1 [ type ] . props . pos ? "at-start" : "at-center"
62- typeMap2 [ type ] . props . pos = "at-end"
63- }
64- }
65- }
66-
67- // Parses spans to pseudo-React elements.
68- function parseSpans ( spans ) {
69- const elements = [ ]
70- for ( const span of spans ) {
71- if ( ! span . formats . length ) {
72- elements . push ( span . content )
73- continue
74- }
75- const element = { }
76- let lastRef = element
77- let ref = lastRef
78- for ( const format of span . formats . sort ( ) ) {
79- Object . assign ( ref , { // <- lastRef
80- type : format ,
81- props : {
82- ...span [ format ] ,
83- children : { } , // <- ref
84- } ,
85- } )
86- lastRef = ref
87- ref = ref . props . children
88- }
89- lastRef . props . children = span . content
90- elements . push ( element )
91- }
92- decoratePos ( elements )
93- return elements
94- }
95-
96- const ReactRenderer = ( { state, dispatch, renderableMap } ) => (
97- state . elements . map ( ( { type : T , spans, ...props } ) => (
98- React . createElement ( T , {
99- key : props . uuid ,
100- ...props ,
101- } , toReact ( parseSpans ( spans ) , renderableMap ) )
102- ) )
103- )
104-
10530const Editor = ( ) => {
10631
10732 // Maps enum types to renderable React components.
0 commit comments