document-model 1.0.15 → 1.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/dist/browser/document/actions/creators.d.ts +1 -1
  2. package/dist/browser/document/actions/index.d.ts +8 -8
  3. package/dist/browser/document/object.d.ts +123 -99
  4. package/dist/browser/document/reducer.d.ts +1 -1
  5. package/dist/browser/document/schema/types.d.ts +5 -0
  6. package/dist/browser/document/schema/zod.d.ts +39 -0
  7. package/dist/browser/document/types.d.ts +41 -31
  8. package/dist/browser/document/utils/base.d.ts +6 -6
  9. package/dist/browser/document/utils/file.d.ts +2 -2
  10. package/dist/browser/document-model/gen/object.d.ts +1 -1
  11. package/dist/browser/document-model/gen/reducer.d.ts +1 -1
  12. package/dist/browser/document-model/gen/schema/types.d.ts +13 -3
  13. package/dist/browser/document-model/gen/schema/zod.d.ts +20 -1
  14. package/dist/browser/document-model/index.d.ts +9 -9
  15. package/dist/browser/document-model.cjs +1 -1
  16. package/dist/browser/document-model.js +2 -2
  17. package/dist/browser/document.cjs +1 -1
  18. package/dist/browser/document.js +2 -2
  19. package/dist/browser/index.cjs +1 -1
  20. package/dist/browser/index.js +3 -3
  21. package/dist/browser/internal/index-01030c3f.js +22 -0
  22. package/dist/{node/internal/index-c9d178be.js → browser/internal/index-2aee8ea9.js} +1 -1
  23. package/dist/browser/internal/{index-8142b58e.js → index-5f1909ad.js} +513 -435
  24. package/dist/{node/internal/index-0b0cb8c2.js → browser/internal/index-a1282d5c.js} +1 -1
  25. package/dist/browser/internal/object-24df9d92.js +6 -0
  26. package/dist/browser/internal/{object-4ec03a36.js → object-7e8eaff4.js} +777 -731
  27. package/dist/node/document/actions/creators.d.ts +1 -1
  28. package/dist/node/document/actions/index.d.ts +8 -8
  29. package/dist/node/document/object.d.ts +123 -99
  30. package/dist/node/document/reducer.d.ts +1 -1
  31. package/dist/node/document/schema/types.d.ts +5 -0
  32. package/dist/node/document/schema/zod.d.ts +39 -0
  33. package/dist/node/document/types.d.ts +41 -31
  34. package/dist/node/document/utils/base.d.ts +6 -6
  35. package/dist/node/document/utils/file.d.ts +2 -2
  36. package/dist/node/document-model/gen/object.d.ts +1 -1
  37. package/dist/node/document-model/gen/reducer.d.ts +1 -1
  38. package/dist/node/document-model/gen/schema/types.d.ts +13 -3
  39. package/dist/node/document-model/gen/schema/zod.d.ts +20 -1
  40. package/dist/node/document-model/index.d.ts +9 -9
  41. package/dist/node/document-model.cjs +1 -1
  42. package/dist/node/document-model.js +2 -2
  43. package/dist/node/document.cjs +1 -1
  44. package/dist/node/document.js +2 -2
  45. package/dist/node/index.cjs +1 -1
  46. package/dist/node/index.js +3 -3
  47. package/dist/node/internal/{index-0c87fbed.js → index-0db636da.js} +513 -435
  48. package/dist/{browser/internal/index-cb990484.js → node/internal/index-2d4783d9.js} +1 -1
  49. package/dist/{browser/internal/index-9f1cf811.js → node/internal/index-5ef0c6b0.js} +1 -1
  50. package/dist/node/internal/index-cddc9dfd.js +22 -0
  51. package/dist/node/internal/object-21f3d2ee.js +1 -0
  52. package/dist/node/internal/{object-89d9b82f.js → object-c32ea0a0.js} +400 -356
  53. package/package.json +3 -3
  54. package/dist/browser/internal/index-cbe39f72.js +0 -22
  55. package/dist/browser/internal/object-5f03095e.js +0 -6
  56. package/dist/node/internal/index-63db4708.js +0 -22
  57. package/dist/node/internal/object-397a9b23.js +0 -1
