document-model 1.0.0

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 (163) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +197 -0
  3. package/dist/browser/document-model.cjs +1 -0
  4. package/dist/browser/document-model.d.ts +1 -0
  5. package/dist/browser/document-model.js +17 -0
  6. package/dist/browser/document.cjs +1 -0
  7. package/dist/browser/document.d.ts +1 -0
  8. package/dist/browser/document.js +15 -0
  9. package/dist/browser/index-1aa6b70c.js +1 -0
  10. package/dist/browser/index-47a38d27.mjs +1776 -0
  11. package/dist/browser/index-71473ecd.js +22 -0
  12. package/dist/browser/index-ede882c4.mjs +29 -0
  13. package/dist/browser/index.cjs +1 -0
  14. package/dist/browser/index.d.ts +3 -0
  15. package/dist/browser/index.js +18 -0
  16. package/dist/browser/object-38724dbb.js +1 -0
  17. package/dist/browser/object-916a56cd.mjs +641 -0
  18. package/dist/browser/src/document/actions/creators.d.ts +46 -0
  19. package/dist/browser/src/document/actions/index.d.ts +10 -0
  20. package/dist/browser/src/document/actions/types.d.ts +9 -0
  21. package/dist/browser/src/document/index.d.ts +5 -0
  22. package/dist/browser/src/document/object.d.ts +118 -0
  23. package/dist/browser/src/document/reducer.d.ts +15 -0
  24. package/dist/browser/src/document/schema/index.d.ts +2 -0
  25. package/dist/browser/src/document/schema/types.d.ts +168 -0
  26. package/dist/browser/src/document/schema/zod.d.ts +72 -0
  27. package/dist/browser/src/document/types.d.ts +154 -0
  28. package/dist/browser/src/document/utils/base.d.ts +55 -0
  29. package/dist/browser/src/document/utils/file.d.ts +47 -0
  30. package/dist/browser/src/document/utils/index.d.ts +2 -0
  31. package/dist/browser/src/document/utils/node.d.ts +9 -0
  32. package/dist/browser/src/document-model/custom/reducers/header.d.ts +2 -0
  33. package/dist/browser/src/document-model/custom/reducers/module.d.ts +2 -0
  34. package/dist/browser/src/document-model/custom/reducers/operation-error.d.ts +2 -0
  35. package/dist/browser/src/document-model/custom/reducers/operation-example.d.ts +2 -0
  36. package/dist/browser/src/document-model/custom/reducers/operation.d.ts +2 -0
  37. package/dist/browser/src/document-model/custom/reducers/state.d.ts +2 -0
  38. package/dist/browser/src/document-model/custom/reducers/versioning.d.ts +7 -0
  39. package/dist/browser/src/document-model/custom/utils.d.ts +1 -0
  40. package/dist/browser/src/document-model/gen/actions.d.ts +15 -0
  41. package/dist/browser/src/document-model/gen/creators.d.ts +7 -0
  42. package/dist/browser/src/document-model/gen/document-model.d.ts +2 -0
  43. package/dist/browser/src/document-model/gen/header/actions.d.ts +9 -0
  44. package/dist/browser/src/document-model/gen/header/creators.d.ts +8 -0
  45. package/dist/browser/src/document-model/gen/header/object.d.ts +11 -0
  46. package/dist/browser/src/document-model/gen/header/operations.d.ts +10 -0
  47. package/dist/browser/src/document-model/gen/index.d.ts +5 -0
  48. package/dist/browser/src/document-model/gen/module/actions.d.ts +8 -0
  49. package/dist/browser/src/document-model/gen/module/creators.d.ts +7 -0
  50. package/dist/browser/src/document-model/gen/module/object.d.ts +10 -0
  51. package/dist/browser/src/document-model/gen/module/operations.d.ts +9 -0
  52. package/dist/browser/src/document-model/gen/object.d.ts +28 -0
  53. package/dist/browser/src/document-model/gen/operation/actions.d.ts +12 -0
  54. package/dist/browser/src/document-model/gen/operation/creators.d.ts +11 -0
  55. package/dist/browser/src/document-model/gen/operation/object.d.ts +14 -0
  56. package/dist/browser/src/document-model/gen/operation/operations.d.ts +13 -0
  57. package/dist/browser/src/document-model/gen/operation-error/actions.d.ts +10 -0
  58. package/dist/browser/src/document-model/gen/operation-error/creators.d.ts +9 -0
  59. package/dist/browser/src/document-model/gen/operation-error/object.d.ts +12 -0
  60. package/dist/browser/src/document-model/gen/operation-error/operations.d.ts +11 -0
  61. package/dist/browser/src/document-model/gen/operation-example/actions.d.ts +7 -0
  62. package/dist/browser/src/document-model/gen/operation-example/creators.d.ts +6 -0
  63. package/dist/browser/src/document-model/gen/operation-example/object.d.ts +9 -0
  64. package/dist/browser/src/document-model/gen/operation-example/operations.d.ts +8 -0
  65. package/dist/browser/src/document-model/gen/reducer.d.ts +3 -0
  66. package/dist/browser/src/document-model/gen/schema/index.d.ts +2 -0
  67. package/dist/browser/src/document-model/gen/schema/types.d.ts +435 -0
  68. package/dist/browser/src/document-model/gen/schema/zod.d.ts +428 -0
  69. package/dist/browser/src/document-model/gen/state/actions.d.ts +9 -0
  70. package/dist/browser/src/document-model/gen/state/creators.d.ts +8 -0
  71. package/dist/browser/src/document-model/gen/state/object.d.ts +11 -0
  72. package/dist/browser/src/document-model/gen/state/operations.d.ts +10 -0
  73. package/dist/browser/src/document-model/gen/types.d.ts +8 -0
  74. package/dist/browser/src/document-model/gen/utils.d.ts +4 -0
  75. package/dist/browser/src/document-model/gen/versioning/actions.d.ts +8 -0
  76. package/dist/browser/src/document-model/gen/versioning/creators.d.ts +7 -0
  77. package/dist/browser/src/document-model/gen/versioning/object.d.ts +10 -0
  78. package/dist/browser/src/document-model/gen/versioning/operations.d.ts +9 -0
  79. package/dist/browser/src/document-model/index.d.ts +73 -0
  80. package/dist/browser/src/index.d.ts +25 -0
  81. package/dist/browser/src/index.doc.d.ts +20 -0
  82. package/dist/browser/vite.config.d.ts +2 -0
  83. package/dist/node/document-model.cjs +1 -0
  84. package/dist/node/document-model.d.ts +1 -0
  85. package/dist/node/document-model.js +20 -0
  86. package/dist/node/document.cjs +1 -0
  87. package/dist/node/document.d.ts +1 -0
  88. package/dist/node/document.js +18 -0
  89. package/dist/node/index-03f82dde.mjs +29 -0
  90. package/dist/node/index-0b695055.js +1 -0
  91. package/dist/node/index-78a1221c.mjs +1779 -0
  92. package/dist/node/index-fbaffded.js +22 -0
  93. package/dist/node/index.cjs +1 -0
  94. package/dist/node/index.d.ts +3 -0
  95. package/dist/node/index.js +21 -0
  96. package/dist/node/object-b1510be8.js +1 -0
  97. package/dist/node/object-bdb7f8c6.mjs +654 -0
  98. package/dist/node/src/document/actions/creators.d.ts +46 -0
  99. package/dist/node/src/document/actions/index.d.ts +10 -0
  100. package/dist/node/src/document/actions/types.d.ts +9 -0
  101. package/dist/node/src/document/index.d.ts +5 -0
  102. package/dist/node/src/document/object.d.ts +118 -0
  103. package/dist/node/src/document/reducer.d.ts +15 -0
  104. package/dist/node/src/document/schema/index.d.ts +2 -0
  105. package/dist/node/src/document/schema/types.d.ts +168 -0
  106. package/dist/node/src/document/schema/zod.d.ts +72 -0
  107. package/dist/node/src/document/types.d.ts +154 -0
  108. package/dist/node/src/document/utils/base.d.ts +55 -0
  109. package/dist/node/src/document/utils/file.d.ts +47 -0
  110. package/dist/node/src/document/utils/index.d.ts +2 -0
  111. package/dist/node/src/document/utils/node.d.ts +9 -0
  112. package/dist/node/src/document-model/custom/reducers/header.d.ts +2 -0
  113. package/dist/node/src/document-model/custom/reducers/module.d.ts +2 -0
  114. package/dist/node/src/document-model/custom/reducers/operation-error.d.ts +2 -0
  115. package/dist/node/src/document-model/custom/reducers/operation-example.d.ts +2 -0
  116. package/dist/node/src/document-model/custom/reducers/operation.d.ts +2 -0
  117. package/dist/node/src/document-model/custom/reducers/state.d.ts +2 -0
  118. package/dist/node/src/document-model/custom/reducers/versioning.d.ts +7 -0
  119. package/dist/node/src/document-model/custom/utils.d.ts +1 -0
  120. package/dist/node/src/document-model/gen/actions.d.ts +15 -0
  121. package/dist/node/src/document-model/gen/creators.d.ts +7 -0
  122. package/dist/node/src/document-model/gen/document-model.d.ts +2 -0
  123. package/dist/node/src/document-model/gen/header/actions.d.ts +9 -0
  124. package/dist/node/src/document-model/gen/header/creators.d.ts +8 -0
  125. package/dist/node/src/document-model/gen/header/object.d.ts +11 -0
  126. package/dist/node/src/document-model/gen/header/operations.d.ts +10 -0
  127. package/dist/node/src/document-model/gen/index.d.ts +5 -0
  128. package/dist/node/src/document-model/gen/module/actions.d.ts +8 -0
  129. package/dist/node/src/document-model/gen/module/creators.d.ts +7 -0
  130. package/dist/node/src/document-model/gen/module/object.d.ts +10 -0
  131. package/dist/node/src/document-model/gen/module/operations.d.ts +9 -0
  132. package/dist/node/src/document-model/gen/object.d.ts +28 -0
  133. package/dist/node/src/document-model/gen/operation/actions.d.ts +12 -0
  134. package/dist/node/src/document-model/gen/operation/creators.d.ts +11 -0
  135. package/dist/node/src/document-model/gen/operation/object.d.ts +14 -0
  136. package/dist/node/src/document-model/gen/operation/operations.d.ts +13 -0
  137. package/dist/node/src/document-model/gen/operation-error/actions.d.ts +10 -0
  138. package/dist/node/src/document-model/gen/operation-error/creators.d.ts +9 -0
  139. package/dist/node/src/document-model/gen/operation-error/object.d.ts +12 -0
  140. package/dist/node/src/document-model/gen/operation-error/operations.d.ts +11 -0
  141. package/dist/node/src/document-model/gen/operation-example/actions.d.ts +7 -0
  142. package/dist/node/src/document-model/gen/operation-example/creators.d.ts +6 -0
  143. package/dist/node/src/document-model/gen/operation-example/object.d.ts +9 -0
  144. package/dist/node/src/document-model/gen/operation-example/operations.d.ts +8 -0
  145. package/dist/node/src/document-model/gen/reducer.d.ts +3 -0
  146. package/dist/node/src/document-model/gen/schema/index.d.ts +2 -0
  147. package/dist/node/src/document-model/gen/schema/types.d.ts +435 -0
  148. package/dist/node/src/document-model/gen/schema/zod.d.ts +428 -0
  149. package/dist/node/src/document-model/gen/state/actions.d.ts +9 -0
  150. package/dist/node/src/document-model/gen/state/creators.d.ts +8 -0
  151. package/dist/node/src/document-model/gen/state/object.d.ts +11 -0
  152. package/dist/node/src/document-model/gen/state/operations.d.ts +10 -0
  153. package/dist/node/src/document-model/gen/types.d.ts +8 -0
  154. package/dist/node/src/document-model/gen/utils.d.ts +4 -0
  155. package/dist/node/src/document-model/gen/versioning/actions.d.ts +8 -0
  156. package/dist/node/src/document-model/gen/versioning/creators.d.ts +7 -0
  157. package/dist/node/src/document-model/gen/versioning/object.d.ts +10 -0
  158. package/dist/node/src/document-model/gen/versioning/operations.d.ts +9 -0
  159. package/dist/node/src/document-model/index.d.ts +73 -0
  160. package/dist/node/src/index.d.ts +25 -0
  161. package/dist/node/src/index.doc.d.ts +20 -0
  162. package/dist/node/vite.config.d.ts +2 -0
  163. package/package.json +89 -0
