@itwin/core-markup 3.4.0-dev.43 → 3.4.0-dev.44

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 (50) hide show
  1. package/lib/cjs/Markup.d.ts +310 -310
  2. package/lib/cjs/Markup.js +410 -410
  3. package/lib/cjs/Markup.js.map +1 -1
  4. package/lib/cjs/MarkupTool.d.ts +38 -38
  5. package/lib/cjs/MarkupTool.js +81 -81
  6. package/lib/cjs/MarkupTool.js.map +1 -1
  7. package/lib/cjs/RedlineTool.d.ts +145 -145
  8. package/lib/cjs/RedlineTool.js +497 -497
  9. package/lib/cjs/RedlineTool.js.map +1 -1
  10. package/lib/cjs/SelectTool.d.ts +126 -126
  11. package/lib/cjs/SelectTool.js +717 -717
  12. package/lib/cjs/SelectTool.js.map +1 -1
  13. package/lib/cjs/SvgJsExt.d.ts +85 -85
  14. package/lib/cjs/SvgJsExt.js +185 -185
  15. package/lib/cjs/SvgJsExt.js.map +1 -1
  16. package/lib/cjs/TextEdit.d.ts +43 -43
  17. package/lib/cjs/TextEdit.js +184 -184
  18. package/lib/cjs/TextEdit.js.map +1 -1
  19. package/lib/cjs/Undo.d.ts +46 -46
  20. package/lib/cjs/Undo.js +141 -141
  21. package/lib/cjs/Undo.js.map +1 -1
  22. package/lib/cjs/core-markup.d.ts +18 -18
  23. package/lib/cjs/core-markup.js +34 -34
  24. package/lib/cjs/core-markup.js.map +1 -1
  25. package/lib/esm/Markup.d.ts +310 -310
  26. package/lib/esm/Markup.js +405 -405
  27. package/lib/esm/Markup.js.map +1 -1
  28. package/lib/esm/MarkupTool.d.ts +38 -38
  29. package/lib/esm/MarkupTool.js +77 -77
  30. package/lib/esm/MarkupTool.js.map +1 -1
  31. package/lib/esm/RedlineTool.d.ts +145 -145
  32. package/lib/esm/RedlineTool.js +483 -483
  33. package/lib/esm/RedlineTool.js.map +1 -1
  34. package/lib/esm/SelectTool.d.ts +126 -126
  35. package/lib/esm/SelectTool.js +710 -710
  36. package/lib/esm/SelectTool.js.map +1 -1
  37. package/lib/esm/SvgJsExt.d.ts +85 -85
  38. package/lib/esm/SvgJsExt.js +180 -180
  39. package/lib/esm/SvgJsExt.js.map +1 -1
  40. package/lib/esm/TextEdit.d.ts +43 -43
  41. package/lib/esm/TextEdit.js +179 -179
  42. package/lib/esm/TextEdit.js.map +1 -1
  43. package/lib/esm/Undo.d.ts +46 -46
  44. package/lib/esm/Undo.js +137 -137
  45. package/lib/esm/Undo.js.map +1 -1
  46. package/lib/esm/core-markup.d.ts +18 -18
  47. package/lib/esm/core-markup.js +22 -22
  48. package/lib/esm/core-markup.js.map +1 -1
  49. package/lib/public/locales/en/MarkupTools.json +98 -98
  50. package/package.json +13 -13
