1 | /*
|
2 | * Copyright (C) 1998-2021 by Northwoods Software Corporation. All Rights Reserved.
|
3 | */
|
4 | /*
|
5 | * This is an extension and not part of the main GoJS library.
|
6 | * Note that the API for this class may change with any version, even point releases.
|
7 | * If you intend to use an extension in production, you should copy the code to your own source directory.
|
8 | * Extensions can be found in the GoJS kit under the extensions or extensionsTS folders.
|
9 | * See the Extensions intro page (https://gojs.net/latest/intro/extensions.html) for more information.
|
10 | */
|
11 | import * as go from '../release/go-module.js';
|
12 | /**
|
13 | * This CommandHandler class uses localStorage as the repository for the clipboard,
|
14 | * rather than an in-memory global variable.
|
15 | * It requires that the {@link Diagram#model} be serializable and deserializable using {@link Model#toJson} and {@link Model.fromJson}.
|
16 | *
|
17 | * The {@link #copyToClipboard} and {@link #pasteFromClipboard} functions fall back to using the standard definitions
|
18 | * if there are any errors calling `Storage.getItem` or `Storage.setItem`.
|
19 | *
|
20 | * Typical usage:
|
21 | * ```js
|
22 | * $(go.Diagram, "myDiagramDiv",
|
23 | * {
|
24 | * commandHandler: $(LocalStorageCommandHandler),
|
25 | * ...
|
26 | * }
|
27 | * )
|
28 | * ```
|
29 | * or:
|
30 | * ```js
|
31 | * myDiagram.commandHandler = new LocalStorageCommandHandler();
|
32 | * ```
|
33 | *
|
34 | * If you want to experiment with this extension, try the <a href="../../extensionsJSM/LocalStorageCommandHandler.html">Local Storage Commands</a> sample.
|
35 | * @category Extension
|
36 | */
|
37 | export class LocalStorageCommandHandler extends go.CommandHandler {
|
38 | constructor() {
|
39 | super(...arguments);
|
40 | this.StorageKey = 'go._clipboard';
|
41 | this.FormatKey = 'go._clipboardFormat';
|
42 | }
|
43 | /**
|
44 | * Makes a copy of the given collection of {@link Part}s
|
45 | * and stores it as JSON in LocalStorage.
|
46 | * @param {Iterable.<Part>} coll a collection of {@link Part}s.
|
47 | */
|
48 | copyToClipboard(coll) {
|
49 | try {
|
50 | if (coll === null) {
|
51 | window.localStorage.setItem(this.StorageKey, '');
|
52 | window.localStorage.setItem(this.FormatKey, '');
|
53 | }
|
54 | else {
|
55 | const clipdiag = new go.Diagram(); // create a temporary Diagram
|
56 | // copy from this diagram to the temporary diagram some properties that affects copying:
|
57 | clipdiag.isTreePathToChildren = this.diagram.isTreePathToChildren;
|
58 | clipdiag.toolManager.draggingTool.dragsLink = this.diagram.toolManager.draggingTool.dragsLink;
|
59 | // create a model like this one but with no data
|
60 | clipdiag.model = this.diagram.model.copy();
|
61 | // copy the given Parts into this temporary Diagram
|
62 | this.diagram.copyParts(coll, clipdiag, false);
|
63 | window.localStorage.setItem(this.StorageKey, clipdiag.model.toJson());
|
64 | window.localStorage.setItem(this.FormatKey, clipdiag.model.dataFormat);
|
65 | }
|
66 | }
|
67 | catch (ex) {
|
68 | // fallback implementation
|
69 | super.copyToClipboard(coll);
|
70 | }
|
71 | }
|
72 | /**
|
73 | * If LocalStorage holds JSON for a collection of {@link Part}s,
|
74 | * and if the {@link Model#dataFormat} matches that stored in the clipboard,
|
75 | * this makes a copy of the clipboard's parts and adds the copies to this {@link Diagram}.
|
76 | * @return {Set.<Part>} a collection of newly pasted {@link Part}s
|
77 | */
|
78 | pasteFromClipboard() {
|
79 | const coll = new go.Set();
|
80 | try {
|
81 | const clipstr = window.localStorage.getItem(this.StorageKey);
|
82 | const clipfrmt = window.localStorage.getItem(this.FormatKey);
|
83 | if (clipstr === null || clipstr === '' || clipfrmt !== this.diagram.model.dataFormat) {
|
84 | return coll;
|
85 | }
|
86 | else {
|
87 | const clipdiag = new go.Diagram(); // create a temporary Diagram
|
88 | // recover the model from the clipboard rendering
|
89 | clipdiag.model = go.Model.fromJson(clipstr);
|
90 | // copy all the CLIPDIAG Parts into this Diagram
|
91 | const allparts = new go.Set();
|
92 | allparts.addAll(clipdiag.parts).addAll(clipdiag.nodes).addAll(clipdiag.links);
|
93 | const copymap = this.diagram.copyParts(allparts, this.diagram, false);
|
94 | // return a Set of the copied Parts
|
95 | return new go.Set().addAll(copymap.iteratorValues);
|
96 | }
|
97 | }
|
98 | catch (ex) {
|
99 | // fallback implementation
|
100 | return super.pasteFromClipboard();
|
101 | }
|
102 | }
|
103 | /**
|
104 | * This predicate controls whether or not the user can invoke the {@link #pasteSelection} command.
|
105 | *
|
106 | * This works just like {@link CommandHandler#canPasteSelection}, but looks at LocalStorage instead of a static variable.
|
107 | */
|
108 | canPasteSelection(pos) {
|
109 | const diagram = this.diagram;
|
110 | if (diagram.isReadOnly || diagram.isModelReadOnly)
|
111 | return false;
|
112 | if (!diagram.allowInsert || !diagram.allowClipboard)
|
113 | return false;
|
114 | try {
|
115 | const clipstr = window.localStorage.getItem(this.StorageKey);
|
116 | const clipfrmt = window.localStorage.getItem(this.FormatKey);
|
117 | if (clipstr === null || clipstr === '')
|
118 | return false;
|
119 | if (clipfrmt !== diagram.model.dataFormat)
|
120 | return false;
|
121 | return true;
|
122 | }
|
123 | catch (ex) {
|
124 | // fallback implementation
|
125 | return go.CommandHandler.prototype.canPasteSelection(pos);
|
126 | }
|
127 | }
|
128 | }
|