Skip to content

Commit

Permalink
Merge pull request #8 from luccalb/1-avoid-empty-decorations
Browse files Browse the repository at this point in the history
#1 - Avoid empty decorations
  • Loading branch information
luccalb authored Sep 4, 2023
2 parents 7404fe5 + d2a20f4 commit 0e9d11b
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 10 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/default.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Build and test

on:
push:
branches: ['**']
pull_request:
branches: [master]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
- run: npm ci
- run: npm run build
- run: npm test
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "tiptap-annotation-magic",
"author": "Lucca Baumgärtner <[email protected]>",
"license": "MIT",
"version": "2.0.0",
"version": "2.0.1",
"main": "build/index.js",
"types": "build/index.d.ts",
"scripts": {
Expand Down
98 changes: 97 additions & 1 deletion src/extension/rendering/engine.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe("Test conflicting annotation", () => {
});
});

describe("Test sorting of annotaiton list", () => {
describe("Test sorting of annotation list", () => {
const testList: Annotation<any>[] = [
{
id: "abc",
Expand Down Expand Up @@ -373,6 +373,102 @@ describe("Mapping the annotation list to a flat representation", () => {
},
],
},
{
name: "Exact overlap - from",
inputAnnotations: [
{
id: "abc",
from: 0,
to: 8,
displayName: "Y",
tag: "Y",
},
{
id: "abc",
from: 0,
to: 5,
displayName: "X",
tag: "X",
},
{
id: "abc",
from: 10,
to: 15,
displayName: "Z",
tag: "Z",
},
],
flatMapping: [
{
id: "abc",
from: 0,
to: 5,
displayName: "X",
tag: "X",
rendering: "normal",
},
{
id: "abc",
from: 5,
to: 8,
displayName: "Y",
tag: "Y",
rendering: "fragment-right",
},
{
id: "abc",
from: 10,
to: 15,
displayName: "Z",
tag: "Z",
rendering: "normal",
},
],
},
{
name: "Exact overlap - from and to",
inputAnnotations: [
{
id: "abc",
from: 0,
to: 8,
displayName: "X",
tag: "X",
},
{
id: "abc",
from: 0,
to: 8,
displayName: "Y",
tag: "Y",
},
{
id: "abc",
from: 10,
to: 15,
displayName: "Z",
tag: "Z",
},
],
flatMapping: [
{
id: "abc",
from: 0,
to: 8,
displayName: "Y",
tag: "Y",
rendering: "normal",
},
{
id: "abc",
from: 10,
to: 15,
displayName: "Z",
tag: "Z",
rendering: "normal",
},
],
},
];
testCases.forEach((testCase) => {
test(
Expand Down
13 changes: 7 additions & 6 deletions src/extension/rendering/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ export const createAnnotationRendering = (
): AnnotationFragment<any>[] => {
const renderedAnnotations: AnnotationFragment<any>[] = [];
const openAnnotationStack: ActionKeyframe[] = [];
//const actionMap: Map<number, ActionKeyframe[]> = new Map();
const actionMap: ActionKeyframe[][] = [];
const annotationFragmentation: boolean[] = [];

// annotations = sortAnnotationsByStart(annotations);

// STEP 1: Create a Map, containing the rendering actions for each index in the document.
// this could be opening or closing an annotation
annotations.forEach((term, index) => {
Expand Down Expand Up @@ -87,7 +84,9 @@ export const createAnnotationRendering = (
from,
rendering,
};
renderedAnnotations.push(normalTerm);
if (normalTerm.from < normalTerm.to) {
renderedAnnotations.push(normalTerm);
}
} else if (
actionStackPeek.action === "open" &&
action.action === "close"
Expand Down Expand Up @@ -131,11 +130,13 @@ export const createAnnotationRendering = (
rendering: "fragment-left",
to: annotations[action.annotationIndex].from,
};
// mark the previous annotation as fragmented, by saving where the fragment ends
// mark the previous annotation as fragmented
annotationFragmentation[actionStackPeek.annotationIndex] = true;
}

renderedAnnotations.push(fragment);
if (fragment.from < fragment.to) {
renderedAnnotations.push(fragment);
}
openAnnotationStack.push(action);
}
} else if (action.action === "open") {
Expand Down

0 comments on commit 0e9d11b

Please sign in to comment.