Skip to content

Commit

Permalink
feat(core): generate updates
Browse files Browse the repository at this point in the history
  • Loading branch information
pubuzhixing8 committed Oct 7, 2021
1 parent d7c7d73 commit c498d9a
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 9 deletions.
10 changes: 1 addition & 9 deletions demo/app/richtext/richtext.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,7 @@ export class DemoRichtextComponent implements OnInit {
type: 'paragraph',
children: [
{
text: 'PingCode & 2'
}
]
},
{
type: 'paragraph',
children: [
{
text: '2Worktile !'
text: 'PingCode'
}
]
}
Expand Down
36 changes: 36 additions & 0 deletions z/common/apply-update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { ZContentAny } from "z/structs/content-any";
import { ZContentString } from "z/structs/content-string";
import { ZContentType } from "z/structs/content-type";
import { ZItem } from "z/structs/item";
import { ZDoc } from "z/types/doc";
import { ZContentStruct } from "./content-struct";
import { DeleteSet } from "./delete-set";

export function applyUpdates(doc: ZDoc, items: ZItem[], ds: DeleteSet) {

}

export function generateUpdates(doc: ZDoc) {
const items = [];
for (const [client, updates] of doc.store.clients) {
updates.forEach((update: ZItem) => {
let contentData;
if (update.content instanceof ZContentType) {
contentData = update.content.type.getRefID();
}
if (update.content instanceof ZContentString) {
contentData = update.content.getContent();
}
if (update.content instanceof ZContentAny) {
contentData = update.content.getContent();
}
items.push({
id: update.id,
origin: update.origin,
rightOrigin: update.rightOrigin,
content: new ZContentStruct(update.content.getRef(), contentData)
});
});
}
return items;
}
9 changes: 9 additions & 0 deletions z/common/content-struct.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class ZContentStruct {
contentRef: number;
data: any;

constructor(contentRef: number, data: any) {
this.contentRef = contentRef;
this.data = data;
}
}
1 change: 1 addition & 0 deletions z/structs/base-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export abstract class ZBaseContent {
abstract isCountable(): boolean;
abstract splice(offset): ZBaseContent;
abstract delete(transaction);
abstract getRef(): number;
}
4 changes: 4 additions & 0 deletions z/structs/content-any.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ export class ZContentAny extends ZBaseContent {
this.arr = this.arr.slice(0, offset);
return contentAny;
}

getRef() {
return 8;
}
}
4 changes: 4 additions & 0 deletions z/structs/content-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ export class ZContentString extends ZBaseContent {

delete(transaction: any) {
}

getRef() {
return 4;
}
}
8 changes: 8 additions & 0 deletions z/structs/content-type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { ZBaseType } from "z/types/base-type";
import { ZBaseContent } from "./base-content";

export const ZArrayRefID = 0;
export const ZMapRefID = 1;
export const ZTextRefID = 2;

export class ZContentType extends ZBaseContent {
splice(offset: any): ZBaseContent {
throw new Error("Method not implemented.");
Expand Down Expand Up @@ -39,4 +43,8 @@ export class ZContentType extends ZBaseContent {
}
}
}

getRef() {
return 7;
}
}
5 changes: 5 additions & 0 deletions z/types/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ZBaseType } from "./base-type";
import { transact, Transaction } from "../common/transaction";
import { typeListDelete, typeListGet, typeListInsertGenerics } from "z/common/type-list";
import { ZMap } from "./map";
import { ZArrayRefID } from "z/structs/content-type";

export class ZArray extends ZBaseType {
_prelimContent: Array<any> = [];
Expand Down Expand Up @@ -70,5 +71,9 @@ export class ZArray extends ZBaseType {
}
return content;
}

getRefID() {
return ZArrayRefID;
}
}

2 changes: 2 additions & 0 deletions z/types/base-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ export abstract class ZBaseType {
}

abstract toJSON();

abstract getRefID();
}
5 changes: 5 additions & 0 deletions z/types/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ZDoc } from "./doc";
import { ZItem } from "../structs/item";
import { transact, Transaction } from "z/common/transaction";
import { typeMapSet } from "z/common/type-map";
import { ZMapRefID } from "z/structs/content-type";

export class ZMap extends ZBaseType {
_map: Map<string, ZItem>;
Expand Down Expand Up @@ -53,6 +54,10 @@ export class ZMap extends ZBaseType {
}
return obj;
}

getRefID() {
return ZMapRefID;
}
}

function* createZMapEntriesIterator(map: Map<string, any>) {
Expand Down
5 changes: 5 additions & 0 deletions z/types/text.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { transact, Transaction } from "z/common/transaction";
import { typeTextDelete, typeTextInsert } from "z/common/type-text";
import { ZContentString } from "z/structs/content-string";
import { ZTextRefID } from "z/structs/content-type";
import { ZItem } from "z/structs/item";
import { ZBaseType } from "./base-type";
import { ZDoc } from "./doc";
Expand Down Expand Up @@ -66,4 +67,8 @@ export class ZText extends ZBaseType {
toJSON() {
return this.toString();
}

getRefID() {
return ZTextRefID;
}
}

0 comments on commit c498d9a

Please sign in to comment.