-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
136 lines (98 loc) · 4.62 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import {Modifier, ContentState, EditorState, RichUtils, convertFromHTML, createContentState} from "draft-js";
/*
The editor i am currently building is for speed and efficiency. Content is organised as blocks --- there are three levels of content. Highest level is header-two, 2nd is header-three... and unstyled text/unordered list item are treated as the same.
All bold has to be convered into header-two... All underlines are converted into header-three
This function implements that styling
*/
function DefaultHandleStyles(block, pastedContentState) {
let pastedEditorState = EditorState.createWithContent(pastedContentState)
const type = block.getType();
const text = block.getText();
let bold = false;
let underline = false;
block.findStyleRanges((value)=>{
if (value.hasStyle('BOLD'))
return true;
return false;
},(start, end)=>{
if(text.length==(end-start))
bold = true;
}
)
block.findStyleRanges((value)=>{
if (value.hasStyle('UNDERLINE'))
return true;
return false;
},(start, end)=>{
if(text.length==(end-start))
underline = true;
}
)
if (['unordered-list-item','ordered-list-item'].indexOf(type)!=-1)
pastedEditorState = RichUtils.toggleBlockType(pastedEditorState, 'unordered-list-item')
if ((["header-four","header-five","header-six"].indexOf(type)!=-1)||underline)
pastedEditorState = RichUtils.toggleBlockType(pastedEditorState, 'header-three');
//if an item was bold and underlined --- it would be converted into bold equivalent only
if ((["header-one","header-two", "header-three",].indexOf(type)!=-1)||bold)
pastedEditorState = RichUtils.toggleBlockType(pastedEditorState, 'header-two');
return pastedEditorState
}
//get tags from text
function DefaultHandleEntities(text) {
let pastedContentState = ContentState.createFromText(text)
//words starting with #
const tagRe = /(\s#[^#\s]+)|(^#[^#\s]+)/g
var m;
let matches = [];
do {
m = tagRe.exec(text);
if (m) {
var start = m.index;
//remember 'tagRe' gives us the matches including the \s before the tag. Adjusting for such matches
if (m[0].length>m[0].trim().length)
start+=1;
matches.push({text:m[0].trim(), start:start, end:start+m[0].trim().length})
}
} while (m);
matches.forEach((tag)=>{
const contentStateWithEntity = pastedContentState.createEntity('TAG', 'IMMUTABLE', {tag:tag.text.substring(1)});
const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
const selectionState = contentStateWithEntity.getSelectionAfter();
const newSelectionState = selectionState.merge({'anchorOffset':tag.start, 'focusOffset':tag.end});
pastedContentState = Modifier.applyEntity(contentStateWithEntity, newSelectionState, entityKey)
})
return pastedContentState
}
const HandlePastedText = (pastedText, html, editorState, onChange, HandleEntities=null, HandleStyles=null) => {
if (html || pastedText) {
var blocks;
//convert from html to text blocks by using draft-js functions or create a block if its plain text
if (html) {
const htmlContentState = convertFromHTML(html);
blocks = htmlContentState.contentBlocks.slice();
}
else {
blocks = ContentState.createFromText(pastedText).getBlocksAsArray();
}
let transformedBlocks = []
for (var i=0; i<blocks.length; i++) {
const block = blocks[i];
const text = block.getText();
//entities are added based on text and regex --- and converted into a contentState
const pastedContentState = (HandleEntities)?HandleEntities(text):DefaultHandleEntities(text)
//contentstate is converted to an editor state to add styling --- styling needs selectionState too - so, needed to convert to editorState
const pastedEditorState = (HandleStyles)?HandleStyles(block, pastedContentState):DefaultHandleStyles(block, pastedContentState);
//convert the entity'ed and styled editorState to a block --- to add to a list of blocks
transformedBlocks = transformedBlocks.concat(pastedEditorState.getCurrentContent().getBlocksAsArray())
}
const transformedContentState = ContentState.createFromBlockArray(transformedBlocks);
const currentContentState = editorState.getCurrentContent()
const selectionState = editorState.getSelection()
const pushContentState = Modifier.replaceWithFragment(currentContentState, selectionState, transformedContentState.getBlockMap())
const newEditorState = EditorState.push(editorState, pushContentState, 'insert-fragment');
onChange(newEditorState);
return true;
}
return false;
}
export default HandlePastedText;