@@ -0,0 +1,46 @@
1
+ import { ExtendedState } from '../types';
2
+ import { LoadStateAction, PruneAction, RedoAction, SetNameAction, UndoAction } from './types';
3
+ /**
4
+ * Changes the name of the document.
5
+ *
6
+ * @param name - The name to be set in the document.
7
+ * @category Actions
8
+ */
9
+ export declare const setName: (name: string) => SetNameAction;
10
+ /**
11
+ * Cancels the last `count` operations.
12
+ *
13
+ * @param count - Number of operations to cancel
14
+ * @category Actions
15
+ */
16
+ export declare const undo: (count?: number) => UndoAction;
17
+ /**
18
+ * Cancels the last `count` {@link undo | UNDO} operations.
19
+ *
20
+ * @param count - Number of UNDO operations to cancel
21
+ * @category Actions
22
+ */
23
+ export declare const redo: (count?: number) => RedoAction;
24
+ /**
25
+ * Joins multiple operations into a single {@link loadState | LOAD_STATE} operation.
26
+ *
27
+ * @remarks
28
+ * Useful to keep operations history smaller. Operations to prune are selected by index,
29
+ * similar to the {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice | slice} method in Arrays.
30
+ *
31
+ * @param start - Index of the first operation to prune
32
+ * @param end - Index of the last operation to prune
33
+ * @category Actions
34
+ */
35
+ export declare const prune: (start?: number | undefined, end?: number | undefined) => PruneAction;
36
+ /**
37
+ * Replaces the state of the document.
38
+ *
39
+ * @remarks
40
+ * This action shouldn't be used directly. It is dispatched by the {@link prune} action.
41
+ *
42
+ * @param state - State to be set in the document.
43
+ * @param operations - Number of operations that were removed from the previous state.
44
+ * @category Actions
45
+ */
46
+ export declare const loadState: (state: Pick<ExtendedState, 'state' | 'name'>, operations: number) => LoadStateAction;
@@ -0,0 +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: {
7
+ name: string;
8
+ state?: T;
9
+ }): Document<T, A>;
10
+ export * from './creators';
@@ -0,0 +1,9 @@
1
+ import { Action, BaseAction } from '../types';
2
+ export declare const SET_NAME = "SET_NAME";
3
+ export declare const UNDO = "UNDO";
4
+ export declare const REDO = "REDO";
5
+ export declare const PRUNE = "PRUNE";
6
+ export declare const LOAD_STATE = "LOAD_STATE";
7
+ export { LoadStateAction, PruneAction, RedoAction, SetNameAction, UndoAction, } from '../types';
8
+ export type { BaseAction };
9
+ export declare function isBaseAction(action: Action): action is BaseAction;
@@ -0,0 +1,5 @@
1
+ export * as actions from './actions/creators';
2
+ export * from './object';
3
+ export * from './reducer';
4
+ export * from './types';
5
+ export * as utils from './utils';
@@ -0,0 +1,118 @@
1
+ import { BaseAction } from './actions/types';
2
+ import type { Action, AttachmentRef, Document, ExtendedState, Reducer } from './types';
3
+ /**
4
+ * This is an abstract class representing a document and provides methods
5
+ * for creating and manipulating documents.
6
+ * @typeparam T - The type of data stored in the document.
7
+ * @typeparam A - The type of action the document can take.
8
+ */
9
+ export declare abstract class BaseDocument<T, A extends Action> {
10
+ protected _document: Document<T, A>;
11
+ private _reducer;
12
+ /**
13
+ * Constructs a BaseDocument instance with an initial state.
14
+ * @param reducer - The reducer function that updates the state.
15
+ * @param document - The initial state of the document.
16
+ */
17
+ constructor(reducer: Reducer<T, A>, document: Document<T, A>);
18
+ /**
19
+ * Dispatches an action to update the state of the document.
20
+ * @param action - The action to dispatch.
21
+ * @returns The Document instance.
22
+ */
23
+ protected dispatch(action: A | BaseAction): this;
24
+ /**
25
+ * Saves the state of the document to a file.
26
+ * @param path - The file path where the state should be saved.
27
+ * @param extension - The file extension to use when saving the state.
28
+ * @returns The file path where the state was saved.
29
+ */
30
+ protected saveToFile(path: string, extension: string, name?: string): Promise<string>;
31
+ /**
32
+ * Loads the state of the document from a file.
33
+ * @param path - The file path where the state is stored.
34
+ */
35
+ loadFromFile(path: string): Promise<void>;
36
+ /**
37
+ * Loads the state of the document from a file and returns it.
38
+ * @param path - The file path where the state is stored.
39
+ * @param reducer - The reducer function that updates the state.
40
+ * @returns The state of the document.
41
+ */
42
+ protected static stateFromFile<T, A extends Action>(path: string, reducer: Reducer<T, A>): Promise<Document<T, A>>;
43
+ /**
44
+ * Gets the current state of the document.
45
+ */
46
+ get state(): T;
47
+ /**
48
+ * Gets the list of operations performed on the document.
49
+ */
50
+ get operations(): import("./types").Operation<BaseAction | A>[];
51
+ /**
52
+ * Gets the name of the document.
53
+ */
54
+ get name(): string;
55
+ /**
56
+ * Gets the type of document.
57
+ */
58
+ get documentType(): string;
59
+ /**
60
+ * Gets the timestamp of the date the document was created.
61
+ */
62
+ get created(): string;
63
+ /**
64
+ * Gets the timestamp of the date the document was last modified.
65
+ */
66
+ get lastModified(): string;
67
+ /**
68
+ * Gets the revision number of the document.
69
+ */
70
+ get revision(): number;
71
+ /**
72
+ * Gets the initial state of the document.
73
+ */
74
+ get initialState(): ExtendedState<T>;
75
+ /**
76
+ * Gets the attachment associated with the given key.
77
+ * @param attachment - The key of the attachment to retrieve.
78
+ */
79
+ getAttachment(attachment: AttachmentRef): import("./types").Attachment;
80
+ /**
81
+ * Sets the name of the document.
82
+ * @param name - The new name of the document.
83
+ */
84
+ setName(name: string): this;
85
+ /**
86
+ * Reverts a number of actions from the document.
87
+ * @param count - The number of actions to revert.
88
+ */
89
+ undo(count: number): this;
90
+ /**
91
+ * Reapplies a number of actions to the document.
92
+ * @param count - The number of actions to reapply.
93
+ */
94
+ redo(count: number): this;
95
+ /**
96
+ * Removes a range of operations from the document.
97
+ * @param start - The starting index of the range to remove.
98
+ * @param end - The ending index of the range to remove.
99
+ */
100
+ prune(start?: number | undefined, end?: number | undefined): this;
101
+ /**
102
+ * Loads a document state and a set of operations.
103
+ * @param state - The state to load.
104
+ * @param operations - The operations to apply to the document.
105
+ */
106
+ loadState(state: Pick<ExtendedState<T>, 'state' | 'name'>, operations: number): this;
107
+ }
108
+ /**
109
+ * Applies multiple mixins to a base class.
110
+ * Used to have separate mixins to group methods by actions.
111
+ *
112
+ * @remarks
113
+ * {@link https://www.typescriptlang.org/docs/handbook/mixins.html#alternative-pattern}
114
+ *
115
+ * @param derivedCtor - The class to apply the mixins to.
116
+ * @param constructors - The constructors of the mixins.
117
+ */
118
+ export declare function applyMixins(derivedCtor: any, constructors: any[]): void;
@@ -0,0 +1,15 @@
1
+ import { BaseAction } from './actions/types';
2
+ import { Action, Document, ImmutableStateReducer } from './types';
3
+ /**
4
+ * Base document reducer that wraps a custom document reducer and handles
5
+ * document-level actions such as undo, redo, prune, and set name.
6
+ *
7
+ * @template T - The type of the state of the custom reducer.
8
+ * @template A - The type of the actions of the custom reducer.
9
+ * @param state - The current state of the document.
10
+ * @param action - The action object to apply to the state.
11
+ * @param customReducer - The custom reducer that implements the application logic
12
+ * specific to the document's state.
13
+ * @returns The new state of the document.
14
+ */
15
+ export declare function baseReducer<T, A extends Action>(document: Document<T, A>, action: A | BaseAction, customReducer: ImmutableStateReducer<T, A>): Document<T, A>;
@@ -0,0 +1,2 @@
1
+ export * from "./types";
2
+ export * as z from "./zod";
@@ -0,0 +1,168 @@
1
+ export type Maybe<T> = T | null;
2
+ export type InputMaybe<T> = T | null | undefined;
3
+ export type Exact<T extends {
4
+ [key: string]: unknown;
5
+ }> = {
6
+ [K in keyof T]: T[K];
7
+ };
8
+ export type MakeOptional<T, K extends keyof T> = Omit<T, K> & {
9
+ [SubKey in K]?: Maybe<T[SubKey]>;
10
+ };
11
+ export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & {
12
+ [SubKey in K]: Maybe<T[SubKey]>;
13
+ };
14
+ export type MakeEmpty<T extends {
15
+ [key: string]: unknown;
16
+ }, K extends keyof T> = {
17
+ [_ in K]?: never;
18
+ };
19
+ export type Incremental<T> = T | {
20
+ [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never;
21
+ };
22
+ /** All built-in and custom scalars, mapped to their actual values */
23
+ export type Scalars = {
24
+ ID: {
25
+ input: string;
26
+ output: string;
27
+ };
28
+ String: {
29
+ input: string;
30
+ output: string;
31
+ };
32
+ Boolean: {
33
+ input: boolean;
34
+ output: boolean;
35
+ };
36
+ Int: {
37
+ input: number;
38
+ output: number;
39
+ };
40
+ Float: {
41
+ input: number;
42
+ output: number;
43
+ };
44
+ Address: {
45
+ input: `${string}:0x${string}`;
46
+ output: `${string}:0x${string}`;
47
+ };
48
+ Attachment: {
49
+ input: string;
50
+ output: string;
51
+ };
52
+ DateTime: {
53
+ input: string;
54
+ output: string;
55
+ };
56
+ Unknown: {
57
+ input: unknown;
58
+ output: unknown;
59
+ };
60
+ };
61
+ export type Action = IAction & {
62
+ __typename?: 'Action';
63
+ type: Scalars['String']['output'];
64
+ };
65
+ export type BaseAction = LoadStateAction | PruneAction | RedoAction | SetNameAction | UndoAction;
66
+ export type DocumentFile = {
67
+ __typename?: 'DocumentFile';
68
+ data: Scalars['String']['output'];
69
+ extension: Maybe<Scalars['String']['output']>;
70
+ fileName: Maybe<Scalars['String']['output']>;
71
+ mimeType: Scalars['String']['output'];
72
+ };
73
+ export type IAction = {
74
+ type: Scalars['String']['output'];
75
+ };
76
+ export type IDocument = {
77
+ created: Scalars['DateTime']['output'];
78
+ documentType: Scalars['String']['output'];
79
+ lastModified: Scalars['DateTime']['output'];
80
+ name: Scalars['String']['output'];
81
+ operations: Array<IOperation>;
82
+ revision: Scalars['Int']['output'];
83
+ };
84
+ export type IOperation = {
85
+ hash: Scalars['String']['output'];
86
+ index: Scalars['Int']['output'];
87
+ timestamp: Scalars['DateTime']['output'];
88
+ type: Scalars['String']['output'];
89
+ };
90
+ export type Load_State = 'LOAD_STATE';
91
+ export type LoadStateAction = {
92
+ input: LoadStateActionInput;
93
+ type: Load_State | `${Load_State}`;
94
+ };
95
+ export type LoadStateActionInput = {
96
+ operations: Scalars['Int']['input'];
97
+ state: LoadStateActionStateInput;
98
+ };
99
+ export type LoadStateActionStateInput = {
100
+ data?: InputMaybe<Scalars['Unknown']['input']>;
101
+ name: Scalars['String']['input'];
102
+ };
103
+ export type Mutation = {
104
+ __typename?: 'Mutation';
105
+ loadState: Maybe<IDocument>;
106
+ prune: Maybe<IDocument>;
107
+ redo: Maybe<IDocument>;
108
+ setName: Maybe<IDocument>;
109
+ undo: Maybe<IDocument>;
110
+ };
111
+ export type MutationLoadStateArgs = {
112
+ input: LoadStateAction;
113
+ };
114
+ export type MutationPruneArgs = {
115
+ input: PruneAction;
116
+ };
117
+ export type MutationRedoArgs = {
118
+ input: RedoAction;
119
+ };
120
+ export type MutationSetNameArgs = {
121
+ input: SetNameAction;
122
+ };
123
+ export type MutationUndoArgs = {
124
+ input: UndoAction;
125
+ };
126
+ export type Operation = IOperation & {
127
+ __typename?: 'Operation';
128
+ hash: Scalars['String']['output'];
129
+ index: Scalars['Int']['output'];
130
+ timestamp: Scalars['DateTime']['output'];
131
+ type: Scalars['String']['output'];
132
+ };
133
+ export type Prune = 'PRUNE';
134
+ export type PruneAction = {
135
+ input: PruneActionInput;
136
+ type: Prune | `${Prune}`;
137
+ };
138
+ export type PruneActionInput = {
139
+ end?: InputMaybe<Scalars['Int']['input']>;
140
+ start?: InputMaybe<Scalars['Int']['input']>;
141
+ };
142
+ export type Query = {
143
+ __typename?: 'Query';
144
+ document: Maybe<IDocument>;
145
+ };
146
+ export type Redo = 'REDO';
147
+ export type RedoAction = {
148
+ input: Scalars['Int']['input'];
149
+ type: Redo | `${Redo}`;
150
+ };
151
+ export type Set_Name = 'SET_NAME';
152
+ export type SetNameAction = {
153
+ input: Scalars['String']['input'];
154
+ type: Set_Name | `${Set_Name}`;
155
+ };
156
+ export type SetNameOperation = IOperation & {
157
+ __typename?: 'SetNameOperation';
158
+ hash: Scalars['String']['output'];
159
+ index: Scalars['Int']['output'];
160
+ input: Scalars['String']['output'];
161
+ timestamp: Scalars['DateTime']['output'];
162
+ type: Scalars['String']['output'];
163
+ };
164
+ export type Undo = 'UNDO';
165
+ export type UndoAction = {
166
+ input: Scalars['Int']['input'];
167
+ type: Undo | `${Undo}`;
168
+ };
@@ -0,0 +1,72 @@
1
+ import { z } from 'zod';
2
+ import { Action, DocumentFile, LoadStateAction, LoadStateActionInput, LoadStateActionStateInput, Operation, PruneAction, PruneActionInput, RedoAction, SetNameAction, SetNameOperation, UndoAction } from '.';
3
+ type Properties<T> = Required<{
4
+ [K in keyof T]: z.ZodType<T[K], any, T[K]>;
5
+ }>;
6
+ type definedNonNullAny = {};
7
+ export declare const isDefinedNonNullAny: (v: any) => v is definedNonNullAny;
8
+ export declare const definedNonNullAnySchema: z.ZodEffects<z.ZodAny, any, any>;
9
+ export declare const Load_StateSchema: z.ZodEnum<["LOAD_STATE"]>;
10
+ export declare const PruneSchema: z.ZodEnum<["PRUNE"]>;
11
+ export declare const RedoSchema: z.ZodEnum<["REDO"]>;
12
+ export declare const Set_NameSchema: z.ZodEnum<["SET_NAME"]>;
13
+ export declare const UndoSchema: z.ZodEnum<["UNDO"]>;
14
+ export declare function ActionSchema(): z.ZodObject<Properties<Action>>;
15
+ export declare function BaseActionSchema(): z.ZodUnion<[z.ZodObject<Required<{
16
+ input: z.ZodType<LoadStateActionInput, any, LoadStateActionInput>;
17
+ type: z.ZodType<"LOAD_STATE", any, "LOAD_STATE">;
18
+ }>, z.UnknownKeysParam, z.ZodTypeAny, {
19
+ input: LoadStateActionInput;
20
+ type: "LOAD_STATE";
21
+ }, {
22
+ input: LoadStateActionInput;
23
+ type: "LOAD_STATE";
24
+ }>, z.ZodObject<Required<{
25
+ input: z.ZodType<PruneActionInput, any, PruneActionInput>;
26
+ type: z.ZodType<"PRUNE", any, "PRUNE">;
27
+ }>, z.UnknownKeysParam, z.ZodTypeAny, {
28
+ input: PruneActionInput;
29
+ type: "PRUNE";
30
+ }, {
31
+ input: PruneActionInput;
32
+ type: "PRUNE";
33
+ }>, z.ZodObject<Required<{
34
+ input: z.ZodType<number, any, number>;
35
+ type: z.ZodType<"REDO", any, "REDO">;
36
+ }>, z.UnknownKeysParam, z.ZodTypeAny, {
37
+ input: number;
38
+ type: "REDO";
39
+ }, {
40
+ input: number;
41
+ type: "REDO";
42
+ }>, z.ZodObject<Required<{
43
+ input: z.ZodType<string, any, string>;
44
+ type: z.ZodType<"SET_NAME", any, "SET_NAME">;
45
+ }>, z.UnknownKeysParam, z.ZodTypeAny, {
46
+ input: string;
47
+ type: "SET_NAME";
48
+ }, {
49
+ input: string;
50
+ type: "SET_NAME";
51
+ }>, z.ZodObject<Required<{
52
+ input: z.ZodType<number, any, number>;
53
+ type: z.ZodType<"UNDO", any, "UNDO">;
54
+ }>, z.UnknownKeysParam, z.ZodTypeAny, {
55
+ input: number;
56
+ type: "UNDO";
57
+ }, {
58
+ input: number;
59
+ type: "UNDO";
60
+ }>]>;
61
+ export declare function DocumentFileSchema(): z.ZodObject<Properties<DocumentFile>>;
62
+ export declare function LoadStateActionSchema(): z.ZodObject<Properties<LoadStateAction>>;
63
+ export declare function LoadStateActionInputSchema(): z.ZodObject<Properties<LoadStateActionInput>>;
64
+ export declare function LoadStateActionStateInputSchema(): z.ZodObject<Properties<LoadStateActionStateInput>>;
65
+ export declare function OperationSchema(): z.ZodObject<Properties<Operation>>;
66
+ export declare function PruneActionSchema(): z.ZodObject<Properties<PruneAction>>;
67
+ export declare function PruneActionInputSchema(): z.ZodObject<Properties<PruneActionInput>>;
68
+ export declare function RedoActionSchema(): z.ZodObject<Properties<RedoAction>>;
69
+ export declare function SetNameActionSchema(): z.ZodObject<Properties<SetNameAction>>;
70
+ export declare function SetNameOperationSchema(): z.ZodObject<Properties<SetNameOperation>>;
71
+ export declare function UndoActionSchema(): z.ZodObject<Properties<UndoAction>>;
72
+ export {};
@@ -0,0 +1,154 @@
1
+ import type { Draft } from 'immer';
2
+ import type { DocumentModelState } from '../document-model';
3
+ import type { BaseAction } from './actions/types';
4
+ import { BaseDocument } from './object';
5
+ import { FileInput } from './utils';
6
+ export { z } from './schema';
7
+ export type * from './schema/types';
8
+ /**
9
+ * Defines the basic structure of an action.
10
+ *
11
+ * @typeParam T - The name of the action type. A `string` type by default.
12
+ */
13
+ export type Action<T extends string = string, I = unknown> = {
14
+ /** The name of the action. */
15
+ type: T;
16
+ /** The payload of the action. */
17
+ input: I;
18
+ /** The attachments included in the action. */
19
+ attachments?: AttachmentInput[] | undefined;
20
+ };
21
+ export type ActionWithAttachment<T extends string = string, I = unknown> = Action<T, I> & {
22
+ attachments: AttachmentInput[];
23
+ };
24
+ /**
25
+ * A pure function that takes an action and the previous state
26
+ * of the document and returns the new state.
27
+ *
28
+ * @typeParam State - The type of the document data.
29
+ * @typeParam A - The type of the actions supported by the reducer.
30
+ */
31
+ export type Reducer<State, A extends Action> = (state: Document<State, A>, action: A | BaseAction) => Document<State, A>;
32
+ /**
33
+ * A {@link Reducer} that prevents mutable code from changing the previous state.
34
+ *
35
+ * @remarks
36
+ * This reducer is wrapped with {@link https://immerjs.github.io/immer/ | Immer}.
37
+ * This allows the reducer code to be mutable, making it simpler and
38
+ * avoiding unintended changes in the provided state.
39
+ * The returned state will always be a new object.
40
+ *
41
+ * @typeParam State - The type of the document data.
42
+ * @typeParam A - The type of the actions supported by the reducer.
43
+ */
44
+ export type ImmutableReducer<State, A extends Action> = (state: Draft<Document<State, A>>, action: A | BaseAction) => Document<State, A> | void;
45
+ export type ImmutableStateReducer<State, A extends Action> = (state: Draft<State>, action: A) => State | void;
46
+ /**
47
+ * An operation that was applied to a {@link Document}.
48
+ *
49
+ * @remarks
50
+ * Wraps an action with an index, to be added to the operations history of a Document.
51
+ * The `index` field is used to keep all operations in order and enable replaying the
52
+ * document's history from the beginning.
53
+ *
54
+ * @typeParam A - The type of the action.
55
+ */
56
+ export type Operation<A extends Action = Action> = A & {
57
+ /** Position of the operation in the history */
58
+ index: number;
59
+ /** Timestamp of when the operation was added */
60
+ timestamp: string;
61
+ /** Hash of the resulting document data after the operation */
62
+ hash: string;
63
+ };
64
+ /**
65
+ * The base attributes of a {@link Document}.
66
+ */
67
+ export type DocumentHeader = {
68
+ /** The name of the document. */
69
+ name: string;
70
+ /** The number of operations applied to the document. */
71
+ revision: number;
72
+ /** The type of the document model. */
73
+ documentType: string;
74
+ /** The timestamp of the creation date of the document. */
75
+ created: string;
76
+ /** The timestamp of the last change in the document. */
77
+ lastModified: string;
78
+ };
79
+ /**
80
+ * The attributes stored for a file. Namely, attachments of a document.
81
+ */
82
+ export type Attachment = {
83
+ /** The binary data of the attachment in Base64 */
84
+ data: string;
85
+ /** The MIME type of the attachment */
86
+ mimeType: string;
87
+ extension?: string | null;
88
+ fileName?: string | null;
89
+ };
90
+ export type AttachmentInput = Attachment & {
91
+ hash: string;
92
+ };
93
+ /**
94
+ * Object that indexes attachments of a Document.
95
+ *
96
+ * @remarks
97
+ * This is used to reduce memory usage to avoid
98
+ * multiple instances of the binary data of the attachments.
99
+ *
100
+ */
101
+ export type FileRegistry = Record<AttachmentRef, Attachment>;
102
+ export type ExtendedState<State = unknown> = DocumentHeader & {
103
+ /** The document model specific state. */
104
+ state: State;
105
+ /** The index of document attachments. */
106
+ attachments: FileRegistry;
107
+ };
108
+ /**
109
+ * The base type of a document model.
110
+ *
111
+ * @remarks
112
+ * This type is extended by all Document models.
113
+ *
114
+ * @typeParam Data - The type of the document data attribute.
115
+ * @typeParam A - The type of the actions supported by the Document.
116
+ */
117
+ export type Document<S = unknown, A extends Action = Action> =
118
+ /** The document model specific state. */
119
+ ExtendedState<S> & {
120
+ /** The operations history of the document. */
121
+ operations: Operation<A | BaseAction>[];
122
+ /** The initial state of the document, enabling replaying operations. */
123
+ initialState: ExtendedState<S>;
124
+ };
125
+ /**
126
+ * String type representing an attachment in a Document.
127
+ *
128
+ * @remarks
129
+ * Attachment string is formatted as `attachment://<filename>`.
130
+ */
131
+ export type AttachmentRef = string;
132
+ export interface DocumentClass<S, A extends Action = Action, C extends BaseDocument<S, A> = BaseDocument<S, A>> {
133
+ fileExtension: string;
134
+ fromFile: (path: string) => Promise<C>;
135
+ new (initialState?: ExtendedState<S>): C;
136
+ }
137
+ export type DocumentModelUtils<S = unknown, A extends Action = Action> = {
138
+ fileExtension: string;
139
+ createState: (state?: Partial<S>) => S;
140
+ createExtendedState: (extendedState?: Partial<ExtendedState<Partial<S>>>, createState?: (state?: Partial<S>) => S) => ExtendedState<S>;
141
+ createDocument: (document?: Partial<ExtendedState<Partial<S>>>, createState?: (state?: Partial<S>) => S) => Document<S, A>;
142
+ loadFromFile: (path: string) => Promise<Document<S, A>>;
143
+ loadFromInput: (input: FileInput) => Promise<Document<S, A>>;
144
+ saveToFile: (document: Document<S, A>, path: string, name?: string) => Promise<string>;
145
+ saveToFileHandle: (document: Document<S, A>, input: FileSystemFileHandle) => Promise<void>;
146
+ };
147
+ export type ActionCreator<A extends Action> = ((input: any) => A) | ((input: any, attachments: AttachmentInput[]) => A) | ((...input: any) => BaseAction);
148
+ export type DocumentModel<S = unknown, A extends Action = Action, C extends BaseDocument<S, A> = BaseDocument<S, A>> = {
149
+ Document: DocumentClass<S, A, C>;
150
+ reducer: Reducer<S, A>;
151
+ actions: Record<string, ActionCreator<A>>;
152
+ utils: DocumentModelUtils<S, A>;
153
+ documentModel: DocumentModelState;
154
+ };
@@ -0,0 +1,55 @@
1
+ import { baseReducer } from '../reducer';
2
+ import { Action, Document, ExtendedState, ImmutableStateReducer, Reducer } from '../types';
3
+ /**
4
+ * Helper function to be used by action creators.
5
+ *
6
+ * @remarks
7
+ * Creates an action with the given type and input properties. The input
8
+ * properties default to an empty object.
9
+ *
10
+ * @typeParam A - Type of the action to be returned.
11
+ *
12
+ * @param type - The type of the action.
13
+ * @param input - The input properties of the action.
14
+ *
15
+ * @throws Error if the type is empty or not a string.
16
+ *
17
+ * @returns The new action.
18
+ */
19
+ export declare function createAction<A extends Action>(type: A['type'], input?: A['input'], attachments?: Action['attachments'], validator?: () => {
20
+ parse(v: unknown): A;
21
+ }): A;
22
+ /**
23
+ * Helper function to create a document model reducer.
24
+ *
25
+ * @remarks
26
+ * This function creates a new reducer that wraps the provided `reducer` with
27
+ * `documentReducer`, adding support for document actions:
28
+ * - `SET_NAME`
29
+ * - `UNDO`
30
+ * - `REDO`
31
+ * - `PRUNE`
32
+ *
33
+ * It also updates the document-related attributes on every operation.
34
+ *
35
+ * @param reducer - The custom reducer to wrap.
36
+ * @param documentReducer - The document reducer to use.
37
+ *
38
+ * @returns The new reducer.
39
+ */
40
+ export declare function createReducer<S = unknown, A extends Action = Action>(reducer: ImmutableStateReducer<S, A>, documentReducer?: typeof baseReducer): Reducer<S, A>;
41
+ export declare const createExtendedState: <S>(initialState?: Partial<ExtendedState<Partial<S>>> | undefined, createState?: ((state?: Partial<S> | undefined) => S) | undefined) => ExtendedState<S>;
42
+ /**
43
+ * Builds the initial document state from the provided data.
44
+ *
45
+ * @typeParam T - The type of the data.
46
+ * @typeParam A - The type of the actions.
47
+ *
48
+ * @param initialState - The initial state of the document. The `data` property
49
+ * is required, but all other properties are optional.
50
+ *
51
+ * @returns The new document state.
52
+ */
53
+ export declare const createDocument: <S, A extends Action>(initialState?: Partial<ExtendedState<Partial<S>>> | undefined, createState?: ((state?: Partial<S> | undefined) => S) | undefined) => Document<S, A>;
54
+ export declare const hashDocument: (document: Document) => string;
55
+ export declare const hashKey: (date?: Date, randomLimit?: number) => string;