@@ -43,4 +43,4 @@ export declare const prune: (start?: number | undefined, end?: number | undefine
43
43
  * @param operations - Number of operations that were removed from the previous state.
44
44
  * @category Actions
45
45
  */
46
- export declare const loadState: (state: Pick<ExtendedState, 'state' | 'name'>, operations: number) => LoadStateAction;
46
+ export declare const loadState: <S, T>(state: Pick<ExtendedState<S, T>, "name" | "state">, operations: number) => LoadStateAction;
@@ -1,10 +1,10 @@
1
- import { Action, Document, ImmutableStateReducer } from '../types';
2
- export declare function setNameOperation<T, A extends Action>(document: Document<T, A>, name: string): Document<T, A>;
3
- export declare function undoOperation<T, A extends Action>(document: Document<T, A>, count: number, wrappedReducer: ImmutableStateReducer<T, A>): Document<T, A>;
4
- export declare function redoOperation<T, A extends Action>(document: Document<T, A>, count: number, wrappedReducer: ImmutableStateReducer<T, A>): Document<T, A>;
5
- export declare function pruneOperation<T, A extends Action>(document: Document<T, A>, start: number | null | undefined, end: number | null | undefined, wrappedReducer: ImmutableStateReducer<T, A>): Document<T, A>;
6
- export declare function loadStateOperation<T, A extends Action>(oldDocument: Document<T, A>, newDocument: {
1
+ import { Action, Document, ImmutableStateReducer, State } from '../types';
2
+ export declare function setNameOperation<T>(document: T, name: string): T;
3
+ export declare function undoOperation<T, A extends Action, L>(document: Document<T, A, L>, count: number, wrappedReducer: ImmutableStateReducer<T, A, L>): Document<T, A, L>;
4
+ export declare function redoOperation<T, A extends Action, L>(document: Document<T, A, L>, count: number, wrappedReducer: ImmutableStateReducer<T, A, L>): Document<T, A, L>;
5
+ export declare function pruneOperation<T, A extends Action, L>(document: Document<T, A, L>, start: number | null | undefined, end: number | null | undefined, wrappedReducer: ImmutableStateReducer<T, A, L>): Document<T, A, L>;
6
+ export declare function loadStateOperation<T, A extends Action, L>(oldDocument: Document<T, A, L>, newDocument: {
7
7
  name: string;
8
- state?: T;
9
- }): Document<T, A>;
8
+ state?: State<T, L>;
9
+ }): Document<T, A, L>;
10
10
  export * from './creators';
@@ -7,8 +7,8 @@ import type { Action, AttachmentRef, Document, ExtendedState, Reducer } from './
7
7
  * @typeparam T - The type of data stored in the document.
8
8
  * @typeparam A - The type of action the document can take.
9
9
  */
10
- export declare abstract class BaseDocument<T, A extends Action> {
11
- protected _document: Document<T, A>;
10
+ export declare abstract class BaseDocument<T, A extends Action, L = unknown> {
11
+ protected _document: Document<T, A, L>;
12
12
  private _reducer;
13
13
  private _signalDispatch?;
14
14
  /**
@@ -16,7 +16,7 @@ export declare abstract class BaseDocument<T, A extends Action> {
16
16
  * @param reducer - The reducer function that updates the state.
17
17
  * @param document - The initial state of the document.
18
18
  */
19
- constructor(reducer: Reducer<T, A>, document: Document<T, A>, signalDispatch?: SignalDispatch);
19
+ constructor(reducer: Reducer<T, A, L>, document: Document<T, A, L>, signalDispatch?: SignalDispatch);
20
20
  /**
21
21
  * Dispatches an action to update the state of the document.
22
22
  * @param action - The action to dispatch.
@@ -41,60 +41,69 @@ export declare abstract class BaseDocument<T, A extends Action> {
41
41
  * @param reducer - The reducer function that updates the state.
42
42
  * @returns The state of the document.
43
43
  */
44
- protected static stateFromFile<T, A extends Action>(path: string, reducer: Reducer<T, A>): Promise<Document<T, A>>;
44
+ protected static stateFromFile<T, A extends Action, L>(path: string, reducer: Reducer<T, A, L>): Promise<Document<T, A, L>>;
45
45
  /**
46
46
  * Gets the current state of the document.
47
47
  */
48
- get state(): import("immer").Immutable<T>;
48
+ get state(): {
49
+ readonly global: import("immer").Immutable<T>;
50
+ readonly local: import("immer").Immutable<L>;
51
+ };
49
52
  /**
50
53
  * Gets the list of operations performed on the document.
51
54
  */
52
- get operations(): readonly ({
53
- readonly input: {
54
- readonly operations: number;
55
- readonly state: {
56
- readonly data?: unknown;
57
- readonly name: string;
55
+ get operations(): {
56
+ readonly global: readonly ({
57
+ readonly input: {
58
+ readonly operations: number;
59
+ readonly state: {
60
+ readonly data?: unknown;
61
+ readonly name: string;
62
+ };
58
63
  };
59
- };
60
- readonly type: "LOAD_STATE";
61
- readonly index: number;
62
- readonly timestamp: string;
63
- readonly hash: string;
64
- } | {
65
- readonly input: {
66
- readonly end?: import("./types").InputMaybe<number>;
67
- readonly start?: import("./types").InputMaybe<number>;
68
- };
69
- readonly type: "PRUNE";
70
- readonly index: number;
71
- readonly timestamp: string;
72
- readonly hash: string;
73
- } | {
74
- readonly input: number;
75
- readonly type: "REDO";
76
- readonly index: number;
77
- readonly timestamp: string;
78
- readonly hash: string;
79
- } | {
80
- readonly input: string;
81
- readonly type: "SET_NAME";
82
- readonly index: number;
83
- readonly timestamp: string;
84
- readonly hash: string;
85
- } | {
86
- readonly input: number;
87
- readonly type: "UNDO";
88
- readonly index: number;
89
- readonly timestamp: string;
90
- readonly hash: string;
91
- } | import("immer").Immutable<A & {
92
- index: number;
93
- timestamp: string;
94
- hash: string; /**
95
- * Gets the timestamp of the date the document was created.
96
- */
97
- }>)[];
64
+ readonly type: "LOAD_STATE";
65
+ readonly scope: "global";
66
+ readonly index: number;
67
+ readonly timestamp: string;
68
+ readonly hash: string;
69
+ } | {
70
+ readonly input: {
71
+ readonly end?: import("./types").InputMaybe<number>;
72
+ readonly start?: import("./types").InputMaybe<number>;
73
+ };
74
+ readonly type: "PRUNE";
75
+ readonly scope: "global";
76
+ readonly index: number;
77
+ readonly timestamp: string;
78
+ readonly hash: string;
79
+ } | {
80
+ readonly input: number;
81
+ readonly type: "REDO";
82
+ readonly scope: "global";
83
+ readonly index: number;
84
+ readonly timestamp: string;
85
+ readonly hash: string;
86
+ } | {
87
+ readonly input: string;
88
+ readonly type: "SET_NAME";
89
+ readonly scope: "global";
90
+ readonly index: number;
91
+ readonly timestamp: string;
92
+ readonly hash: string;
93
+ } | {
94
+ readonly input: number;
95
+ readonly type: "UNDO";
96
+ readonly scope: "global";
97
+ readonly index: number;
98
+ readonly timestamp: string;
99
+ readonly hash: string;
100
+ } | import("immer").Immutable<A & {
101
+ index: number;
102
+ timestamp: string;
103
+ hash: string;
104
+ }>)[];
105
+ readonly local: readonly import("immer").Immutable<import("./types").Operation<A>>[];
106
+ };
98
107
  /**
99
108
  * Gets the name of the document.
100
109
  */
@@ -124,7 +133,10 @@ export declare abstract class BaseDocument<T, A extends Action> {
124
133
  readonly documentType: string;
125
134
  readonly created: string;
126
135
  readonly lastModified: string;
127
- readonly state: import("immer").Immutable<T>;
136
+ readonly state: {
137
+ readonly global: import("immer").Immutable<T>;
138
+ readonly local: import("immer").Immutable<L>;
139
+ };
128
140
  readonly attachments: {
129
141
  readonly [x: string]: {
130
142
  readonly data: string;
@@ -143,7 +155,10 @@ export declare abstract class BaseDocument<T, A extends Action> {
143
155
  readonly documentType: string;
144
156
  readonly created: string;
145
157
  readonly lastModified: string;
146
- readonly state: import("immer").Immutable<T>;
158
+ readonly state: {
159
+ readonly global: import("immer").Immutable<T>;
160
+ readonly local: import("immer").Immutable<L>;
161
+ };
147
162
  readonly attachments: {
148
163
  readonly [x: string]: {
149
164
  readonly data: string;
@@ -152,59 +167,68 @@ export declare abstract class BaseDocument<T, A extends Action> {
152
167
  readonly fileName?: string | null | undefined;
153
168
  };
154
169
  };
155
- readonly operations: readonly ({
156
- readonly input: {
157
- readonly operations: number;
158
- readonly state: {
159
- readonly data?: unknown;
160
- readonly name: string;
170
+ readonly operations: {
171
+ readonly global: readonly ({
172
+ readonly input: {
173
+ readonly operations: number;
174
+ readonly state: {
175
+ readonly data?: unknown;
176
+ readonly name: string;
177
+ };
161
178
  };
162
- };
163
- readonly type: "LOAD_STATE";
164
- readonly index: number;
165
- readonly timestamp: string;
166
- readonly hash: string;
167
- } | {
168
- readonly input: {
169
- readonly end?: import("./types").InputMaybe<number>;
170
- readonly start?: import("./types").InputMaybe<number>;
171
- };
172
- readonly type: "PRUNE";
173
- readonly index: number;
174
- readonly timestamp: string;
175
- readonly hash: string;
176
- } | {
177
- readonly input: number;
178
- readonly type: "REDO";
179
- readonly index: number;
180
- readonly timestamp: string;
181
- readonly hash: string;
182
- } | {
183
- readonly input: string;
184
- readonly type: "SET_NAME";
185
- readonly index: number;
186
- readonly timestamp: string;
187
- readonly hash: string;
188
- } | {
189
- readonly input: number;
190
- readonly type: "UNDO";
191
- readonly index: number;
192
- readonly timestamp: string;
193
- readonly hash: string;
194
- } | import("immer").Immutable<A & {
195
- index: number;
196
- timestamp: string;
197
- hash: string; /**
198
- * Gets the timestamp of the date the document was created.
199
- */
200
- }>)[];
179
+ readonly type: "LOAD_STATE";
180
+ readonly scope: "global";
181
+ readonly index: number;
182
+ readonly timestamp: string;
183
+ readonly hash: string;
184
+ } | {
185
+ readonly input: {
186
+ readonly end?: import("./types").InputMaybe<number>;
187
+ readonly start?: import("./types").InputMaybe<number>;
188
+ };
189
+ readonly type: "PRUNE";
190
+ readonly scope: "global";
191
+ readonly index: number;
192
+ readonly timestamp: string;
193
+ readonly hash: string;
194
+ } | {
195
+ readonly input: number;
196
+ readonly type: "REDO";
197
+ readonly scope: "global";
198
+ readonly index: number;
199
+ readonly timestamp: string;
200
+ readonly hash: string;
201
+ } | {
202
+ readonly input: string;
203
+ readonly type: "SET_NAME";
204
+ readonly scope: "global";
205
+ readonly index: number;
206
+ readonly timestamp: string;
207
+ readonly hash: string;
208
+ } | {
209
+ readonly input: number;
210
+ readonly type: "UNDO";
211
+ readonly scope: "global";
212
+ readonly index: number;
213
+ readonly timestamp: string;
214
+ readonly hash: string;
215
+ } | import("immer").Immutable<A & {
216
+ index: number;
217
+ timestamp: string;
218
+ hash: string;
219
+ }>)[];
220
+ readonly local: readonly import("immer").Immutable<import("./types").Operation<A>>[];
221
+ };
201
222
  readonly initialState: {
202
223
  readonly name: string;
203
224
  readonly revision: number;
204
225
  readonly documentType: string;
205
226
  readonly created: string;
206
227
  readonly lastModified: string;
207
- readonly state: import("immer").Immutable<T>;
228
+ readonly state: {
229
+ readonly global: import("immer").Immutable<T>;
230
+ readonly local: import("immer").Immutable<L>;
231
+ };
208
232
  readonly attachments: {
209
233
  readonly [x: string]: {
210
234
  readonly data: string;
@@ -246,7 +270,7 @@ export declare abstract class BaseDocument<T, A extends Action> {
246
270
  * @param state - The state to load.
247
271
  * @param operations - The operations to apply to the document.
248
272
  */
249
- loadState(state: Pick<ExtendedState<T>, 'state' | 'name'>, operations: number): this;
273
+ loadState(state: Pick<ExtendedState<T, L>, 'state' | 'name'>, operations: number): this;
250
274
  }
251
275
  /**
252
276
  * Applies multiple mixins to a base class.
@@ -13,4 +13,4 @@ import { SignalDispatch } from './signal';
13
13
  * specific to the document's state.
14
14
  * @returns The new state of the document.
15
15
  */
16
- export declare function baseReducer<T, A extends Action>(document: Document<T, A>, action: A | BaseAction, customReducer: ImmutableStateReducer<T, A>, dispatch?: SignalDispatch): Document<T, A>;
16
+ export declare function baseReducer<T, A extends Action, L>(document: Document<T, A, L>, action: A | BaseAction, customReducer: ImmutableStateReducer<T, A, L>, dispatch?: SignalDispatch): Document<T, A, L>;
@@ -91,6 +91,7 @@ export type Load_State = 'LOAD_STATE';
91
91
  export type LoadStateAction = {
92
92
  input: LoadStateActionInput;
93
93
  type: Load_State | `${Load_State}`;
94
+ scope: "global";
94
95
  };
95
96
  export type LoadStateActionInput = {
96
97
  operations: Scalars['Int']['input'];
@@ -134,6 +135,7 @@ export type Prune = 'PRUNE';
134
135
  export type PruneAction = {
135
136
  input: PruneActionInput;
136
137
  type: Prune | `${Prune}`;
138
+ scope: "global";
137
139
  };
138
140
  export type PruneActionInput = {
139
141
  end?: InputMaybe<Scalars['Int']['input']>;
@@ -147,11 +149,13 @@ export type Redo = 'REDO';
147
149
  export type RedoAction = {
148
150
  input: Scalars['Int']['input'];
149
151
  type: Redo | `${Redo}`;
152
+ scope: "global";
150
153
  };
151
154
  export type Set_Name = 'SET_NAME';
152
155
  export type SetNameAction = {
153
156
  input: Scalars['String']['input'];
154
157
  type: Set_Name | `${Set_Name}`;
158
+ scope: "global";
155
159
  };
156
160
  export type SetNameOperation = IOperation & {
157
161
  __typename?: 'SetNameOperation';
@@ -165,4 +169,5 @@ export type Undo = 'UNDO';
165
169
  export type UndoAction = {
166
170
  input: Scalars['Int']['input'];
167
171
  type: Undo | `${Undo}`;
172
+ scope: "global";
168
173
  };
@@ -15,48 +15,63 @@ export declare function ActionSchema(): z.ZodObject<Properties<Action>>;
15
15
  export declare function BaseActionSchema(): z.ZodUnion<[z.ZodObject<Required<{
16
16
  input: z.ZodType<LoadStateActionInput, any, LoadStateActionInput>;
17
17
  type: z.ZodType<"LOAD_STATE", any, "LOAD_STATE">;
18
+ scope: z.ZodType<"global", any, "global">;
18
19
  }>, z.UnknownKeysParam, z.ZodTypeAny, {
19
20
  input: LoadStateActionInput;
20
21
  type: "LOAD_STATE";
22
+ scope: "global";
21
23
  }, {
22
24
  input: LoadStateActionInput;
23
25
  type: "LOAD_STATE";
26
+ scope: "global";
24
27
  }>, z.ZodObject<Required<{
25
28
  input: z.ZodType<PruneActionInput, any, PruneActionInput>;
26
29
  type: z.ZodType<"PRUNE", any, "PRUNE">;
30
+ scope: z.ZodType<"global", any, "global">;
27
31
  }>, z.UnknownKeysParam, z.ZodTypeAny, {
28
32
  input: PruneActionInput;
29
33
  type: "PRUNE";
34
+ scope: "global";
30
35
  }, {
31
36
  input: PruneActionInput;
32
37
  type: "PRUNE";
38
+ scope: "global";
33
39
  }>, z.ZodObject<Required<{
34
40
  input: z.ZodType<number, any, number>;
35
41
  type: z.ZodType<"REDO", any, "REDO">;
42
+ scope: z.ZodType<"global", any, "global">;
36
43
  }>, z.UnknownKeysParam, z.ZodTypeAny, {
37
44
  input: number;
38
45
  type: "REDO";
46
+ scope: "global";
39
47
  }, {
40
48
  input: number;
41
49
  type: "REDO";
50
+ scope: "global";
42
51
  }>, z.ZodObject<Required<{
43
52
  input: z.ZodType<string, any, string>;
44
53
  type: z.ZodType<"SET_NAME", any, "SET_NAME">;
54
+ scope: z.ZodType<"global", any, "global">;
45
55
  }>, z.UnknownKeysParam, z.ZodTypeAny, {
46
56
  input: string;
47
57
  type: "SET_NAME";
58
+ scope: "global";
48
59
  }, {
49
60
  input: string;
50
61
  type: "SET_NAME";
62
+ scope: "global";
51
63
  }>, z.ZodObject<Required<{
52
64
  input: z.ZodType<number, any, number>;
53
65
  type: z.ZodType<"UNDO", any, "UNDO">;
66
+ scope: z.ZodType<"global", any, "global">;
54
67
  }>, z.UnknownKeysParam, z.ZodTypeAny, {
55
68
  input: number;
56
69
  type: "UNDO";
70
+ scope: "global";
57
71
  }, {
58
72
  input: number;
59
73
  type: "UNDO";
74
+ scope: "global";
60
75
  }>]>;
61
76
  export declare function DocumentFileSchema(): z.ZodObject<Properties<DocumentFile>>;
62
77
  export declare function LoadStateActionSchema(): z.ZodObject<Properties<LoadStateAction>>;
@@ -65,8 +80,32 @@ export declare function LoadStateActionStateInputSchema(): z.ZodObject<Propertie
65
80
  export declare function OperationSchema(): z.ZodObject<Properties<Operation>>;
66
81
  export declare function PruneActionSchema(): z.ZodObject<Properties<PruneAction>>;
67
82
  export declare function PruneActionInputSchema(): z.ZodObject<Properties<PruneActionInput>>;
83
+ export declare const RedoActionInputSchema: (params?: ({
84
+ errorMap?: z.ZodErrorMap | undefined;
85
+ invalid_type_error?: string | undefined;
86
+ required_error?: string | undefined;
87
+ description?: string | undefined;
88
+ } & {
89
+ coerce?: boolean | undefined;
90
+ }) | undefined) => z.ZodNumber;
68
91
  export declare function RedoActionSchema(): z.ZodObject<Properties<RedoAction>>;
92
+ export declare const SetNameActionInputSchema: (params?: ({
93
+ errorMap?: z.ZodErrorMap | undefined;
94
+ invalid_type_error?: string | undefined;
95
+ required_error?: string | undefined;
96
+ description?: string | undefined;
97
+ } & {
98
+ coerce?: true | undefined;
99
+ }) | undefined) => z.ZodString;
69
100
  export declare function SetNameActionSchema(): z.ZodObject<Properties<SetNameAction>>;
70
101
  export declare function SetNameOperationSchema(): z.ZodObject<Properties<SetNameOperation>>;
102
+ export declare const UndoActionInputSchema: (params?: ({
103
+ errorMap?: z.ZodErrorMap | undefined;
104
+ invalid_type_error?: string | undefined;
105
+ required_error?: string | undefined;
106
+ description?: string | undefined;
107
+ } & {
108
+ coerce?: boolean | undefined;
109
+ }) | undefined) => z.ZodNumber;
71
110
  export declare function UndoActionSchema(): z.ZodObject<Properties<UndoAction>>;
72
111
  export {};
@@ -14,17 +14,17 @@ export type { Immutable } from 'immer';
14
14
  *
15
15
  * @typeParam T - The name of the action type. A `string` type by default.
16
16
  */
17
- export type Action<T extends string = string, I = unknown> = {
17
+ export type Action<T extends string = string, I = unknown, S extends OperationScope = OperationScope> = {
18
18
  /** The name of the action. */
19
19
  type: T;
20
20
  /** The payload of the action. */
21
21
  input: I;
22
+ /** The scope of the action, can either be 'global' or 'local' */
23
+ scope: S;
22
24
  /** The attachments included in the action. */
23
25
  attachments?: AttachmentInput[] | undefined;
24
- /** The scope of the action, can either be 'global' or 'local'. Defaults to 'global' */
25
- scope?: OperationScope;
26
26
  };
27
- export type ActionWithAttachment<T extends string = string, I = unknown> = Action<T, I> & {
27
+ export type ActionWithAttachment<T extends string = string, I = unknown, S extends OperationScope = OperationScope> = Action<T, I, S> & {
28
28
  attachments: AttachmentInput[];
29
29
  };
30
30
  /**
@@ -34,7 +34,7 @@ export type ActionWithAttachment<T extends string = string, I = unknown> = Actio
34
34
  * @typeParam State - The type of the document data.
35
35
  * @typeParam A - The type of the actions supported by the reducer.
36
36
  */
37
- export type Reducer<State, A extends Action> = (state: Document<State, A>, action: A | BaseAction, dispatch?: SignalDispatch) => Document<State, A>;
37
+ export type Reducer<State, A extends Action, LocalState> = (state: Document<State, A, LocalState>, action: A | BaseAction, dispatch?: SignalDispatch) => Document<State, A, LocalState>;
38
38
  /**
39
39
  * A {@link Reducer} that prevents mutable code from changing the previous state.
40
40
  *
@@ -47,8 +47,8 @@ export type Reducer<State, A extends Action> = (state: Document<State, A>, actio
47
47
  * @typeParam State - The type of the document data.
48
48
  * @typeParam A - The type of the actions supported by the reducer.
49
49
  */
50
- export type ImmutableReducer<State, A extends Action> = (state: Draft<Document<State, A>>, action: A | BaseAction, dispatch?: SignalDispatch) => Document<State, A> | void;
51
- export type ImmutableStateReducer<State, A extends Action> = (state: Draft<State>, action: A, dispatch?: SignalDispatch) => State | void;
50
+ export type ImmutableReducer<State, A extends Action, LocalState> = (state: Draft<Document<State, A, LocalState>>, action: A | BaseAction, dispatch?: SignalDispatch) => Document<State, A, LocalState> | void;
51
+ export type ImmutableStateReducer<S, A extends Action, L = unknown> = (state: Draft<State<S, L>>, action: A, dispatch?: SignalDispatch) => State<S, L> | void;
52
52
  /**
53
53
  * Scope of an operation.
54
54
  * Global: The operation is synchronized everywhere in the network. This is the default document operation.
@@ -111,12 +111,22 @@ export type AttachmentInput = Attachment & {
111
111
  *
112
112
  */
113
113
  export type FileRegistry = Record<AttachmentRef, Attachment>;
114
- export type ExtendedState<State = unknown> = DocumentHeader & {
114
+ export type State<GlobalState, LocalState> = {
115
+ global: GlobalState;
116
+ local: LocalState;
117
+ };
118
+ export type PartialState<S> = S | Partial<S>;
119
+ export type CreateState<S, L> = (state?: Partial<State<PartialState<S>, PartialState<L>>>) => State<S, L>;
120
+ export type ExtendedState<GlobalState, LocalState = unknown> = DocumentHeader & {
115
121
  /** The document model specific state. */
116
- state: State;
122
+ state: State<GlobalState, LocalState>;
117
123
  /** The index of document attachments. */
118
124
  attachments: FileRegistry;
119
125
  };
126
+ export type DocumentOperations<A extends Action> = {
127
+ global: Operation<A | BaseAction>[];
128
+ local: Operation<A>[];
129
+ };
120
130
  /**
121
131
  * The base type of a document model.
122
132
  *
@@ -126,13 +136,13 @@ export type ExtendedState<State = unknown> = DocumentHeader & {
126
136
  * @typeParam Data - The type of the document data attribute.
127
137
  * @typeParam A - The type of the actions supported by the Document.
128
138
  */
129
- export type Document<S = unknown, A extends Action = Action> =
139
+ export type Document<GlobalState = unknown, A extends Action = Action, LocalState = unknown> =
130
140
  /** The document model specific state. */
131
- ExtendedState<S> & {
141
+ ExtendedState<GlobalState, LocalState> & {
132
142
  /** The operations history of the document. */
133
- operations: Operation<A | BaseAction>[];
143
+ operations: DocumentOperations<A>;
134
144
  /** The initial state of the document, enabling replaying operations. */
135
- initialState: ExtendedState<S>;
145
+ initialState: ExtendedState<GlobalState, LocalState>;
136
146
  };
137
147
  /**
138
148
  * String type representing an attachment in a Document.
@@ -141,40 +151,40 @@ ExtendedState<S> & {
141
151
  * Attachment string is formatted as `attachment://<filename>`.
142
152
  */
143
153
  export type AttachmentRef = string;
144
- export interface DocumentClass<S, A extends Action = Action, C extends BaseDocument<S, A> = BaseDocument<S, A>> {
154
+ export interface DocumentClass<S, A extends Action = Action, L = unknown, C extends BaseDocument<S, A, L> = BaseDocument<S, A, L>> {
145
155
  fileExtension: string;
146
156
  fromFile: (path: string) => Promise<C>;
147
- new (initialState?: ExtendedState<S>): C;
157
+ new (initialState?: ExtendedState<S, L>): C;
148
158
  }
149
- export type DocumentModelUtils<S = unknown, A extends Action = Action> = {
159
+ export type DocumentModelUtils<S = unknown, A extends Action = Action, L = unknown> = {
150
160
  fileExtension: string;
151
- createState: (state?: Partial<S>) => S;
152
- createExtendedState: (extendedState?: Partial<ExtendedState<Partial<S>>>, createState?: (state?: Partial<S>) => S) => ExtendedState<S>;
153
- createDocument: (document?: Partial<ExtendedState<Partial<S>>>, createState?: (state?: Partial<S>) => S) => Document<S, A>;
154
- loadFromFile: (path: string) => Promise<Document<S, A>>;
155
- loadFromInput: (input: FileInput) => Promise<Document<S, A>>;
156
- saveToFile: (document: Document<S, A>, path: string, name?: string) => Promise<string>;
157
- saveToFileHandle: (document: Document<S, A>, input: FileSystemFileHandle) => Promise<void>;
161
+ createState: CreateState<S, L>;
162
+ createExtendedState: (extendedState?: Partial<ExtendedState<PartialState<S>, PartialState<L>>>, createState?: CreateState<S, L>) => ExtendedState<S, L>;
163
+ createDocument: (document?: Partial<ExtendedState<PartialState<S>, PartialState<L>>>, createState?: CreateState<S, L>) => Document<S, A, L>;
164
+ loadFromFile: (path: string) => Promise<Document<S, A, L>>;
165
+ loadFromInput: (input: FileInput) => Promise<Document<S, A, L>>;
166
+ saveToFile: (document: Document<S, A, L>, path: string, name?: string) => Promise<string>;
167
+ saveToFileHandle: (document: Document<S, A, L>, input: FileSystemFileHandle) => Promise<void>;
158
168
  };
159
169
  export type ActionCreator<A extends Action> = ((input: any) => A) | ((input: any, attachments: AttachmentInput[]) => A) | ((...input: any) => BaseAction);
160
- export type DocumentModel<S = unknown, A extends Action = Action, C extends BaseDocument<S, A> = BaseDocument<S, A>> = {
161
- Document: DocumentClass<S, A, C>;
162
- reducer: Reducer<S, A>;
170
+ export type DocumentModel<S = unknown, A extends Action = Action, L = unknown, C extends BaseDocument<S, A, L> = BaseDocument<S, A, L>> = {
171
+ Document: DocumentClass<S, A, L, C>;
172
+ reducer: Reducer<S, A, L>;
163
173
  actions: Record<string, ActionCreator<A>>;
164
- utils: DocumentModelUtils<S, A>;
174
+ utils: DocumentModelUtils<S, A, L>;
165
175
  documentModel: DocumentModelState;
166
176
  };
167
177
  export type EditorContext = {
168
178
  theme: 'light' | 'dark';
169
179
  debug?: boolean;
170
180
  };
171
- export type EditorProps<S, A extends Action> = {
172
- document: Document<S, A>;
181
+ export type EditorProps<S, A extends Action, L> = {
182
+ document: Document<S, A, L>;
173
183
  dispatch: (action: A | BaseAction) => void;
174
184
  editorContext: EditorContext;
175
185
  };
176
- export type Editor<S = unknown, A extends Action = Action> = {
177
- Component: FC<EditorProps<S, A>>;
186
+ export type Editor<S = unknown, A extends Action = Action, L = unknown> = {
187
+ Component: FC<EditorProps<S, A, L>>;
178
188
  documentTypes: string[];
179
189
  };
180
190
  export type DocumentModelLib = {
@@ -1,5 +1,5 @@
1
1
  import { baseReducer } from '../reducer';
2
- import { Action, BaseAction, Document, ExtendedState, ImmutableStateReducer, Reducer, Immutable, OperationScope } from '../types';
2
+ import { Action, BaseAction, Document, ExtendedState, ImmutableStateReducer, Reducer, Immutable, OperationScope, State, CreateState, PartialState } from '../types';
3
3
  export declare function isBaseAction(action: Action): action is BaseAction;
4
4
  /**
5
5
  * Helper function to be used by action creators.
@@ -18,7 +18,7 @@ export declare function isBaseAction(action: Action): action is BaseAction;
18
18
  * @returns The new action.
19
19
  */
20
20
  export declare function createAction<A extends Action>(type: A['type'], input?: A['input'], attachments?: Action['attachments'], validator?: () => {
21
- parse(v: unknown): A;
21
+ parse(v: unknown): A['input'];
22
22
  }, scope?: OperationScope): A;
23
23
  /**
24
24
  * Helper function to create a document model reducer.
@@ -38,8 +38,8 @@ export declare function createAction<A extends Action>(type: A['type'], input?:
38
38
  *
39
39
  * @returns The new reducer.
40
40
  */
41
- export declare function createReducer<S = unknown, A extends Action = Action>(reducer: ImmutableStateReducer<S, A>, documentReducer?: typeof baseReducer): Reducer<S, A>;
42
- export declare const createExtendedState: <S>(initialState?: Partial<ExtendedState<Partial<S>>> | undefined, createState?: ((state?: Partial<S> | undefined) => S) | undefined) => ExtendedState<S>;
41
+ export declare function createReducer<S = unknown, A extends Action = Action, L = unknown>(reducer: ImmutableStateReducer<S, A, L>, documentReducer?: typeof baseReducer): Reducer<S, A, L>;
42
+ export declare const createExtendedState: <S, L>(initialState?: Partial<ExtendedState<PartialState<S>, PartialState<L>>> | undefined, createState?: CreateState<S, L> | undefined) => ExtendedState<S, L>;
43
43
  /**
44
44
  * Builds the initial document state from the provided data.
45
45
  *
@@ -51,7 +51,7 @@ export declare const createExtendedState: <S>(initialState?: Partial<ExtendedSta
51
51
  *
52
52
  * @returns The new document state.
53
53
  */
54
- export declare const createDocument: <S, A extends Action>(initialState?: Partial<ExtendedState<Partial<S>>> | undefined, createState?: ((state?: Partial<S> | undefined) => S) | undefined) => Document<S, A>;
55
- export declare const hashDocument: (document: Document) => string;
54
+ export declare const createDocument: <S, A extends Action, L = unknown>(initialState?: Partial<ExtendedState<PartialState<S>, PartialState<L>>> | undefined, createState?: ((state?: Partial<State<PartialState<S>, PartialState<L>>> | undefined) => State<S, L>) | undefined) => Document<S, A, L>;
55
+ export declare const hashDocument: (document: Document, scope?: OperationScope) => string;
56
56
  export declare const hashKey: (date?: Date, randomLimit?: number) => string;
57
57
  export declare function readOnly<T>(value: T): Immutable<T>;