Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle intersecting mutations #57

Merged
merged 2 commits into from
Apr 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/blockEntities.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import updateMutation from './util/updateMutation';
import rangeSort from './util/rangeSort';
import getElementHTML from './util/getElementHTML';
import getElementPrefix from './util/getElementPrefix';
import getElementTagLength from './util/getElementTagLength';

const converter = (entity = {}, originalText) => {
return originalText;
Expand Down Expand Up @@ -31,20 +31,32 @@ export default (block, entityMap, entityConverter = converter) => {
const converted = getElementHTML(entityHTML, originalText)
|| originalText;

const prefixLength = getElementPrefix(entityHTML);
const prefixLength = getElementTagLength(entityHTML, 'start');
const suffixLength = getElementTagLength(entityHTML, 'end');

const updateLaterMutation = (mutation, mutationIndex) => {
if (mutationIndex >= index || Object.prototype.hasOwnProperty.call(mutation, 'style')) {
return updateMutation(
mutation, entityRange.offset, entityRange.length,
converted.length, prefixLength
converted.length, prefixLength, suffixLength
);
}
return mutation;
};

entities = entities.map(updateLaterMutation);
styles = styles.map(updateLaterMutation);
const updateLaterMutations = mutationList => mutationList.reduce(
(acc, mutation, mutationIndex) => {
const updatedMutation = updateLaterMutation(mutation, mutationIndex);
if (Array.isArray(updatedMutation)) {
return acc.concat(updatedMutation);
}

return acc.concat([updatedMutation]);
}
, []);

entities = updateLaterMutations(entities);
styles = updateLaterMutations(styles);

resultText = resultText.substring(0, entityRange.offset)
+ converted
Expand Down
2 changes: 1 addition & 1 deletion src/encodeBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default block => {
resultText += encoded;

const updateForChar = mutation => {
return updateMutation(mutation, resultIndex, char.length, encoded.length, 0);
return updateMutation(mutation, resultIndex, char.length, encoded.length, 0, 0);
};

entities = entities.map(updateForChar);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react';
import splitReactElement from './splitReactElement';

export default element => {
export default (element, type = 'start') => {
if (React.isValidElement(element) && React.Children.count(element.props.children) === 0) {
return splitReactElement(element).start.length;
return splitReactElement(element)[type].length;
}

if (typeof element === 'object') {
return element.start ? element.start.length : 0;
return element[type] ? element[type].length : 0;
}

return 0;
Expand Down
57 changes: 50 additions & 7 deletions src/util/updateMutation.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,73 @@
export default function updateMutation(
mutation, originalOffset, originalLength, newLength, prefixLength
mutation,
originalOffset,
originalLength,
newLength,
prefixLength,
suffixLength
) {
// three cases we can reasonably adjust - disjoint mutations that
// happen later on where the offset will need to be changed,
// mutations that completely contain the new one where we can adjust
// the length, and mutations that occur partially within the new one.
const lengthDiff = newLength - originalLength;

if (originalOffset + originalLength <= mutation.offset) {
const mutationAfterChange = originalOffset + originalLength <= mutation.offset;
if (mutationAfterChange) {
return Object.assign({}, mutation, {
offset: mutation.offset + lengthDiff
});
}
if (
originalOffset >= mutation.offset
&& originalOffset + originalLength <= mutation.offset + mutation.length
) {

const mutationContainsChange = originalOffset >= mutation.offset
&& originalOffset + originalLength <= mutation.offset + mutation.length;
if (mutationContainsChange) {
return Object.assign({}, mutation, {
length: mutation.length + lengthDiff
});
}
if (originalOffset <= mutation.offset) {

const mutationWithinPrefixChange = mutation.offset >= originalOffset
&& mutation.offset + mutation.length <= originalOffset + originalLength
&& prefixLength > 0;
if (mutationWithinPrefixChange) {
return Object.assign({}, mutation, {
offset: mutation.offset + prefixLength
});
}

const mutationContainsPrefix = mutation.offset < originalOffset
&& mutation.offset + mutation.length <= originalOffset + originalLength
&& prefixLength > 0;
if (mutationContainsPrefix) {
return [
Object.assign({}, mutation, {
length: originalOffset - mutation.offset
}),
Object.assign({}, mutation, {
offset: originalOffset + prefixLength,
length: mutation.offset - originalOffset + mutation.length
})
];
}

const mutationContainsSuffix = mutation.offset >= originalOffset
&& mutation.offset + mutation.length > originalOffset + originalLength
&& suffixLength > 0;
if (mutationContainsSuffix) {
return [
Object.assign({}, mutation, {
offset: mutation.offset + prefixLength,
length: originalOffset + originalLength - mutation.offset
}),
Object.assign({}, mutation, {
offset: originalOffset + originalLength + prefixLength + suffixLength,
length:
mutation.offset + mutation.length
- (originalOffset + originalLength)
})
];
}

return mutation;
}
Loading