1 | /*
|
2 | * Copyright (C) 1998-2021 by Northwoods Software Corporation. All Rights Reserved.
|
3 | */
|
4 |
|
5 | /*
|
6 | * This is an extension and not part of the main GoJS library.
|
7 | * Note that the API for this class may change with any version, even point releases.
|
8 | * If you intend to use an extension in production, you should copy the code to your own source directory.
|
9 | * Extensions can be found in the GoJS kit under the extensions or extensionsTS folders.
|
10 | * See the Extensions intro page (https://gojs.net/latest/intro/extensions.html) for more information.
|
11 | */
|
12 |
|
13 | import * as go from '../release/go-module.js';
|
14 |
|
15 | /**
|
16 | * This CommandHandler class uses localStorage as the repository for the clipboard,
|
17 | * rather than an in-memory global variable.
|
18 | * It requires that the {@link Diagram#model} be serializable and deserializable using {@link Model#toJson} and {@link Model.fromJson}.
|
19 | *
|
20 | * The {@link #copyToClipboard} and {@link #pasteFromClipboard} functions fall back to using the standard definitions
|
21 | * if there are any errors calling `Storage.getItem` or `Storage.setItem`.
|
22 | *
|
23 | * Typical usage:
|
24 | * ```js
|
25 | * $(go.Diagram, "myDiagramDiv",
|
26 | * {
|
27 | * commandHandler: $(LocalStorageCommandHandler),
|
28 | * ...
|
29 | * }
|
30 | * )
|
31 | * ```
|
32 | * or:
|
33 | * ```js
|
34 | * myDiagram.commandHandler = new LocalStorageCommandHandler();
|
35 | * ```
|
36 | *
|
37 | * If you want to experiment with this extension, try the <a href="../../extensionsJSM/LocalStorageCommandHandler.html">Local Storage Commands</a> sample.
|
38 | * @category Extension
|
39 | */
|
40 | export class LocalStorageCommandHandler extends go.CommandHandler {
|
41 | private StorageKey: string = 'go._clipboard';
|
42 | private FormatKey: string = 'go._clipboardFormat';
|
43 |
|
44 | /**
|
45 | * Makes a copy of the given collection of {@link Part}s
|
46 | * and stores it as JSON in LocalStorage.
|
47 | * @param {Iterable.<Part>} coll a collection of {@link Part}s.
|
48 | */
|
49 | public copyToClipboard(coll: go.Iterable<go.Part>): void {
|
50 | try {
|
51 | if (coll === null) {
|
52 | window.localStorage.setItem(this.StorageKey, '');
|
53 | window.localStorage.setItem(this.FormatKey, '');
|
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 |
|
64 | window.localStorage.setItem(this.StorageKey, clipdiag.model.toJson());
|
65 | window.localStorage.setItem(this.FormatKey, clipdiag.model.dataFormat);
|
66 | }
|
67 | } catch (ex) {
|
68 | // fallback implementation
|
69 | super.copyToClipboard(coll);
|
70 | }
|
71 | }
|
72 |
|
73 | /**
|
74 | * If LocalStorage holds JSON for a collection of {@link Part}s,
|
75 | * and if the {@link Model#dataFormat} matches that stored in the clipboard,
|
76 | * this makes a copy of the clipboard's parts and adds the copies to this {@link Diagram}.
|
77 | * @return {Set.<Part>} a collection of newly pasted {@link Part}s
|
78 | */
|
79 | public pasteFromClipboard(): go.Set<go.Part> {
|
80 | const coll = new go.Set<go.Part>();
|
81 | try {
|
82 | const clipstr = window.localStorage.getItem(this.StorageKey);
|
83 | const clipfrmt = window.localStorage.getItem(this.FormatKey);
|
84 | if (clipstr === null || clipstr === '' || clipfrmt !== this.diagram.model.dataFormat) {
|
85 | return coll;
|
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<go.Part>();
|
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<go.Part>().addAll(copymap.iteratorValues);
|
96 | }
|
97 | } catch (ex) {
|
98 | // fallback implementation
|
99 | return super.pasteFromClipboard();
|
100 | }
|
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 | public canPasteSelection(pos?: go.Point): boolean {
|
109 | const diagram = this.diagram;
|
110 | if (diagram.isReadOnly || diagram.isModelReadOnly) return false;
|
111 | if (!diagram.allowInsert || !diagram.allowClipboard) return false;
|
112 | try {
|
113 | const clipstr = window.localStorage.getItem(this.StorageKey);
|
114 | const clipfrmt = window.localStorage.getItem(this.FormatKey);
|
115 | if (clipstr === null || clipstr === '') return false;
|
116 | if (clipfrmt !== diagram.model.dataFormat) return false;
|
117 | return true;
|
118 | } catch (ex) {
|
119 | // fallback implementation
|
120 | return go.CommandHandler.prototype.canPasteSelection(pos);
|
121 | }
|
122 | }
|
123 | }
|