streamich / json-joy

JSON CRDT, JSON CRDT Patch, JSON Patch+, JSON Predicate, JSON Pointer, JSON Expression, JSON Type
https://jsonjoy.com/libs/json-joy-js
Apache License 2.0
751 stars 14 forks source link

Can you provide a method to find the corresponding JsonNode by id: ITimestampStruct? #630

Closed 534254884 closed 3 months ago

534254884 commented 4 months ago

Can you provide a method to find the corresponding JsonNode through id: ITimestampStruct, so as to do some operations such as undo/redo with the information in JsonCrdtPatchOperation?

streamich commented 3 months ago

It is already possible, there is the index object which stores all the nodes by ID:

model.index.get(id);

If you console.log some model, you will also see the index printed:

console.log('' + model);

will output something like this:

model
├─ root 0.0
│  └─ obj 2.1
│     ├─ "id"
│     │   └─ con 2.2 { "xyz" }
│     ├─ "text"
│     │   └─ str 2.3 { "hello" }
│     │      └─ StrChunk 2.4!5 len:5 { "hello" }
│     └─ "temperature"
│         └─ con 2.9 { 36.6 }
│
├─ index (4 nodes)
│  ├─ obj 2.1
│  ├─ con 2.2
│  ├─ str 2.3
│  └─ con 2.9
│
├─ view
│  └─ {
│       "id": "xyz",
│       "text": "hello",
│       "temperature": 36.6
│     }
│
└─ clock 1234.12
   └─ 2.11