Skip to content

Commit

Permalink
Merge branch 'main' into recogito#127-annotating-enabled-reactive
Browse files Browse the repository at this point in the history
# Conflicts:
#	packages/text-annotator/src/SelectionHandler.ts
  • Loading branch information
oleksandr-danylchenko committed Nov 4, 2024
2 parents 8084534 + d9d3819 commit b3e9a54
Show file tree
Hide file tree
Showing 18 changed files with 642 additions and 45,650 deletions.
1,214 changes: 518 additions & 696 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@recogito/text-annotator-monorepo",
"version": "3.0.0-rc.49",
"version": "3.0.0-rc.50",
"description": "Recogito Text Annotator monorepo",
"author": "Rainer Simon",
"repository": {
Expand Down
12 changes: 6 additions & 6 deletions packages/extension-tei/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@recogito/text-annotator-tei",
"version": "3.0.0-rc.49",
"version": "3.0.0-rc.50",
"description": "Recogito Text Annotator TEI extension",
"author": "Rainer Simon",
"license": "BSD-3-Clause",
Expand All @@ -27,12 +27,12 @@
},
"devDependencies": {
"CETEIcean": "^1.9.3",
"typescript": "5.6.2",
"vite": "^5.4.8",
"vite-plugin-dts": "^4.2.3"
"typescript": "5.6.3",
"vite": "^5.4.10",
"vite-plugin-dts": "^4.3.0"
},
"peerDependencies": {
"@annotorious/core": "^3.0.9",
"@recogito/text-annotator": "3.0.0-rc.49"
"@annotorious/core": "^3.0.11",
"@recogito/text-annotator": "3.0.0-rc.50"
}
}
20 changes: 17 additions & 3 deletions packages/extension-tei/src/crosswalk/forward.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { rangeToSelector, reviveTarget as reviveTextOffsetTarget } from '@recogito/text-annotator';
import type { TEIAnnotation, TEIAnnotationTarget, TEIRangeSelector } from '../TEIAnnotation';
import { reanchor } from './utils';
import type {
TextAnnotation,
TextAnnotationTarget,
TextSelector
} from '@recogito/text-annotator';
import type { TEIAnnotation, TEIAnnotationTarget, TEIRangeSelector } from '../TEIAnnotation';

