Skip to content

Commit

Permalink
handle tour steps id derefencing
Browse files Browse the repository at this point in the history
  • Loading branch information
sdumetz committed Feb 12, 2024
1 parent 6722fb9 commit 351b3ba
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
45 changes: 44 additions & 1 deletion source/server/utils/merge/merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const thisDir = path.dirname(fileURLToPath(import.meta.url));
import { IDocument, INode } from "../schema/document.js";
import {DELETE_KEY, apply, applyDoc, diff, diffDoc} from "./index.js";
import { ISetup } from "../schema/setup.js";
import { DerefSnapshots } from "./pointers/types.js";
import { DerefScene, DerefSnapshots } from "./pointers/types.js";



Expand Down Expand Up @@ -137,6 +137,49 @@ describe("merge documents", function(){
expect(values[1]).to.deep.equal([[0,0,0], [1,1,0], [0,0,0]]);
expect(values[2]).to.deep.equal([[1,1,1], [2,2,2], [1,1,1]]);
});

it("merge added tour steps", function(){
const doc = JSON.parse(docString);
doc.setups[0].tours = [{
"id": "fxQkZ9rUwNAU",
"steps": [
{"id": "gLi0xz", "titles": {"EN": "New Step #0"}},
{"id": "bdh7ob", "titles": {"EN": "New Step #1"}}
]
}];

const current = JSON.parse(docString);
current.setups[0].tours = [{
"id": "fxQkZ9rUwNAU",
"steps": [
{"id": "gLi0xz", "titles": {"EN": "New Step #0"}},
{"id": "bdh7ob", "titles": {"EN": "New Step #1"}},
{"id": "bYMguT", "titles": {"EN": "New Step #2"}}
]
}];

const next = JSON.parse(docString);
next.setups[0].tours = [{
"id": "fxQkZ9rUwNAU",
"steps": [
{"id": "gLi0xz", "titles": {"EN": "New Step #0"}},
]
}];
const d = diffDoc(doc, next);

expect((d?.scene as DerefScene)?.setup?.tours).to.have.property("fxQkZ9rUwNAU");

const result = applyDoc(current, d);
expect(result.setups).to.have.length(1);
const setup = (result.setups as any)[0]
expect(setup, JSON.stringify(setup.tours, null, 2)).to.have.property("tours").to.deep.equal([{
"id": "fxQkZ9rUwNAU",
"steps": [
{"id": "gLi0xz", "titles": {"EN": "New Step #0"}},
{"id": "bYMguT", "titles": {"EN": "New Step #2"}}
]
}]);
})

it("detects a no-op", function(){
const current = JSON.parse(docString);
Expand Down
24 changes: 18 additions & 6 deletions source/server/utils/merge/pointers/setup.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { IDocument } from "../../schema/document.js";
import { ISetup, ITour } from "../../schema/setup.js";
import { ISetup, ITour, ITourStep } from "../../schema/setup.js";
import uid from "../../uid.js";
import { mapTarget, unmapTarget } from "./snapshot.js";
import { DerefNode, DerefSetup, DerefSnapshots, IdMap } from "./types.js";
import { DerefNode, DerefSetup, DerefSnapshots, DerefTour, IdMap } from "./types.js";

export function appendSetup(document :Required<IDocument>, {tours: toursMap, snapshots, ...setup} :DerefSetup) :number{
let iSetup :ISetup = {...setup};

const tours = Object.values(toursMap ?? {});
if(tours.length){
iSetup.tours = tours;
iSetup.tours = tours.map(({steps, ...t})=>{
const tour = t as ITour;
const stepsValues = Object.values(steps);
if(stepsValues.length){
tour.steps = stepsValues;
}
return tour;
});
}

if(snapshots){
const targetStrings = Object.keys(snapshots.targets)
.map(k=> Object.keys(snapshots.targets[k]).map(prop=>`${k}/${prop}`))
Expand All @@ -34,13 +42,17 @@ export function appendSetup(document :Required<IDocument>, {tours: toursMap, sna
export function mapSetup({tours, snapshots, ...iSetup} :ISetup, nodes :DerefNode[]) :DerefSetup {
const setup = {
...iSetup,
tours: (tours?.length ? {} : undefined) as IdMap<ITour & {id:string}>,
tours: (tours?.length ? {} : undefined) as IdMap<DerefTour & {id:string}>,
snapshots: undefined as any as DerefSnapshots,
}

for(let tour of tours??[]){
for(let iTour of tours??[]){
const tour :DerefTour = {...iTour, steps: {}};
tour.id ??= uid();
setup.tours[tour.id] = tour as ITour & {id: string};
for(let step of iTour.steps ?? []){
tour.steps[step.id] = step;
}
setup.tours[tour.id] = tour as DerefTour & {id: string};
}
if(snapshots){
//Nest targets into objects to ease deep merge by node ID
Expand Down
6 changes: 5 additions & 1 deletion source/server/utils/merge/pointers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,12 @@ export interface DerefMeta extends Omit<IMeta, "images"|"articles"|"audio"|"lead
leadArticle?: string;
}

export interface DerefTour extends Omit<ITour, "steps">{
steps: IdMap<ITourStep>;
}

export interface DerefSetup extends Omit<ISetup,"tours"|"snapshots">{
tours?: IdMap<ITour & {id:string}>;
tours?: IdMap<DerefTour & {id:string}>;
snapshots?: DerefSnapshots;
}

Expand Down

0 comments on commit 351b3ba

Please sign in to comment.