From 50b7e77da0ddfd7e9355c49f3c5145b66b733f62 Mon Sep 17 00:00:00 2001 From: Martin Ledvinka Date: Mon, 9 Dec 2024 14:53:55 +0100 Subject: [PATCH] [Enhancement #587] Use backend to generated new occurrence selectors. --- .../annotator/AnnotationDomHelper.ts | 33 ------------------- src/component/annotator/AnnotatorUtil.ts | 9 +++-- .../__tests__/AnnotationDomHelper.test.tsx | 17 ---------- src/model/TermOccurrence.ts | 3 ++ 4 files changed, 9 insertions(+), 53 deletions(-) diff --git a/src/component/annotator/AnnotationDomHelper.ts b/src/component/annotator/AnnotationDomHelper.ts index 040dfe48d..f36936547 100644 --- a/src/component/annotator/AnnotationDomHelper.ts +++ b/src/component/annotator/AnnotationDomHelper.ts @@ -8,7 +8,6 @@ import { import VocabularyUtils from "../../util/VocabularyUtils"; import HtmlParserUtils from "./HtmlParserUtils"; import HtmlDomUtils from "./HtmlDomUtils"; -import { TextQuoteSelector } from "../../model/TermOccurrence"; import Utils from "../../util/Utils"; export const AnnotationType = { @@ -16,8 +15,6 @@ export const AnnotationType = { DEFINITION: VocabularyUtils.DEFINITION, }; -export const SELECTOR_CONTEXT_LENGTH = 32; - function toHtmlString(nodeList: NodeList): string { let result = ""; for (let i = 0; i < nodeList.length; i++) { @@ -120,13 +117,6 @@ const AnnotationDomHelper = { }; }, - replaceAnnotation( - oldAnnotation: DomHandlerNode, - newAnnotation: DomHandlerNode - ): void { - DomUtils.replaceElement(oldAnnotation, newAnnotation); - }, - createNewAnnotation( about: string, nodeList: NodeList, @@ -159,29 +149,6 @@ const AnnotationDomHelper = { } return score <= Number(node.attribs.score); }, - - generateSelector(node: DomHandlerNode): TextQuoteSelector { - let prefix = undefined; - let suffix = undefined; - if (node.previousSibling) { - prefix = HtmlDomUtils.getTextContent(node.previousSibling); - if (prefix.length > SELECTOR_CONTEXT_LENGTH) { - prefix = prefix.substring(prefix.length - SELECTOR_CONTEXT_LENGTH); - } - } - if (node.nextSibling) { - suffix = HtmlDomUtils.getTextContent(node.nextSibling); - if (suffix.length > SELECTOR_CONTEXT_LENGTH) { - suffix = suffix.substring(0, SELECTOR_CONTEXT_LENGTH); - } - } - return { - exactMatch: HtmlDomUtils.getTextContent(node), - prefix, - suffix, - types: [VocabularyUtils.TEXT_QUOTE_SELECTOR], - }; - }, }; export default AnnotationDomHelper; diff --git a/src/component/annotator/AnnotatorUtil.ts b/src/component/annotator/AnnotatorUtil.ts index 4afc92564..ded077e9b 100644 --- a/src/component/annotator/AnnotatorUtil.ts +++ b/src/component/annotator/AnnotatorUtil.ts @@ -3,7 +3,6 @@ import VocabularyUtils, { IRI, IRIImpl } from "../../util/VocabularyUtils"; import JsonLdUtils from "../../util/JsonLdUtils"; import Term from "../../model/Term"; import TermOccurrence from "../../model/TermOccurrence"; -import AnnotationDomHelper from "./AnnotationDomHelper"; const OCCURRENCE_SEPARATOR = "/occurrences"; @@ -22,7 +21,7 @@ export function createTermOccurrence( annotationElement: Element, fileIri: IRI ) { - return new TermOccurrence({ + const to = new TermOccurrence({ iri: annotationIdToTermOccurrenceIri( annotationElement.attribs.about, fileIri @@ -32,9 +31,13 @@ export function createTermOccurrence( source: { iri: fileIri.namespace + fileIri.fragment, }, - selectors: [AnnotationDomHelper.generateSelector(annotationElement)], + selectors: [], types: [VocabularyUtils.FILE_OCCURRENCE_TARGET], }, types: [], }); + to.elementAbout = annotationElement.attribs.about.substring( + JsonLdUtils.BNODE_PREFIX.length + ); + return to; } diff --git a/src/component/annotator/__tests__/AnnotationDomHelper.test.tsx b/src/component/annotator/__tests__/AnnotationDomHelper.test.tsx index 940c7fbc0..53b5290f2 100644 --- a/src/component/annotator/__tests__/AnnotationDomHelper.test.tsx +++ b/src/component/annotator/__tests__/AnnotationDomHelper.test.tsx @@ -290,21 +290,4 @@ describe("AnnotationDomHelper", () => { ); }); }); - - describe("generateSelector", () => { - it("creates TextQuoteSelector from text content of the specified node", () => { - const selector = sut.generateSelector(annotationSpan); - expect(selector).toBeDefined(); - expect(selector.exactMatch).toEqual( - (annotationSpan.children![0] as DataNode).data - ); - }); - - it("uses previous and next siblings to provide selector prefix and suffix", () => { - const selector = sut.generateSelector(annotationSpan); - expect(selector).toBeDefined(); - expect(selector.prefix).toEqual("First paragraph.\n "); - expect(selector.suffix).toEqual("\n "); - }); - }); }); diff --git a/src/model/TermOccurrence.ts b/src/model/TermOccurrence.ts index b11b742ac..fe4f42020 100644 --- a/src/model/TermOccurrence.ts +++ b/src/model/TermOccurrence.ts @@ -15,6 +15,7 @@ const textQuoteSelectorCtx = { exactMatch: VocabularyUtils.NS_TERMIT + "má-přesný-text-quote", prefix: VocabularyUtils.NS_TERMIT + "má-prefix-text-quote", suffix: VocabularyUtils.NS_TERMIT + "má-suffix-text-quote", + elementAbout: VocabularyUtils.DC_IDENTIFIER, }; /** @@ -56,6 +57,8 @@ export interface TermOccurrenceData extends TermAssignmentData { export default class TermOccurrence extends TermAssignment { public target: OccurrenceTarget; + public elementAbout?: string; + constructor(data: TermOccurrenceData) { super(data); this.target = data.target;