/**
* Helper: Returns the given XPath for a DOM node, in the form of
Expand Down Expand Up @@ -145,8 +146,21 @@ export const reviveTarget = (t: TextAnnotationTarget, container: HTMLElement) =>
const [endNode, endOffset] = evaluateSelector(endExpression);

const range = document.createRange();
range.setStart(startNode.firstChild, startOffset);
range.setEnd(endNode.firstChild, endOffset);

// Helper
const reanchorIfNeeded = (parent: Node, offset: number) => {
if (parent.firstChild.toString().length >= offset) {
return { node: parent.firstChild, offset };
} else {
return reanchor(parent.firstChild, parent, offset);
}
}

const reanchoredStart = reanchorIfNeeded(startNode, startOffset);
const reanchoredEnd = reanchorIfNeeded(endNode, endOffset);

range.setStart(reanchoredStart.node, reanchoredStart.offset);
range.setEnd(reanchoredEnd.node, reanchoredEnd.offset);

const textSelector = rangeToSelector(range, container);

Expand Down
30 changes: 2 additions & 28 deletions packages/extension-tei/src/crosswalk/reverse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TextAnnotation, TextAnnotationTarget } from '@recogito/text-annotator';
import type { TEIAnnotation, TEIAnnotationTarget, TEIRangeSelector } from '../TEIAnnotation';
import { reanchor } from './utils';

/**
* Helper: converts the given XPath string and DOM container element
Expand All @@ -24,35 +25,8 @@ const xpathToDOMPosition = (path: string, container: Element) => {

const offset = parseInt(path.substring(offsetIdx + 2));

const reanchor = (originalNode: Node, originalOffset: number) => {
let node = originalNode;

let offset = originalOffset;

const it = document.createNodeIterator(parentNode, NodeFilter.SHOW_TEXT);

let currentNode = it.nextNode();

let run = true;

do {
if (currentNode instanceof Text) {
if (currentNode.length < offset) {
offset -= currentNode.length;
} else {
node = currentNode;
run = false;
}
}

currentNode = it.nextNode();
} while (currentNode && run);

return { node, offset };
};

if (!(node instanceof Text) || offset > node.length) {
return reanchor(node, offset);
return reanchor(node, parentNode, offset);
} else {
return { node, offset };
}
Expand Down
1 change: 1 addition & 0 deletions packages/extension-tei/src/crosswalk/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './reanchor';
26 changes: 26 additions & 0 deletions packages/extension-tei/src/crosswalk/utils/reanchor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const reanchor = (originalNode: Node, parentNode: Node, originalOffset: number) => {
let node = originalNode;

let offset = originalOffset;

const it = document.createNodeIterator(parentNode, NodeFilter.SHOW_TEXT);

let currentNode = it.nextNode();

let run = true;

do {
if (currentNode instanceof Text) {
if (currentNode.length < offset) {
offset -= currentNode.length;
} else {
node = currentNode;
run = false;
}
}

currentNode = it.nextNode();
} while (currentNode && run);

return { node, offset };
};
102 changes: 35 additions & 67 deletions packages/extension-tei/test/annotations.json
Original file line number Diff line number Diff line change
@@ -1,72 +1,40 @@
[{
"id": "5430edac-706b-4943-9b87-fd6e8f281b39",
"bodies": [],
"id": "uid-5be9c1d9-a92f-47b9-aa95-acdc05cd6268",
"layer_id": "tei_standoff",
"target": {
"annotation": "5430edac-706b-4943-9b87-fd6e8f281b39",
"selector": [{
"startSelector": {
"type": "XPathSelector",
"value": "//castItem[@xml:id='WITCHES_Mac']::13"
"annotation": "uid-5be9c1d9-a92f-47b9-aa95-acdc05cd6268",
"creator": {
"id": "uid-af5d65e0-a46f-4eb9-80f6-11ba5e907485",
"name": "\n Professor\n "
},
"endSelector": {
"type": "XPathSelector",
"value": "//castItem[@xml:id='Ross_Mac']/role[1]/name[1]::4"
}
}],
"creator": {
"isGuest": true,
"id": "DwQPXeG3uLHfsSLnvTV8"
},
"created": "2023-09-21T05:47:51.594Z"
}
}, {
"id": "fa049492-020b-4eaf-ba93-3d595e9d624f",
"bodies": [],
"target": {
"annotation": "fa049492-020b-4eaf-ba93-3d595e9d624f",
"selector": [{
"start": 6464,
"startSelector": {
"type": "XPathSelector",
"value": "//w[@xml:id='fs-mac-0000010']::0"
"created": "2024-10-30T17:48:12.149Z",
"updatedBy": {
"id": "uid-af5d65e0-a46f-4eb9-80f6-11ba5e907485",
"name": "\n Professor\n "
},
"end": 6828,
"endSelector": {
"type": "XPathSelector",
"value": "//pc[@xml:id='fs-mac-0000190']::1"
},
"quote": "ACT 1 Scene 1 Thunder and Lightning . Enter three Witches .",
"range": {}
}],
"creator": {
"isGuest": true,
"id": "jzgy0s50ZDZBGakEHfTU"
},
"created": "2023-09-21T05:29:04.847Z"
}
},{
"id": "091078e1-4073-4efc-a82a-bee79774dbf0",
"bodies": [],
"target": {
"annotation": "091078e1-4073-4efc-a82a-bee79774dbf0",
"selector": [{
"start": 3671,
"startSelector": {
"type": "XPathSelector",
"value": "//castItem[@xml:id='Malcolm_Mac']/role[1]/name[1]::4"
},
"end": 4578,
"endSelector": {
"type": "XPathSelector",
"value": "//castItem[@xml:id='Banquo_Mac']/roleDesc[1]::39"
},
"quote": "olm his elder son Donalbain Duncan’s younger son Macbeth thane of Glamis Lady Macbeth Seyton attendant to Macbeth Three Murderers in Macbeth’s service both attending upon Lady Macbeth A Doctor A Gentlewoman A Porter Banquo commander, with Macbeth, of Duncan’s ar",
"range": {}
}],
"creator": {
"isGuest": true,
"id": "2G7YULxo_qVPC2wCYxfW"
},
"created": "2023-09-21T05:47:24.687Z"
}
"updated": "2024-10-30T17:48:08.235Z",
"selector": [
{
"startSelector": {
"type": "XPathSelector",
"value": "//text[@xml:id='text-1']/body[1]/div[1]/p[4]/hi[1]::0"
},
"endSelector": {
"type": "XPathSelector",
"value": "//text[@xml:id='text-1']/body[1]/div[1]/p[4]::383"
}
}
]
},
"bodies": [
{
"id": "479229dc-fc78-4a6d-8179-0ab58ffe89be",
"annotation": "uid-5be9c1d9-a92f-47b9-aa95-acdc05cd6268",
"value": "Very cool comment",
"creator": {
"id": "uid-af5d65e0-a46f-4eb9-80f6-11ba5e907485",
"name": "\n Professor\n "
}
}
]
}]
2 changes: 1 addition & 1 deletion packages/extension-tei/test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
window.onload = async function () {
var CETEIcean = new CETEI();

CETEIcean.getHTML5('macbeth.xml', data => {
CETEIcean.getHTML5('paradise-lost.xml', data => {
document.getElementById('content').appendChild(data);

var anno = TEIPlugin(createTextAnnotator(document.getElementById('content')));
Expand Down
Loading

0 comments on commit b3e9a54

Please sign in to comment.