Skip to content

Commit

Permalink
Merge pull request #13 from imnapo/np/textinput-remfix
Browse files Browse the repository at this point in the history
TextInput + Convert fix
  • Loading branch information
imnapo authored Feb 14, 2019
2 parents a8be73d + c8b3054 commit f0721ac
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 35 deletions.
76 changes: 49 additions & 27 deletions src/CNTextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,28 +674,39 @@ class CNTextInput extends Component {
const content = items;
let indx = 0;
let upComingAdded = false;

for (var i = 0; i < content.length; i++) {
const {id, len, stype,tag, NewLine, text, styleList, readOnly} = content[i];

const {id, len, stype,tag, text, styleList} = content[i];
const NewLine = content[i].NewLine ? content[i].NewLine : false;
const readOnly = content[i].readOnly ? content[i].readOnly : false;

let indexOfToolType = stype.indexOf(toolType);
let newStype =(indexOfToolType != -1) ?
update(stype, {$splice: [[indexOfToolType, 1]]})
: update(stype, { $push: [toolType] });




let newStyles = StyleSheet.flatten(this.convertStyleList(update(newStype, { $push: [tag] })));

let from = indx;
indx += len;
let to = indx;


if((start >= from && start < to) && (end >= from && end < to))
{


if(readOnly) {
newCollection.push({
id: id,
text: text,
len: to - from,
tag:tag,
stype: stype,
styleList: styleList,
NewLine: NewLine,
readOnly: readOnly
});
}
else if((start >= from && start < to) && (end >= from && end < to))
{

if(start !== end) {

if(start !== from)
Expand Down Expand Up @@ -769,7 +780,7 @@ class CNTextInput extends Component {
}
else if(start >= from && start < to)
{

if(start !== from)
{
newCollection.push({
Expand Down Expand Up @@ -827,7 +838,7 @@ class CNTextInput extends Component {

newCollection.push({
id: shortid.generate(),
text: text.substring(from, end - from),
text: text.substring(0, end - from),
len: end - from,
tag: tag,
NewLine: NewLine,
Expand All @@ -854,7 +865,6 @@ class CNTextInput extends Component {
}
}
else if(from === to && start === from && end === to) {

newCollection.push({
id: id,
text: text,
Expand All @@ -877,7 +887,23 @@ class CNTextInput extends Component {
upComingAdded = true;
}
}
else if ((from >= start && from <= end) && (to >= start && to <= end)) {

newCollection.push({
id: id,
text: text,
len: to - from,
tag:tag,
stype: newStype,
styleList: newStyles,
NewLine: NewLine,
readOnly: readOnly

});

}
else {

newCollection.push({
id: id,
text: text,
Expand All @@ -890,22 +916,20 @@ class CNTextInput extends Component {

});

}
}


}



const res = this.findContentIndex(newCollection, this.state.selection.end);

let styles = [];
if(this.upComingStype != null) {
styles = this.upComingStype.stype;

}
else {
styles = newCollection[res.findIndx].stype;;
}


this.justToolAdded = start !== end;
this.props.onContentChanged(newCollection);
Expand Down Expand Up @@ -935,9 +959,7 @@ class CNTextInput extends Component {
}

changeToTagIn(items, tag, index, fromTextChange = false) {




const needBold = tag === 'heading' || tag === 'title';
let content = items;

Expand All @@ -946,7 +968,8 @@ class CNTextInput extends Component {
break;
}
else {
content[i].tag = tag;


if(needBold === true && content[i].stype.indexOf('bold') == -1) {
content[i].stype = update(content[i].stype, { $push: ['bold'] })

Expand All @@ -956,8 +979,8 @@ class CNTextInput extends Component {
content[i].stype.indexOf('bold') != -1) {
content[i].stype = content[i].stype.filter(typ => typ != 'bold')
}
content[i].styleList = StyleSheet.flatten(this.convertStyleList(update(content[i].stype, { $push: [content[i].tag] })));

content[i].tag = tag;
content[i].styleList = StyleSheet.flatten(this.convertStyleList(update(content[i].stype, { $push: [content[i].tag] })));
}
}
let shouldReorderList = false;
Expand All @@ -967,10 +990,9 @@ class CNTextInput extends Component {
shouldReorderList = true;
}

content[i].tag = tag;

if(needBold === true &&
(content[i].tag === 'heading' || content[i].tag === 'title') &&
// (content[i].tag === 'heading' || content[i].tag === 'title') &&
content[i].stype.indexOf('bold') == -1) {
content[i].stype = update(content[i].stype, { $push: ['bold'] })
}
Expand All @@ -980,7 +1002,7 @@ class CNTextInput extends Component {
content[i].stype = content[i].stype.filter(typ => typ != 'bold')
}


content[i].tag = tag;
content[i].styleList = StyleSheet.flatten(this.convertStyleList(update(content[i].stype, { $push: [content[i].tag] })));

if(content[i].NewLine === true) {
Expand Down
12 changes: 4 additions & 8 deletions src/Convertors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ export function convertToHtmlString(contents, styleList = null) {
let availableStyles = styleList == null ? defaultStyles : styleList;

//let keys = Object.keys(availableStyles);
//console.log('contents', contents);

// console.log('==================================');
var myDoc = new DOMParser().parseFromString(
'<div></div>', 'text/xml'
);
Expand Down Expand Up @@ -151,7 +148,6 @@ export function convertToHtmlString(contents, styleList = null) {
}

export function convertToObject(htmlString) {
// console.log('htmlString', htmlString);

var doc = new DOMParser().parseFromString(htmlString, 'text/xml');
let contents = [];
Expand Down Expand Up @@ -239,7 +235,7 @@ export function convertToHtmlString(contents, styleList = null) {
}

let firstLine = (i == 0) || (i > 0 && item.content.length > 0 && item.content[item.content.length - 1].tag == 'image');


if(tag == 'ul' || tag == 'ol') {

Expand All @@ -266,7 +262,7 @@ export function convertToHtmlString(contents, styleList = null) {

item.content.push(
xmlNodeToItem(child, tag,false)
);
);
}
}
}
Expand All @@ -276,13 +272,13 @@ export function convertToHtmlString(contents, styleList = null) {
let child = element.childNodes[j];
let childItem = xmlNodeToItem(child,tag, firstLine == false && j == 0);
if(firstLine) {
childItem.NewLine = true;
childItem.NewLine = j==0;
}
item.content.push(
childItem
);
}
}
}

}
}
Expand Down

0 comments on commit f0721ac

Please sign in to comment.