Skip to content

Commit

Permalink
Merge pull request #40 from ispras/kalmyk_markup
Browse files Browse the repository at this point in the history
Kalmyk markup
  • Loading branch information
nataliauvarova authored Dec 20, 2024
2 parents bce3b48 + 2c45f48 commit 099daf7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/components/LexicalEntryCorp/Text.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Button, Checkbox, Confirm } from "semantic-ui-react";
import { find, isEqual } from "lodash";
import PropTypes from "prop-types";
import { onlyUpdateForKeys } from "recompose";
//import { patienceDiff } from "utils/patienceDiff";
import { deleteMarkupGroupMutation, refetchLexicalEntries } from "components/JoinMarkupsModal";
import { useMutation } from "hooks";
import { gql, useApolloClient } from "@apollo/client";
Expand Down Expand Up @@ -241,6 +240,10 @@ const TextEntityContent = ({
if (event.code === "Enter" && !event.ctrlKey) {
onEdit();
}

if (event.keyCode === 27) {
setEdit(false);
}
},
[edit, content]
);
Expand Down
55 changes: 52 additions & 3 deletions src/components/LexicalEntryCorp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { queryCounter } from "backend";
import { queryLexicalEntries } from "components/CorporaView";
import { deleteMarkupGroupMutation, refetchLexicalEntries } from "components/JoinMarkupsModal";
import TranslationContext from "Layout/TranslationContext";
import { patienceDiff } from "utils/patienceDiff";
import { compositeIdToString, compositeIdToString as id2str } from "utils/compositeId";

import GroupingTag from "./GroupingTag";
Expand Down Expand Up @@ -401,14 +402,62 @@ const Entities = ({
});
}, [remove_set]);

const update = useCallback((entity, content = entity.content, markups = null) => {
const update = useCallback((entity, content = entity.content, ready_markups = null) => {

const entity_id_str = id2str(entity.id);

const update_set2 = update_set;
update_set2[entity_id_str] = null;
setUpdateSet(update_set2);

const markups = ready_markups || [[]];

if (!ready_markups &&
content !== entity.content &&
entity.additional_metadata?.markups &&
entity.additional_metadata.markups.length > 1) {

const diff = patienceDiff(entity.content, content).lines;

for (const markup of entity.additional_metadata.markups) {
if (!markup.length || markup[0].length !== 2) {
continue;
}

let [[startOffset, endOffset], ...groupIds] = markup;
const startIndex = diff.map(ch => ch.aIndex).indexOf(startOffset);
const endIndex = diff.map(ch => ch.aIndex).indexOf(endOffset);
const markupDiff = diff.slice(0, endIndex);

for (const [i, {aIndex, bIndex}] of markupDiff.entries()) {
if (aIndex === -1) {
if (i < startIndex) {
startOffset++;
}
endOffset++;
}
if (bIndex === -1) {
if (i < startIndex) {
startOffset--;
}
endOffset--;
}
}

if (startOffset < endOffset) {
console.log("=== Markup was moved");
markups.push([[startOffset, endOffset], ...groupIds]);

} else if (groupIds.length > 0) {
console.log("=== Markup with groups were deleted");
deleteMarkupGroup({ variables: { groupIds, perspectiveId } });

} else {
console.log("=== Markup was deleted");
}
}
}

updateEntity({
variables: { id: entity.id, content, markups },
refetchQueries: [
Expand Down Expand Up @@ -495,7 +544,7 @@ const Entities = ({
const secondMarkups = [[]];

for (const markup of markups) {
if (!markup.length || markup[0].length != 2) {
if (!markup.length || markup[0].length !== 2) {
continue;
}

Expand Down Expand Up @@ -598,7 +647,7 @@ const Entities = ({
is_being_removed={remove_set.hasOwnProperty(id2str(entity.id))}
is_being_updated={update_set.hasOwnProperty(id2str(entity.id))}
number={number}
id={entity.id}
id={entity.id}
/>
))}
{mode === "edit" && !is_order_column && (
Expand Down
2 changes: 1 addition & 1 deletion src/utils/patienceDiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*
*/

function patienceDiff( aLines, bLines, diffPlusFlag ) {
export function patienceDiff( aLines, bLines, diffPlusFlag ) {

//
// findUnique finds all unique values in arr[lo..hi], inclusive. This
Expand Down

0 comments on commit 099daf7

Please sign in to comment.