/
model-saving.ts
136 lines (120 loc) · 5.18 KB
/
model-saving.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/********************************************************************************
* Copyright (c) 2021-2023 STMicroelectronics and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { hasBooleanProp, hasStringProp } from '../utils/type-util';
import { Action, RequestAction, ResponseAction } from './base-protocol';
/**
* Sent from the client to the server in order to persist the current model state back to the source model.
* A new fileUri can be defined to save the model to a new destination different from its original source model.
* The corresponding namespace declares the action kind as constant and offers helper functions for type guard checks
* and creating new `SaveModelActions`.
*/
export interface SaveModelAction extends Action {
kind: typeof SaveModelAction.KIND;
/**
* The optional destination file uri.
*/
fileUri?: string;
}
export namespace SaveModelAction {
export const KIND = 'saveModel';
export function is(object: unknown): object is SaveModelAction {
return Action.hasKind(object, KIND);
}
export function create(options: { fileUri?: string } = {}): SaveModelAction {
return {
kind: KIND,
...options
};
}
}
/**
* The server sends this action to indicate to the client that the current model state on the server does not correspond
* to the persisted model state of the source model. A client may ignore such an action or use it to indicate to the user the dirty state.
* The corresponding namespace declares the action kind as constant and offers helper functions for type guard checks
* and creating new `SetDirtyStateActions`.
*/
export interface SetDirtyStateAction extends Action {
kind: typeof SetDirtyStateAction.KIND;
/**
* True if the current model state is dirty
*/
isDirty: boolean;
/**
* A string indicating the reason for the dirty state change e.g 'operation', 'undo',...
*/
reason?: DirtyStateChangeReason;
}
export type DirtyStateChangeReason = 'operation' | 'undo' | 'redo' | 'save' | 'external';
export namespace SetDirtyStateAction {
export const KIND = 'setDirtyState';
export function is(object: unknown): object is SetDirtyStateAction {
return Action.hasKind(object, KIND) && hasBooleanProp(object, 'isDirty');
}
export function create(isDirty: boolean, options: { reason?: DirtyStateChangeReason } = {}): SetDirtyStateAction {
return {
kind: KIND,
isDirty,
...options
};
}
}
/**
* A `RequestExportSvgAction` is sent by the client (or the server) to initiate the SVG export of the current diagram.
* The handler of this action is expected to retrieve the diagram SVG and should send a {@link ExportSvgAction} as response.
* Typically the {@link ExportSvgAction} is handled directly on client side.
*/
export interface RequestExportSvgAction extends RequestAction<ExportSvgAction> {
kind: typeof RequestExportSvgAction.KIND;
}
export namespace RequestExportSvgAction {
export const KIND = 'requestExportSvg';
export function is(object: unknown): object is RequestExportSvgAction {
return RequestAction.hasKind(object, KIND);
}
export function create(options: { requestId?: string } = {}): RequestExportSvgAction {
return {
kind: KIND,
requestId: '',
...options
};
}
}
/**
* The client sends an `ExportSvgAction` to indicate that the diagram, which represents the current model state,
* should be exported in SVG format. The action only provides the diagram SVG as plain string. The expected result of executing
* an `ExportSvgAction` is a new file in SVG-format on the underlying filesystem. However, other details like the target destination,
* concrete file name, file extension etc. are not specified in the protocol. So it is the responsibility of the action handler to
* process this information accordingly and export the result to the underlying filesystem.
*/
export interface ExportSvgAction extends ResponseAction {
kind: typeof ExportSvgAction.KIND;
svg: string;
responseId: string;
}
export namespace ExportSvgAction {
export const KIND = 'exportSvg';
export function is(object: unknown): object is ExportSvgAction {
return Action.hasKind(object, KIND) && hasStringProp(object, 'svg');
}
export function create(svg: string, options: { responseId?: string } = {}): ExportSvgAction {
return {
kind: KIND,
svg,
responseId: '',
...options
};
}
}