package/lib/esm/Markup.js CHANGED
@@ -1,406 +1,406 @@
1
- /*---------------------------------------------------------------------------------------------
2
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
- * See LICENSE.md in the project root for license terms and full copyright notice.
4
- *--------------------------------------------------------------------------------------------*/
5
- /** @packageDocumentation
6
- * @module MarkupApp
7
- */
8
- import { BentleyError, Logger } from "@itwin/core-bentley";
9
- import { Point3d } from "@itwin/core-geometry";
10
- import { ImageSource, ImageSourceFormat } from "@itwin/core-common";
11
- import { FrontendLoggerCategory, imageElementFromImageSource, IModelApp, ScreenViewport } from "@itwin/core-frontend";
12
- import { adopt, create, Matrix, Point, SVG } from "@svgdotjs/svg.js";
13
- import * as redlineTool from "./RedlineTool";
14
- import { MarkupSelected, SelectTool } from "./SelectTool";
15
- import * as textTool from "./TextEdit";
16
- import { UndoManager } from "./Undo";
17
- /**
18
- * The main object for the Markup package. It is a singleton that stores the state of the Markup application.
19
- * It has only static members and methods. Applications may customize and control the behavior of the Markup by
20
- * setting members of [[MarkupApp.props]]. When [[MarkupApp.start]] is first called, it registers a set of "Markup.xxx"
21
- * tools that may be invoked from UI controls.
22
- * @public
23
- */
24
- export class MarkupApp {
25
- /** @internal */
26
- static screenToVbMtx() {
27
- var _a, _b;
28
- const matrix = (_b = (_a = this.markup) === null || _a === void 0 ? void 0 : _a.svgMarkup) === null || _b === void 0 ? void 0 : _b.screenCTM().inverse();
29
- return (undefined !== matrix ? matrix : new Matrix());
30
- }
31
- /** @internal */
32
- static getVpToScreenMtx() { const rect = this.markup.markupDiv.getBoundingClientRect(); return (new Matrix()).translateO(rect.left, rect.top); }
33
- /** @internal */
34
- static getVpToVbMtx() { return this.getVpToScreenMtx().lmultiplyO(this.screenToVbMtx()); }
35
- /** @internal */
36
- static convertVpToVb(pt) {
37
- const pt0 = new Point(pt.x, pt.y);
38
- pt0.transformO(this.getVpToVbMtx());
39
- return new Point3d(pt0.x, pt0.y, 0);
40
- }
41
- /** determine whether there's a markup session currently active */
42
- static get isActive() { return undefined !== this.markup; }
43
- static createMarkup(view, markupData) { return new Markup(view, markupData); }
44
- static lockViewportSize(view, markupData) {
45
- const parentDiv = view.vpDiv;
46
- const rect = parentDiv.getBoundingClientRect();
47
- let width = rect.width;
48
- let height = rect.height;
49
- if (markupData) {
50
- const aspect = markupData.rect.height / markupData.rect.width;
51
- if ((width * aspect) > height)
52
- width = Math.floor(height / aspect);
53
- else
54
- height = Math.floor(width * aspect);
55
- }
56
- const style = parentDiv.style;
57
- style.width = `${width}px`;
58
- style.height = `${height}px`;
59
- }
60
- /** @internal */
61
- static getActionName(action) { return IModelApp.localization.getLocalizedString(`${this.namespace}:actions.${action}`); }
62
- /** Start a markup session */
63
- static async start(view, markupData) {
64
- if (this.markup)
65
- return; // a markup session is already active.
66
- await this.initialize();
67
- // first, lock the viewport to its current size while the markup session is running
68
- this.lockViewportSize(view, markupData);
69
- this.markup = this.createMarkup(view, markupData); // start a markup against the provided view.
70
- if (!this.markup.svgMarkup) {
71
- ScreenViewport.setToParentSize(this.markup.vp.vpDiv);
72
- this.markup.markupDiv.remove();
73
- return;
74
- }
75
- IModelApp.toolAdmin.markupView = view; // so viewing tools won't operate on the view.
76
- // set the markup Select tool as the default tool and start it, saving current default tool
77
- this._saveDefaultToolId = IModelApp.toolAdmin.defaultToolId;
78
- IModelApp.toolAdmin.defaultToolId = this.markupSelectToolId;
79
- return IModelApp.toolAdmin.startDefaultTool();
80
- }
81
- /** Read the result of a Markup session, then stop the session.
82
- * @note see [MarkupApp.props.result] for options.
83
- */
84
- static async stop() {
85
- const data = await this.readMarkup();
86
- if (!this.markup)
87
- return data;
88
- // restore original size for vp.
89
- ScreenViewport.setToParentSize(this.markup.vp.vpDiv);
90
- IModelApp.toolAdmin.markupView = undefined; // re-enable viewing tools for the view being marked-up
91
- this.markup.destroy();
92
- this.markup = undefined;
93
- // now restore the default tool and start it
94
- IModelApp.toolAdmin.defaultToolId = this._saveDefaultToolId;
95
- this._saveDefaultToolId = "";
96
- await IModelApp.toolAdmin.startDefaultTool();
97
- return data;
98
- }
99
- /** Call this method to initialize the Markup system.
100
- * It asynchronously loads the MarkupTools namespace for the prompts and tool names for the Markup system, and
101
- * also registers all of the Markup tools.
102
- * @return a Promise that may be awaited to ensure that the MarkupTools namespace had been loaded.
103
- * @note This method is automatically called every time you call [[start]]. Since the Markup tools cannot
104
- * start unless there is a Markup active, there's really no need to call this method directly.
105
- * The only virtue in doing so is to pre-load the Markup namespace if you have an opportunity to do so earlier in your
106
- * startup code.
107
- * @note This method may be called multiple times, but only the first time initiates the loading/registering. Subsequent
108
- * calls return the same Promise.
109
- */
110
- static async initialize() {
111
- if (undefined === this.namespace) { // only need to do this once
112
- this.namespace = "MarkupTools";
113
- const namespacePromise = IModelApp.localization.registerNamespace(this.namespace);
114
- IModelApp.tools.register(SelectTool, this.namespace);
115
- IModelApp.tools.registerModule(redlineTool, this.namespace);
116
- IModelApp.tools.registerModule(textTool, this.namespace);
117
- return namespacePromise;
118
- }
119
- return IModelApp.localization.getNamespacePromise(this.namespace); // so caller can make sure localized messages are ready.
120
- }
121
- /** convert the current markup SVG into a string, but don't include decorations or dynamics
122
- * @internal
123
- */
124
- static readMarkupSvg() {
125
- const markup = this.markup;
126
- if (!markup || !markup.svgContainer)
127
- return undefined;
128
- markup.svgDecorations.remove(); // we don't want the decorations or dynamics to be included
129
- markup.svgDynamics.remove();
130
- void IModelApp.toolAdmin.startDefaultTool();
131
- return markup.svgContainer.svg(); // string-ize the SVG data
132
- }
133
- /** convert the current markup SVG into a string (after calling readMarkupSvg) making sure width and height are specified.
134
- * @internal
135
- */
136
- static readMarkupSvgForDrawImage() {
137
- const markup = this.markup;
138
- if (!markup || !markup.svgContainer)
139
- return undefined;
140
- // Firefox requires width and height on top-level svg or drawImage does nothing, passing width/height to drawImage doesn't work.
141
- const rect = markup.markupDiv.getBoundingClientRect();
142
- markup.svgContainer.width(rect.width);
143
- markup.svgContainer.height(rect.height);
144
- return markup.svgContainer.svg(); // string-ize the SVG data
145
- }
146
- /** @internal */
147
- static async readMarkup() {
148
- const result = this.props.result;
149
- let canvas = this.markup.vp.readImageToCanvas();
150
- let svg, image;
151
- try {
152
- svg = this.readMarkupSvg(); // read the current svg data for the markup
153
- const svgForImage = (svg && result.imprintSvgOnImage ? this.readMarkupSvgForDrawImage() : undefined);
154
- if (svgForImage) {
155
- const svgImage = await imageElementFromImageSource(new ImageSource(svgForImage, ImageSourceFormat.Svg));
156
- canvas.getContext("2d").drawImage(svgImage, 0, 0); // draw markup svg onto view's canvas2d
157
- }
158
- // is the source view too wide? If so, we need to scale the image down.
159
- if (canvas.width > result.maxWidth) {
160
- // yes, we have to scale it down, create a new canvas and set the new canvas' size
161
- const newCanvas = document.createElement("canvas");
162
- newCanvas.width = result.maxWidth;
163
- newCanvas.height = canvas.height * (result.maxWidth / canvas.width);
164
- newCanvas.getContext("2d").drawImage(canvas, 0, 0, canvas.width, canvas.height, 0, 0, newCanvas.width, newCanvas.height);
165
- canvas = newCanvas; // return the image from adjusted canvas, not view canvas.
166
- }
167
- // return the markup data to be saved by the application.
168
- image = (!result.imageFormat ? undefined : canvas.toDataURL(result.imageFormat));
169
- }
170
- catch (e) {
171
- Logger.logError(`${FrontendLoggerCategory.Package}.markup`, "Error creating image from svg", BentleyError.getErrorProps(e));
172
- }
173
- return { rect: { width: canvas.width, height: canvas.height }, svg, image };
174
- }
175
- /** @internal */
176
- static get dropShadowId() { return `${this.markupPrefix}dropShadow`; } // this is referenced in the markup Svg to apply the drop-shadow filter to all markup elements.
177
- /** @internal */
178
- static get cornerId() { return `${this.markupPrefix}photoCorner`; }
179
- /** @internal */
180
- static get containerClass() { return `${this.markupPrefix}container`; }
181
- /** @internal */
182
- static get dynamicsClass() { return `${this.markupPrefix}dynamics`; }
183
- /** @internal */
184
- static get decorationsClass() { return `${this.markupPrefix}decorations`; }
185
- /** @internal */
186
- static get markupSvgClass() { return `${this.markupPrefix}svg`; }
187
- /** @internal */
188
- static get boxedTextClass() { return `${this.markupPrefix}boxedText`; }
189
- /** @internal */
190
- static get textClass() { return `${this.markupPrefix}text`; }
191
- /** @internal */
192
- static get stretchHandleClass() { return `${this.markupPrefix}stretchHandle`; }
193
- /** @internal */
194
- static get rotateLineClass() { return `${this.markupPrefix}rotateLine`; }
195
- /** @internal */
196
- static get rotateHandleClass() { return `${this.markupPrefix}rotateHandle`; }
197
- /** @internal */
198
- static get vertexHandleClass() { return `${this.markupPrefix}vertexHandle`; }
199
- /** @internal */
200
- static get moveHandleClass() { return `${this.markupPrefix}moveHandle`; }
201
- /** @internal */
202
- static get textOutlineClass() { return `${this.markupPrefix}textOutline`; }
203
- /** @internal */
204
- static get textEditorClass() { return `${this.markupPrefix}textEditor`; }
205
- }
206
- /** By setting members of this object, applications can control the appearance and behavior of various parts of MarkupApp. */
207
- MarkupApp.props = {
208
- /** the UI controls displayed on Elements by the Select Tool to allow users to modify them. */
209
- handles: {
210
- /** The diameter of the circles for the handles. */
211
- size: 10,
212
- /** The attributes of the stretch handles */
213
- stretch: { "fill-opacity": .85, "stroke": "black", "fill": "white" },
214
- /** The attributes of the line that connects the top-center stretch handle to the rotate handle. */
215
- rotateLine: { "stroke": "grey", "fill-opacity": .85 },
216
- /** The attributes of the rotate handle. */
217
- rotate: { "cursor": `url(${IModelApp.publicPath}Markup/rotate.png) 12 12, auto`, "fill-opacity": .85, "stroke": "black", "fill": "lightBlue" },
218
- /** The attributes of box around the element. */
219
- moveOutline: { "cursor": "move", "stroke-dasharray": "6,6", "fill": "none", "stroke-opacity": .85, "stroke": "white" },
220
- /** The attributes of box that provides the move cursor. */
221
- move: { "cursor": "move", "opacity": 0, "stroke-width": 10, "stroke": "white" },
222
- /** The attributes of handles on the vertices of lines. */
223
- vertex: { "cursor": `url(${IModelApp.publicPath}cursors/crosshair.cur), crosshair`, "fill-opacity": .85, "stroke": "black", "fill": "white" },
224
- },
225
- /** properties for providing feedback about selected elements. */
226
- hilite: {
227
- /** the color of selected elements */
228
- color: "magenta",
229
- /** the color of an element as the cursor passes over it */
230
- flash: "cyan",
231
- },
232
- /** optionally, show a drop-shadow behind all markup elements. */
233
- dropShadow: {
234
- /** if false, no drop shadow */
235
- enable: true,
236
- /** the attributes of the drop shadow. See https://developer.mozilla.org/en-US/docs/Web/SVG/Element/feDropShadow */
237
- attr: {
238
- "stdDeviation": 2,
239
- "dx": 1.2,
240
- "dy": 1.4,
241
- "flood-color": "#1B3838",
242
- },
243
- },
244
- /** The "active placement" parameters. New elements are created with these parameters, so UI controls should set them. */
245
- active: {
246
- /** the CSS style properties of new text elements. */
247
- text: {
248
- "font-family": "sans-serif",
249
- "font-size": "30px",
250
- "stroke": "none",
251
- "fill": "red",
252
- },
253
- /** the CSS style properties of new elements. */
254
- element: {
255
- "stroke": "red",
256
- "stroke-opacity": 0.8,
257
- "stroke-width": 3,
258
- "stroke-dasharray": 0,
259
- "stroke-linecap": "round",
260
- "stroke-linejoin": "round",
261
- "fill": "blue",
262
- "fill-opacity": 0.2,
263
- },
264
- arrow: {
265
- length: 7,
266
- width: 6,
267
- },
268
- cloud: {
269
- path: "M3.0,2.5 C3.9,.78 5.6,-.4 8.1,1.0 C9.1,0 11.3,-.2 12.5,.5 C14.2,-.5 17,.16 17.9,2.5 C21,3 20.2,7.3 17.6,7.5 C16.5,9.2 14.4,9.8 12.7,8.9 C11.6,10 9.5,10.3 8.1,9.4 C5.7,10.8 3.3,9.4 2.6,7.5 C-.9,7.7 .6,1.7 3.0,2.5z",
270
- },
271
- },
272
- /** Values for placing and editing Text. */
273
- text: {
274
- /** A default string for the Markup.Text.Place command. Applications can turn this off, or supply the user's initials, for example. */
275
- startValue: "Note: ",
276
- /** Parameters for the size and appearance of the text editor */
277
- edit: {
278
- background: "blanchedalmond",
279
- /** Starting size, will be updated if user stretches the box */
280
- size: { width: "25%", height: "4em" },
281
- /** font size of the text editor */
282
- fontSize: "14pt",
283
- /** A background box drawn around text so user can tell what's being selected */
284
- textBox: { "fill": "lightGrey", "fill-opacity": .1, "stroke-opacity": .85, "stroke": "lightBlue" },
285
- },
286
- },
287
- /** Used to draw the border outline around the view while it is being marked up so the user can tell Markup is active */
288
- borderOutline: {
289
- "stroke": "gold",
290
- "stroke-width": 6,
291
- "stroke-opacity": 0.4,
292
- "fill": "none",
293
- },
294
- /** Used to draw the border corner symbols for the view while it is being marked up so the user can tell Markup is active */
295
- borderCorners: {
296
- "stroke": "black",
297
- "stroke-width": 2,
298
- "stroke-opacity": 0.2,
299
- "fill": "gold",
300
- "fill-opacity": 0.2,
301
- },
302
- /** Determines what is returned by MarkupApp.stop */
303
- result: {
304
- /** The format for the image data. */
305
- imageFormat: "image/png",
306
- /** If true, the markup graphics will be imprinted in the returned image. */
307
- imprintSvgOnImage: true,
308
- /** the maximum width for the returned image. If the source view width is larger than this, it will be scaled down to this size. */
309
- maxWidth: 2048,
310
- },
311
- };
312
- MarkupApp._saveDefaultToolId = "";
313
- MarkupApp.markupSelectToolId = "Markup.Select";
314
- /** @internal */
315
- MarkupApp.markupPrefix = "markup-";
316
- const removeSvgNamespace = (svg) => { svg.node.removeAttribute("xmlns:svgjs"); return svg; };
317
- const newSvgElement = (name) => adopt(create(name));
318
- /**
319
- * The current markup being created/edited. Holds the SVG elements, plus the active [[MarkupTool]].
320
- * When starting a Markup, a new Div is added as a child of the ScreenViewport's vpDiv.
321
- * @public
322
- */
323
- export class Markup {
324
- /** Create a new Markup for the supplied ScreenViewport. Adds a new "overlay-markup" div into the "vpDiv"
325
- * of the viewport.
326
- * @note you must call destroy on this object at end of markup to remove the markup div.
327
- */
328
- constructor(vp, markupData) {
329
- this.vp = vp;
330
- /** @internal */
331
- this.undo = new UndoManager();
332
- this.markupDiv = vp.addNewDiv("overlay-markup", true, 20); // this div goes on top of the canvas, but behind UI layers
333
- const rect = this.markupDiv.getBoundingClientRect();
334
- // First, see if there is a markup passed in as an argument
335
- if (markupData && markupData.svg) {
336
- this.markupDiv.innerHTML = markupData.svg; // make it a child of the markupDiv
337
- this.svgContainer = SVG(`.${MarkupApp.containerClass}`); // get it in svg.js format
338
- this.svgMarkup = SVG(`.${MarkupApp.markupSvgClass}`);
339
- if (!this.svgContainer || !this.svgMarkup) // if either isn't present, its not a valid markup
340
- return;
341
- removeSvgNamespace(this.svgContainer); // the SVG call above adds this - remove it
342
- this.svgMarkup.each(() => { }, true); // create an SVG.Element for each entry in the supplied markup.
343
- }
344
- else {
345
- // create the container that will be returned as the "svg" data for this markup
346
- this.svgContainer = SVG().addTo(this.markupDiv).addClass(MarkupApp.containerClass).viewbox(0, 0, rect.width, rect.height);
347
- removeSvgNamespace(this.svgContainer);
348
- this.svgMarkup = this.addNested(MarkupApp.markupSvgClass);
349
- }
350
- if (MarkupApp.props.dropShadow.enable) {
351
- this.createDropShadow(this.svgContainer);
352
- this.svgContainer.attr("filter", `url(#${MarkupApp.dropShadowId})`);
353
- }
354
- /** add two nested groups for providing feedback during the markup session. These Svgs are removed before the data is returned. */
355
- this.svgDynamics = this.addNested(MarkupApp.dynamicsClass); // only for tool dynamics of SVG graphics.
356
- this.svgDecorations = this.addNested(MarkupApp.decorationsClass); // only for temporary decorations of SVG graphics.
357
- this.addBorder();
358
- this.selected = new MarkupSelected(this.svgDecorations);
359
- }
360
- /** create the drop-shadow filter in the Defs section of the supplied svg element */
361
- createDropShadow(svg) {
362
- const filter = SVG(`#${MarkupApp.dropShadowId}`); // see if we already have one?
363
- if (filter)
364
- filter.remove(); // yes, remove it. This must be someone modifying the drop shadow properties
365
- // create a new filter, and add it to the Defs of the supplied svg
366
- svg.defs()
367
- .add(newSvgElement("filter").id(MarkupApp.dropShadowId)
368
- .add(newSvgElement("feDropShadow").attr(MarkupApp.props.dropShadow.attr)));
369
- }
370
- addNested(className) { return this.svgContainer.group().addClass(className); }
371
- addBorder() {
372
- const rect = this.svgContainer.viewbox();
373
- const inset = MarkupApp.props.borderOutline["stroke-width"];
374
- const cornerSize = inset * 6;
375
- const cornerPts = [0, 0, cornerSize, 0, cornerSize * .7, cornerSize * .3, cornerSize * .3, cornerSize * .3, cornerSize * .3, cornerSize * .7, 0, cornerSize];
376
- const decorations = this.svgDecorations;
377
- const photoCorner = decorations.symbol().polygon(cornerPts).attr(MarkupApp.props.borderCorners).id(MarkupApp.cornerId);
378
- const cornerGroup = decorations.group();
379
- cornerGroup.rect(rect.width - inset, rect.height - inset).move(inset / 2, inset / 2).attr(MarkupApp.props.borderOutline);
380
- cornerGroup.use(photoCorner);
381
- cornerGroup.use(photoCorner).rotate(90).translate(rect.width - cornerSize, 0);
382
- cornerGroup.use(photoCorner).rotate(180).translate(rect.width - cornerSize, rect.height - cornerSize);
383
- cornerGroup.use(photoCorner).rotate(270).translate(0, rect.height - cornerSize);
384
- }
385
- /** Called when the Markup is destroyed */
386
- destroy() { this.markupDiv.remove(); }
387
- /** Turn on picking the markup elements in the markup view */
388
- enablePick() { this.markupDiv.style.pointerEvents = "auto"; }
389
- /** Turn off picking the markup elements in the markup view */
390
- disablePick() { this.markupDiv.style.pointerEvents = "none"; }
391
- /** Change the default cursor for the markup view */
392
- setCursor(cursor) { this.markupDiv.style.cursor = cursor; }
393
- /** Delete all the entries in the selection set, then empty it. */
394
- deleteSelected() { this.selected.deleteAll(this.undo); }
395
- /** Bring all the entries in the selection set to the front. */
396
- bringToFront() { this.selected.reposition(MarkupApp.getActionName("toFront"), this.undo, (el) => el.front()); }
397
- /** Send all the entries in the selection set to the back. */
398
- sendToBack() { this.selected.reposition(MarkupApp.getActionName("toBack"), this.undo, (el) => el.back()); }
399
- /** Group all the entries in the selection set, then select the group. */
400
- groupSelected() { if (undefined !== this.svgMarkup)
401
- this.selected.groupAll(this.undo); }
402
- /** Ungroup all the group entries in the selection set. */
403
- ungroupSelected() { if (undefined !== this.svgMarkup)
404
- this.selected.ungroupAll(this.undo); }
405
- }
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
+ * See LICENSE.md in the project root for license terms and full copyright notice.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ /** @packageDocumentation
6
+ * @module MarkupApp
7
+ */
8
+ import { BentleyError, Logger } from "@itwin/core-bentley";
9
+ import { Point3d } from "@itwin/core-geometry";
10
+ import { ImageSource, ImageSourceFormat } from "@itwin/core-common";
11
+ import { FrontendLoggerCategory, imageElementFromImageSource, IModelApp, ScreenViewport } from "@itwin/core-frontend";
12
+ import { adopt, create, Matrix, Point, SVG } from "@svgdotjs/svg.js";
13
+ import * as redlineTool from "./RedlineTool";
14
+ import { MarkupSelected, SelectTool } from "./SelectTool";
15
+ import * as textTool from "./TextEdit";
16
+ import { UndoManager } from "./Undo";
17
+ /**
18
+ * The main object for the Markup package. It is a singleton that stores the state of the Markup application.
19
+ * It has only static members and methods. Applications may customize and control the behavior of the Markup by
20
+ * setting members of [[MarkupApp.props]]. When [[MarkupApp.start]] is first called, it registers a set of "Markup.xxx"
21
+ * tools that may be invoked from UI controls.
22
+ * @public
23
+ */
24
+ export class MarkupApp {
25
+ /** @internal */
26
+ static screenToVbMtx() {
27
+ var _a, _b;
28
+ const matrix = (_b = (_a = this.markup) === null || _a === void 0 ? void 0 : _a.svgMarkup) === null || _b === void 0 ? void 0 : _b.screenCTM().inverse();
29
+ return (undefined !== matrix ? matrix : new Matrix());
30
+ }
31
+ /** @internal */
32
+ static getVpToScreenMtx() { const rect = this.markup.markupDiv.getBoundingClientRect(); return (new Matrix()).translateO(rect.left, rect.top); }
33
+ /** @internal */
34
+ static getVpToVbMtx() { return this.getVpToScreenMtx().lmultiplyO(this.screenToVbMtx()); }
35
+ /** @internal */
36
+ static convertVpToVb(pt) {
37
+ const pt0 = new Point(pt.x, pt.y);
38
+ pt0.transformO(this.getVpToVbMtx());
39
+ return new Point3d(pt0.x, pt0.y, 0);
40
+ }
41
+ /** determine whether there's a markup session currently active */
42
+ static get isActive() { return undefined !== this.markup; }
43
+ static createMarkup(view, markupData) { return new Markup(view, markupData); }
44
+ static lockViewportSize(view, markupData) {
45
+ const parentDiv = view.vpDiv;
46
+ const rect = parentDiv.getBoundingClientRect();
47
+ let width = rect.width;
48
+ let height = rect.height;
49
+ if (markupData) {
50
+ const aspect = markupData.rect.height / markupData.rect.width;
51
+ if ((width * aspect) > height)
52
+ width = Math.floor(height / aspect);
53
+ else
54
+ height = Math.floor(width * aspect);
55
+ }
56
+ const style = parentDiv.style;
57
+ style.width = `${width}px`;
58
+ style.height = `${height}px`;
59
+ }
60
+ /** @internal */
61
+ static getActionName(action) { return IModelApp.localization.getLocalizedString(`${this.namespace}:actions.${action}`); }
62
+ /** Start a markup session */
63
+ static async start(view, markupData) {
64
+ if (this.markup)
65
+ return; // a markup session is already active.
66
+ await this.initialize();
67
+ // first, lock the viewport to its current size while the markup session is running
68
+ this.lockViewportSize(view, markupData);
69
+ this.markup = this.createMarkup(view, markupData); // start a markup against the provided view.
70
+ if (!this.markup.svgMarkup) {
71
+ ScreenViewport.setToParentSize(this.markup.vp.vpDiv);
72
+ this.markup.markupDiv.remove();
73
+ return;
74
+ }
75
+ IModelApp.toolAdmin.markupView = view; // so viewing tools won't operate on the view.
76
+ // set the markup Select tool as the default tool and start it, saving current default tool
77
+ this._saveDefaultToolId = IModelApp.toolAdmin.defaultToolId;
78
+ IModelApp.toolAdmin.defaultToolId = this.markupSelectToolId;
79
+ return IModelApp.toolAdmin.startDefaultTool();
80
+ }
81
+ /** Read the result of a Markup session, then stop the session.
82
+ * @note see [MarkupApp.props.result] for options.
83
+ */
84
+ static async stop() {
85
+ const data = await this.readMarkup();
86
+ if (!this.markup)
87
+ return data;
88
+ // restore original size for vp.
89
+ ScreenViewport.setToParentSize(this.markup.vp.vpDiv);
90
+ IModelApp.toolAdmin.markupView = undefined; // re-enable viewing tools for the view being marked-up
91
+ this.markup.destroy();
92
+ this.markup = undefined;
93
+ // now restore the default tool and start it
94
+ IModelApp.toolAdmin.defaultToolId = this._saveDefaultToolId;
95
+ this._saveDefaultToolId = "";
96
+ await IModelApp.toolAdmin.startDefaultTool();
97
+ return data;
98
+ }
99
+ /** Call this method to initialize the Markup system.
100
+ * It asynchronously loads the MarkupTools namespace for the prompts and tool names for the Markup system, and
101
+ * also registers all of the Markup tools.
102
+ * @return a Promise that may be awaited to ensure that the MarkupTools namespace had been loaded.
103
+ * @note This method is automatically called every time you call [[start]]. Since the Markup tools cannot
104
+ * start unless there is a Markup active, there's really no need to call this method directly.
105
+ * The only virtue in doing so is to pre-load the Markup namespace if you have an opportunity to do so earlier in your
106
+ * startup code.
107
+ * @note This method may be called multiple times, but only the first time initiates the loading/registering. Subsequent
108
+ * calls return the same Promise.
109
+ */
110
+ static async initialize() {
111
+ if (undefined === this.namespace) { // only need to do this once
112
+ this.namespace = "MarkupTools";
113
+ const namespacePromise = IModelApp.localization.registerNamespace(this.namespace);
114
+ IModelApp.tools.register(SelectTool, this.namespace);
115
+ IModelApp.tools.registerModule(redlineTool, this.namespace);
116
+ IModelApp.tools.registerModule(textTool, this.namespace);
117
+ return namespacePromise;
118
+ }
119
+ return IModelApp.localization.getNamespacePromise(this.namespace); // so caller can make sure localized messages are ready.
120
+ }
121
+ /** convert the current markup SVG into a string, but don't include decorations or dynamics
122
+ * @internal
123
+ */
124
+ static readMarkupSvg() {
125
+ const markup = this.markup;
126
+ if (!markup || !markup.svgContainer)
127
+ return undefined;
128
+ markup.svgDecorations.remove(); // we don't want the decorations or dynamics to be included
129
+ markup.svgDynamics.remove();
130
+ void IModelApp.toolAdmin.startDefaultTool();
131
+ return markup.svgContainer.svg(); // string-ize the SVG data
132
+ }
133
+ /** convert the current markup SVG into a string (after calling readMarkupSvg) making sure width and height are specified.
134
+ * @internal
135
+ */
136
+ static readMarkupSvgForDrawImage() {
137
+ const markup = this.markup;
138
+ if (!markup || !markup.svgContainer)
139
+ return undefined;
140
+ // Firefox requires width and height on top-level svg or drawImage does nothing, passing width/height to drawImage doesn't work.
141
+ const rect = markup.markupDiv.getBoundingClientRect();
142
+ markup.svgContainer.width(rect.width);
143
+ markup.svgContainer.height(rect.height);
144
+ return markup.svgContainer.svg(); // string-ize the SVG data
145
+ }
146
+ /** @internal */
147
+ static async readMarkup() {
148
+ const result = this.props.result;
149
+ let canvas = this.markup.vp.readImageToCanvas();
150
+ let svg, image;
151
+ try {
152
+ svg = this.readMarkupSvg(); // read the current svg data for the markup
153
+ const svgForImage = (svg && result.imprintSvgOnImage ? this.readMarkupSvgForDrawImage() : undefined);
154
+ if (svgForImage) {
155
+ const svgImage = await imageElementFromImageSource(new ImageSource(svgForImage, ImageSourceFormat.Svg));
156
+ canvas.getContext("2d").drawImage(svgImage, 0, 0); // draw markup svg onto view's canvas2d
157
+ }
158
+ // is the source view too wide? If so, we need to scale the image down.
159
+ if (canvas.width > result.maxWidth) {
160
+ // yes, we have to scale it down, create a new canvas and set the new canvas' size
161
+ const newCanvas = document.createElement("canvas");
162
+ newCanvas.width = result.maxWidth;
163
+ newCanvas.height = canvas.height * (result.maxWidth / canvas.width);
164
+ newCanvas.getContext("2d").drawImage(canvas, 0, 0, canvas.width, canvas.height, 0, 0, newCanvas.width, newCanvas.height);
165
+ canvas = newCanvas; // return the image from adjusted canvas, not view canvas.
166
+ }
167
+ // return the markup data to be saved by the application.
168
+ image = (!result.imageFormat ? undefined : canvas.toDataURL(result.imageFormat));
169
+ }
170
+ catch (e) {
171
+ Logger.logError(`${FrontendLoggerCategory.Package}.markup`, "Error creating image from svg", BentleyError.getErrorProps(e));
172
+ }
173
+ return { rect: { width: canvas.width, height: canvas.height }, svg, image };
174
+ }
175
+ /** @internal */
176
+ static get dropShadowId() { return `${this.markupPrefix}dropShadow`; } // this is referenced in the markup Svg to apply the drop-shadow filter to all markup elements.
177
+ /** @internal */
178
+ static get cornerId() { return `${this.markupPrefix}photoCorner`; }
179
+ /** @internal */
180
+ static get containerClass() { return `${this.markupPrefix}container`; }
181
+ /** @internal */
182
+ static get dynamicsClass() { return `${this.markupPrefix}dynamics`; }
183
+ /** @internal */
184
+ static get decorationsClass() { return `${this.markupPrefix}decorations`; }
185
+ /** @internal */
186
+ static get markupSvgClass() { return `${this.markupPrefix}svg`; }
187
+ /** @internal */
188
+ static get boxedTextClass() { return `${this.markupPrefix}boxedText`; }
189
+ /** @internal */
190
+ static get textClass() { return `${this.markupPrefix}text`; }
191
+ /** @internal */
192
+ static get stretchHandleClass() { return `${this.markupPrefix}stretchHandle`; }
193
+ /** @internal */
194
+ static get rotateLineClass() { return `${this.markupPrefix}rotateLine`; }
195
+ /** @internal */
196
+ static get rotateHandleClass() { return `${this.markupPrefix}rotateHandle`; }
197
+ /** @internal */
198
+ static get vertexHandleClass() { return `${this.markupPrefix}vertexHandle`; }
199
+ /** @internal */
200
+ static get moveHandleClass() { return `${this.markupPrefix}moveHandle`; }
201
+ /** @internal */
202
+ static get textOutlineClass() { return `${this.markupPrefix}textOutline`; }
203
+ /** @internal */
204
+ static get textEditorClass() { return `${this.markupPrefix}textEditor`; }
205
+ }
206
+ /** By setting members of this object, applications can control the appearance and behavior of various parts of MarkupApp. */
207
+ MarkupApp.props = {
208
+ /** the UI controls displayed on Elements by the Select Tool to allow users to modify them. */
209
+ handles: {
210
+ /** The diameter of the circles for the handles. */
211
+ size: 10,
212
+ /** The attributes of the stretch handles */
213
+ stretch: { "fill-opacity": .85, "stroke": "black", "fill": "white" },
214
+ /** The attributes of the line that connects the top-center stretch handle to the rotate handle. */
215
+ rotateLine: { "stroke": "grey", "fill-opacity": .85 },
216
+ /** The attributes of the rotate handle. */
217
+ rotate: { "cursor": `url(${IModelApp.publicPath}Markup/rotate.png) 12 12, auto`, "fill-opacity": .85, "stroke": "black", "fill": "lightBlue" },
218
+ /** The attributes of box around the element. */
219
+ moveOutline: { "cursor": "move", "stroke-dasharray": "6,6", "fill": "none", "stroke-opacity": .85, "stroke": "white" },
220
+ /** The attributes of box that provides the move cursor. */
221
+ move: { "cursor": "move", "opacity": 0, "stroke-width": 10, "stroke": "white" },
222
+ /** The attributes of handles on the vertices of lines. */
223
+ vertex: { "cursor": `url(${IModelApp.publicPath}cursors/crosshair.cur), crosshair`, "fill-opacity": .85, "stroke": "black", "fill": "white" },
224
+ },
225
+ /** properties for providing feedback about selected elements. */
226
+ hilite: {
227
+ /** the color of selected elements */
228
+ color: "magenta",
229
+ /** the color of an element as the cursor passes over it */
230
+ flash: "cyan",
231
+ },
232
+ /** optionally, show a drop-shadow behind all markup elements. */
233
+ dropShadow: {
234
+ /** if false, no drop shadow */
235
+ enable: true,
236
+ /** the attributes of the drop shadow. See https://developer.mozilla.org/en-US/docs/Web/SVG/Element/feDropShadow */
237
+ attr: {
238
+ "stdDeviation": 2,
239
+ "dx": 1.2,
240
+ "dy": 1.4,
241
+ "flood-color": "#1B3838",
242
+ },
243
+ },
244
+ /** The "active placement" parameters. New elements are created with these parameters, so UI controls should set them. */
245
+ active: {
246
+ /** the CSS style properties of new text elements. */
247
+ text: {
248
+ "font-family": "sans-serif",
249
+ "font-size": "30px",
250
+ "stroke": "none",
251
+ "fill": "red",
252
+ },
253
+ /** the CSS style properties of new elements. */
254
+ element: {
255
+ "stroke": "red",
256
+ "stroke-opacity": 0.8,
257
+ "stroke-width": 3,
258
+ "stroke-dasharray": 0,
259
+ "stroke-linecap": "round",
260
+ "stroke-linejoin": "round",
261
+ "fill": "blue",
262
+ "fill-opacity": 0.2,
263
+ },
264
+ arrow: {
265
+ length: 7,
266
+ width: 6,
267
+ },
268
+ cloud: {
269
+ path: "M3.0,2.5 C3.9,.78 5.6,-.4 8.1,1.0 C9.1,0 11.3,-.2 12.5,.5 C14.2,-.5 17,.16 17.9,2.5 C21,3 20.2,7.3 17.6,7.5 C16.5,9.2 14.4,9.8 12.7,8.9 C11.6,10 9.5,10.3 8.1,9.4 C5.7,10.8 3.3,9.4 2.6,7.5 C-.9,7.7 .6,1.7 3.0,2.5z",
270
+ },
271
+ },
272
+ /** Values for placing and editing Text. */
273
+ text: {
274
+ /** A default string for the Markup.Text.Place command. Applications can turn this off, or supply the user's initials, for example. */
275
+ startValue: "Note: ",
276
+ /** Parameters for the size and appearance of the text editor */
277
+ edit: {
278
+ background: "blanchedalmond",
279
+ /** Starting size, will be updated if user stretches the box */
280
+ size: { width: "25%", height: "4em" },
281
+ /** font size of the text editor */
282
+ fontSize: "14pt",
283
+ /** A background box drawn around text so user can tell what's being selected */
284
+ textBox: { "fill": "lightGrey", "fill-opacity": .1, "stroke-opacity": .85, "stroke": "lightBlue" },
285
+ },
286
+ },
287
+ /** Used to draw the border outline around the view while it is being marked up so the user can tell Markup is active */
288
+ borderOutline: {
289
+ "stroke": "gold",
290
+ "stroke-width": 6,
291
+ "stroke-opacity": 0.4,
292
+ "fill": "none",
293
+ },
294
+ /** Used to draw the border corner symbols for the view while it is being marked up so the user can tell Markup is active */
295
+ borderCorners: {
296
+ "stroke": "black",
297
+ "stroke-width": 2,
298
+ "stroke-opacity": 0.2,
299
+ "fill": "gold",
300
+ "fill-opacity": 0.2,
301
+ },
302
+ /** Determines what is returned by MarkupApp.stop */
303
+ result: {
304
+ /** The format for the image data. */
305
+ imageFormat: "image/png",
306
+ /** If true, the markup graphics will be imprinted in the returned image. */
307
+ imprintSvgOnImage: true,
308
+ /** the maximum width for the returned image. If the source view width is larger than this, it will be scaled down to this size. */
309
+ maxWidth: 2048,
310
+ },
311
+ };
312
+ MarkupApp._saveDefaultToolId = "";
313
+ MarkupApp.markupSelectToolId = "Markup.Select";
314
+ /** @internal */
315
+ MarkupApp.markupPrefix = "markup-";
316
+ const removeSvgNamespace = (svg) => { svg.node.removeAttribute("xmlns:svgjs"); return svg; };
317
+ const newSvgElement = (name) => adopt(create(name));
318
+ /**
319
+ * The current markup being created/edited. Holds the SVG elements, plus the active [[MarkupTool]].
320
+ * When starting a Markup, a new Div is added as a child of the ScreenViewport's vpDiv.
321
+ * @public
322
+ */
323
+ export class Markup {
324
+ /** Create a new Markup for the supplied ScreenViewport. Adds a new "overlay-markup" div into the "vpDiv"
325
+ * of the viewport.
326
+ * @note you must call destroy on this object at end of markup to remove the markup div.
327
+ */
328
+ constructor(vp, markupData) {
329
+ this.vp = vp;
330
+ /** @internal */
331
+ this.undo = new UndoManager();
332
+ this.markupDiv = vp.addNewDiv("overlay-markup", true, 20); // this div goes on top of the canvas, but behind UI layers
333
+ const rect = this.markupDiv.getBoundingClientRect();
334
+ // First, see if there is a markup passed in as an argument
335
+ if (markupData && markupData.svg) {
336
+ this.markupDiv.innerHTML = markupData.svg; // make it a child of the markupDiv
337
+ this.svgContainer = SVG(`.${MarkupApp.containerClass}`); // get it in svg.js format
338
+ this.svgMarkup = SVG(`.${MarkupApp.markupSvgClass}`);
339
+ if (!this.svgContainer || !this.svgMarkup) // if either isn't present, its not a valid markup
340
+ return;
341
+ removeSvgNamespace(this.svgContainer); // the SVG call above adds this - remove it
342
+ this.svgMarkup.each(() => { }, true); // create an SVG.Element for each entry in the supplied markup.
343
+ }
344
+ else {
345
+ // create the container that will be returned as the "svg" data for this markup
346
+ this.svgContainer = SVG().addTo(this.markupDiv).addClass(MarkupApp.containerClass).viewbox(0, 0, rect.width, rect.height);
347
+ removeSvgNamespace(this.svgContainer);
348
+ this.svgMarkup = this.addNested(MarkupApp.markupSvgClass);
349
+ }
350
+ if (MarkupApp.props.dropShadow.enable) {
351
+ this.createDropShadow(this.svgContainer);
352
+ this.svgContainer.attr("filter", `url(#${MarkupApp.dropShadowId})`);
353
+ }
354
+ /** add two nested groups for providing feedback during the markup session. These Svgs are removed before the data is returned. */
355
+ this.svgDynamics = this.addNested(MarkupApp.dynamicsClass); // only for tool dynamics of SVG graphics.
356
+ this.svgDecorations = this.addNested(MarkupApp.decorationsClass); // only for temporary decorations of SVG graphics.
357
+ this.addBorder();
358
+ this.selected = new MarkupSelected(this.svgDecorations);
359
+ }
360
+ /** create the drop-shadow filter in the Defs section of the supplied svg element */
361
+ createDropShadow(svg) {
362
+ const filter = SVG(`#${MarkupApp.dropShadowId}`); // see if we already have one?
363
+ if (filter)
364
+ filter.remove(); // yes, remove it. This must be someone modifying the drop shadow properties
365
+ // create a new filter, and add it to the Defs of the supplied svg
366
+ svg.defs()
367
+ .add(newSvgElement("filter").id(MarkupApp.dropShadowId)
368
+ .add(newSvgElement("feDropShadow").attr(MarkupApp.props.dropShadow.attr)));
369
+ }
370
+ addNested(className) { return this.svgContainer.group().addClass(className); }
371
+ addBorder() {
372
+ const rect = this.svgContainer.viewbox();
373
+ const inset = MarkupApp.props.borderOutline["stroke-width"];
374
+ const cornerSize = inset * 6;
375
+ const cornerPts = [0, 0, cornerSize, 0, cornerSize * .7, cornerSize * .3, cornerSize * .3, cornerSize * .3, cornerSize * .3, cornerSize * .7, 0, cornerSize];
376
+ const decorations = this.svgDecorations;
377
+ const photoCorner = decorations.symbol().polygon(cornerPts).attr(MarkupApp.props.borderCorners).id(MarkupApp.cornerId);
378
+ const cornerGroup = decorations.group();
379
+ cornerGroup.rect(rect.width - inset, rect.height - inset).move(inset / 2, inset / 2).attr(MarkupApp.props.borderOutline);
380
+ cornerGroup.use(photoCorner);
381
+ cornerGroup.use(photoCorner).rotate(90).translate(rect.width - cornerSize, 0);
382
+ cornerGroup.use(photoCorner).rotate(180).translate(rect.width - cornerSize, rect.height - cornerSize);
383
+ cornerGroup.use(photoCorner).rotate(270).translate(0, rect.height - cornerSize);
384
+ }
385
+ /** Called when the Markup is destroyed */
386
+ destroy() { this.markupDiv.remove(); }
387
+ /** Turn on picking the markup elements in the markup view */
388
+ enablePick() { this.markupDiv.style.pointerEvents = "auto"; }
389
+ /** Turn off picking the markup elements in the markup view */
390
+ disablePick() { this.markupDiv.style.pointerEvents = "none"; }
391
+ /** Change the default cursor for the markup view */
392
+ setCursor(cursor) { this.markupDiv.style.cursor = cursor; }
393
+ /** Delete all the entries in the selection set, then empty it. */
394
+ deleteSelected() { this.selected.deleteAll(this.undo); }
395
+ /** Bring all the entries in the selection set to the front. */
396
+ bringToFront() { this.selected.reposition(MarkupApp.getActionName("toFront"), this.undo, (el) => el.front()); }
397
+ /** Send all the entries in the selection set to the back. */
398
+ sendToBack() { this.selected.reposition(MarkupApp.getActionName("toBack"), this.undo, (el) => el.back()); }
399
+ /** Group all the entries in the selection set, then select the group. */
400
+ groupSelected() { if (undefined !== this.svgMarkup)
401
+ this.selected.groupAll(this.undo); }
402
+ /** Ungroup all the group entries in the selection set. */
403
+ ungroupSelected() { if (undefined !== this.svgMarkup)
404
+ this.selected.ungroupAll(this.undo); }
405
+ }
406
406
  //# sourceMappingURL=Markup.js.map