@@ -15,109 +15,110 @@ import DraftUtils from 'draftjs-utils';
1515
1616/**
1717 * Wrapper around draftjs-utils, with our custom functions.
18- * import * as DraftUtils from '../utils/DraftUtils';
18+ * import DraftUtils from '../utils/DraftUtils';
1919 */
20-
21- export const getSelectionEntity = DraftUtils . getSelectionEntity . bind ( DraftUtils ) ;
22-
23- export const getEntityRange = DraftUtils . getEntityRange . bind ( DraftUtils ) ;
24-
25- export const getEntityData = ( entityKey ) => {
26- const entity = Entity . get ( entityKey ) ;
27- return entity . getData ( ) ;
28- } ;
29-
30- /**
31- * Returns the type of the block the current selection starts in.
32- */
33- export const getSelectedBlockType = ( editorState ) => {
34- const selectionState = editorState . getSelection ( ) ;
35- const contentState = editorState . getCurrentContent ( ) ;
36- const startKey = selectionState . getStartKey ( ) ;
37- const block = contentState . getBlockForKey ( startKey ) ;
38-
39- return block . type ;
40- } ;
41-
42- /**
43- * Creates a selection for the entirety of an entity that can be partially selected.
44- */
45- export const getSelectedEntitySelection = ( editorState ) => {
46- const selectionState = editorState . getSelection ( ) ;
47- const contentState = editorState . getCurrentContent ( ) ;
48- const entityKey = getSelectionEntity ( editorState ) ;
49- const entityRange = getEntityRange ( editorState , entityKey ) ;
50- const anchorKey = selectionState . getAnchorKey ( ) ;
51- const block = contentState . getBlockForKey ( anchorKey ) ;
52- const blockKey = block . getKey ( ) ;
53-
54- return new SelectionState ( {
55- anchorOffset : entityRange . start ,
56- anchorKey : blockKey ,
57- focusOffset : entityRange . end ,
58- focusKey : blockKey ,
59- isBackward : false ,
60- hasFocus : selectionState . getHasFocus ( ) ,
61- } ) ;
62- } ;
63-
64- // TODO Document.
65- export const insertBlock = ( editorState , entityKey , character , blockType ) => {
66- const contentState = editorState . getCurrentContent ( ) ;
67- const selectionState = editorState . getSelection ( ) ;
68-
69- const afterRemoval = Modifier . removeRange ( contentState , selectionState , 'backward' ) ;
70-
71- const targetSelection = afterRemoval . getSelectionAfter ( ) ;
72- const afterSplit = Modifier . splitBlock ( afterRemoval , targetSelection ) ;
73- const insertionTarget = afterSplit . getSelectionAfter ( ) ;
74-
75- const asAtomicBlock = Modifier . setBlockType ( afterSplit , insertionTarget , blockType ) ;
76-
77- const charData = CharacterMetadata . create ( { entity : entityKey } ) ;
78-
79- const fragmentArray = [
80- new ContentBlock ( {
81- key : genKey ( ) ,
82- type : blockType ,
83- text : character ,
84- characterList : List ( Repeat ( charData , character . length ) ) ,
85- } ) ,
86- new ContentBlock ( {
87- key : genKey ( ) ,
88- type : 'unstyled' ,
89- text : '' ,
90- characterList : List ( ) ,
91- } ) ,
92- ] ;
93-
94- const fragment = BlockMapBuilder . createFromArray ( fragmentArray ) ;
95-
96- const withBlock = Modifier . replaceWithFragment ( asAtomicBlock , insertionTarget , fragment ) ;
97-
98- const newContent = withBlock . merge ( {
99- selectionBefore : selectionState ,
100- selectionAfter : withBlock . getSelectionAfter ( ) . set ( 'hasFocus' , true ) ,
101- } ) ;
102-
103- return EditorState . push ( editorState , newContent , 'insert-fragment' ) ;
104- } ;
105-
106- // TODO Document.
107- export const createEntity = ( editorState , entityType , entityData , entityText , entityMutability = 'IMMUTABLE' ) => {
108- const entityKey = Entity . create ( entityType , entityMutability , entityData ) ;
109-
110- const contentState = editorState . getCurrentContent ( ) ;
111- const selection = editorState . getSelection ( ) ;
112-
113- let nextContentState ;
114-
115- if ( selection . isCollapsed ( ) ) {
116- nextContentState = Modifier . insertText ( contentState , editorState . getSelection ( ) , entityText , null , entityKey ) ;
117- } else {
118- nextContentState = Modifier . replaceText ( contentState , editorState . getSelection ( ) , entityText , null , entityKey ) ;
119- }
120-
121- const nextState = EditorState . push ( editorState , nextContentState , 'insert' ) ;
122- return nextState ;
20+ export default {
21+ getSelectionEntity : DraftUtils . getSelectionEntity . bind ( DraftUtils ) ,
22+
23+ getEntityRange : DraftUtils . getEntityRange . bind ( DraftUtils ) ,
24+
25+ getEntityData : ( entityKey ) => {
26+ const entity = Entity . get ( entityKey ) ;
27+ return entity . getData ( ) ;
28+ } ,
29+
30+ /**
31+ * Returns the type of the block the current selection starts in.
32+ */
33+ getSelectedBlockType : ( editorState ) => {
34+ const selectionState = editorState . getSelection ( ) ;
35+ const contentState = editorState . getCurrentContent ( ) ;
36+ const startKey = selectionState . getStartKey ( ) ;
37+ const block = contentState . getBlockForKey ( startKey ) ;
38+
39+ return block . type ;
40+ } ,
41+
42+ /**
43+ * Creates a selection for the entirety of an entity that can be partially selected.
44+ */
45+ getSelectedEntitySelection : ( editorState ) => {
46+ const selectionState = editorState . getSelection ( ) ;
47+ const contentState = editorState . getCurrentContent ( ) ;
48+ const entityKey = getSelectionEntity ( editorState ) ;
49+ const entityRange = getEntityRange ( editorState , entityKey ) ;
50+ const anchorKey = selectionState . getAnchorKey ( ) ;
51+ const block = contentState . getBlockForKey ( anchorKey ) ;
52+ const blockKey = block . getKey ( ) ;
53+
54+ return new SelectionState ( {
55+ anchorOffset : entityRange . start ,
56+ anchorKey : blockKey ,
57+ focusOffset : entityRange . end ,
58+ focusKey : blockKey ,
59+ isBackward : false ,
60+ hasFocus : selectionState . getHasFocus ( ) ,
61+ } ) ;
62+ } ,
63+
64+ // TODO Document.
65+ insertBlock : ( editorState , entityKey , character , blockType ) => {
66+ const contentState = editorState . getCurrentContent ( ) ;
67+ const selectionState = editorState . getSelection ( ) ;
68+
69+ const afterRemoval = Modifier . removeRange ( contentState , selectionState , 'backward' ) ;
70+
71+ const targetSelection = afterRemoval . getSelectionAfter ( ) ;
72+ const afterSplit = Modifier . splitBlock ( afterRemoval , targetSelection ) ;
73+ const insertionTarget = afterSplit . getSelectionAfter ( ) ;
74+
75+ const asAtomicBlock = Modifier . setBlockType ( afterSplit , insertionTarget , blockType ) ;
76+
77+ const charData = CharacterMetadata . create ( { entity : entityKey } ) ;
78+
79+ const fragmentArray = [
80+ new ContentBlock ( {
81+ key : genKey ( ) ,
82+ type : blockType ,
83+ text : character ,
84+ characterList : List ( Repeat ( charData , character . length ) ) ,
85+ } ) ,
86+ new ContentBlock ( {
87+ key : genKey ( ) ,
88+ type : 'unstyled' ,
89+ text : '' ,
90+ characterList : List ( ) ,
91+ } ) ,
92+ ] ;
93+
94+ const fragment = BlockMapBuilder . createFromArray ( fragmentArray ) ;
95+
96+ const withBlock = Modifier . replaceWithFragment ( asAtomicBlock , insertionTarget , fragment ) ;
97+
98+ const newContent = withBlock . merge ( {
99+ selectionBefore : selectionState ,
100+ selectionAfter : withBlock . getSelectionAfter ( ) . set ( 'hasFocus' , true ) ,
101+ } ) ;
102+
103+ return EditorState . push ( editorState , newContent , 'insert-fragment' ) ;
104+ } ,
105+
106+ // TODO Document.
107+ createEntity : ( editorState , entityType , entityData , entityText , entityMutability = 'IMMUTABLE' ) => {
108+ const entityKey = Entity . create ( entityType , entityMutability , entityData ) ;
109+
110+ const contentState = editorState . getCurrentContent ( ) ;
111+ const selection = editorState . getSelection ( ) ;
112+
113+ let nextContentState ;
114+
115+ if ( selection . isCollapsed ( ) ) {
116+ nextContentState = Modifier . insertText ( contentState , editorState . getSelection ( ) , entityText , null , entityKey ) ;
117+ } else {
118+ nextContentState = Modifier . replaceText ( contentState , editorState . getSelection ( ) , entityText , null , entityKey ) ;
119+ }
120+
121+ const nextState = EditorState . push ( editorState , nextContentState , 'insert' ) ;
122+ return nextState ;
123+ } ,
123124} ;
0 commit comments