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