@operato/scene-pdf 9.0.0-beta.15 → 9.0.0
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.
- package/dist/pdf-page.d.ts +1 -1
- package/dist/pdf-page.js +6 -6
- package/dist/pdf-page.js.map +1 -1
- package/package.json +3 -3
package/dist/pdf-page.d.ts
CHANGED
package/dist/pdf-page.js
CHANGED
|
@@ -54,7 +54,7 @@ let PDFPage = class PDFPage extends RectPath(Shape) {
|
|
|
54
54
|
this.renderTask = null;
|
|
55
55
|
this.page = null;
|
|
56
56
|
this.documentSize = null;
|
|
57
|
-
this.
|
|
57
|
+
this.pos = { x: 0, y: 0 };
|
|
58
58
|
}
|
|
59
59
|
async ready() {
|
|
60
60
|
this.loadPDF();
|
|
@@ -217,7 +217,7 @@ let PDFPage = class PDFPage extends RectPath(Shape) {
|
|
|
217
217
|
this.displayScale = Math.min(Math.max(0.5, this.displayScale * zoomFactor), SCALE_LEVELS[SCALE_LEVELS.length - 1]);
|
|
218
218
|
const scaleLevel = SCALE_LEVELS.findIndex(level => this.displayScale <= level);
|
|
219
219
|
const newScaleIndex = scaleLevel === -1 ? SCALE_LEVELS.length - 1 : scaleLevel;
|
|
220
|
-
// Calculate zoom reference point (zoom in/out based on mouse
|
|
220
|
+
// Calculate zoom reference point (zoom in/out based on mouse pos)
|
|
221
221
|
const scaleRatio = this.displayScale / oldScale;
|
|
222
222
|
this.offsetX = x - (x - this.offsetX) * scaleRatio;
|
|
223
223
|
this.offsetY = y - (y - this.offsetY) * scaleRatio;
|
|
@@ -228,15 +228,15 @@ let PDFPage = class PDFPage extends RectPath(Shape) {
|
|
|
228
228
|
}
|
|
229
229
|
}
|
|
230
230
|
ondragstart(e) {
|
|
231
|
-
this.
|
|
231
|
+
this.pos = this.transcoordC2S(e.offsetX, e.offsetY);
|
|
232
232
|
}
|
|
233
233
|
ondragmove(e) {
|
|
234
234
|
var { x, y } = this.transcoordC2S(e.offsetX, e.offsetY);
|
|
235
|
-
const dx = x - this.
|
|
236
|
-
const dy = y - this.
|
|
235
|
+
const dx = x - this.pos.x;
|
|
236
|
+
const dy = y - this.pos.y;
|
|
237
237
|
this.offsetX += dx;
|
|
238
238
|
this.offsetY += dy;
|
|
239
|
-
this.
|
|
239
|
+
this.pos = { x, y };
|
|
240
240
|
this.invalidate();
|
|
241
241
|
}
|
|
242
242
|
ondblclick(e) {
|
package/dist/pdf-page.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pdf-page.js","sourceRoot":"","sources":["../src/pdf-page.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAA4D,MAAM,YAAY,CAAA;AACvH,OAAO,EAAyB,QAAQ,EAAE,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAA;AAE/F,mBAAmB,CAAC,SAAS,GAAG,mDAAmD,CAAA;AAEnF,MAAM,MAAM,GAAG;IACb,OAAO,EAAE,KAAK;IACd,SAAS,EAAE,IAAI;IACf,SAAS,EAAE,IAAI;IACf,UAAU,EAAE;QACV;YACE,IAAI,EAAE,QAAQ;YACd,KAAK,EAAE,OAAO;YACd,IAAI,EAAE,OAAO;YACb,QAAQ,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE;SACrB;QACD;YACE,IAAI,EAAE,qBAAqB;YAC3B,KAAK,EAAE,UAAU;YACjB,IAAI,EAAE,MAAM;YACZ,QAAQ,EAAE;gBACR,YAAY,EAAE,UAAU;gBACxB,cAAc,EAAE,IAAI;gBACpB,cAAc,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,aAAa,EAAE,CAAC,EAAE;gBACpF,SAAS,EAAE,IAAI;aAChB;SACF;QACD;YACE,IAAI,EAAE,QAAQ;YACd,KAAK,EAAE,UAAU;YACjB,IAAI,EAAE,MAAM;SACb;QACD;YACE,IAAI,EAAE,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AAO,MAAM,CAAA;IACf,CAAC;CACF,CAAA;AApPoB,OAAO;IAD3B,cAAc,CAAC,UAAU,CAAC;GACN,OAAO,CAoP3B;eApPoB,OAAO","sourcesContent":["import { getDocument, GlobalWorkerOptions, PageViewport, PDFDocumentProxy, PDFPageProxy, RenderTask } from 'pdfjs-dist'\nimport { Component, Properties, RectPath, Shape, sceneComponent } from '@hatiolab/things-scene'\n\nGlobalWorkerOptions.workerSrc = '/node_modules/pdfjs-dist/build/pdf.worker.min.mjs'\n\nconst NATURE = {\n mutable: false,\n resizable: true,\n rotatable: true,\n properties: [\n {\n type: 'number',\n label: 'round',\n name: 'round',\n property: { min: 0 }\n },\n {\n type: 'attachment-selector',\n label: 'pdf-file',\n name: 'file',\n property: {\n displayField: 'fullpath',\n displayFullUrl: true,\n storageFilters: { type: Array, value: [{ name: 'category', value: 'application' }] },\n useUpload: true\n }\n },\n {\n type: 'number',\n label: 'pdf-page',\n name: 'page'\n },\n {\n type: 'select',\n label: 'pdf-fit',\n name: 'fit',\n property: {\n options: ['none', 'ratio', 'width', 'height']\n }\n }\n ],\n help: 'scene/component/pdf-viewer'\n}\n\nconst SCALE_LEVELS = [2, 4, 8, 16, 32]\nconst MAX_CANVAS_SIZE = 16000\n\n@sceneComponent('pdf-page')\nexport default class PDFPage extends RectPath(Shape) {\n private scaleIndex: number = 0\n private displayScale: number = 1\n private offsetX: number = 0\n private offsetY: number = 0\n private pdfDoc: PDFDocumentProxy | null = null\n private canvas: HTMLCanvasElement | null = document.createElement('canvas')\n private renderTask: RenderTask | null = null\n private page: PDFPageProxy | null = null\n private documentSize: PageViewport | null = null\n private position: { x: number; y: number } = { x: 0, y: 0 }\n\n async ready() {\n this.loadPDF()\n }\n\n dispose() {\n super.dispose()\n\n if (this.renderTask) {\n this.renderTask.cancel()\n }\n this.pdfDoc = null\n this.canvas = null\n }\n\n async loadPDF() {\n const { file, page: pageNo = 1 } = this.state\n if (!file) return\n\n try {\n console.log(`📌 Loading PDF: ${file}`)\n this.pdfDoc = await getDocument(file).promise\n\n const numPages = this.pdfDoc.numPages\n this.page = await this.pdfDoc.getPage(pageNo > numPages ? 1 : pageNo)\n this.documentSize = this.page.getViewport({ scale: 1 })\n\n this.fit()\n } catch (error) {\n console.error('🚨 Failed to load PDF:', error)\n }\n }\n\n onchange(after: Properties, before: Properties) {\n if ('file' in after || 'fit' in after || 'page' in after) {\n this.loadPDF()\n }\n }\n\n /**\n * Adjusts the PDF display scale and offsets based on the fit mode.\n *\n * @async\n * @method fit\n * @memberof PdfPage\n * @returns {Promise<void>}\n *\n * @description\n * This method adjusts the display scale of the PDF page based on the `fit` mode specified in the component's state.\n * It supports the following fit modes:\n * - 'width': Scales the PDF to fit the width of the container.\n * - 'height': Scales the PDF to fit the height of the container.\n * - 'ratio': Scales the PDF to fit both the width and height of the container while maintaining the aspect ratio.\n * - 'none': Displays the PDF at its original size.\n *\n * The method also calculates the appropriate offsets to center the PDF within the container when the fit mode is 'ratio'.\n * Finally, it triggers the rendering of the PDF.\n */\n async fit() {\n if (!this.documentSize) {\n return\n }\n\n const { fit = 'none' } = this.state\n const { width, height } = this.bounds\n const viewport = this.documentSize\n\n switch (fit) {\n case 'none':\n this.displayScale = 1\n break\n case 'width':\n this.displayScale = width / viewport.width\n break\n case 'height':\n this.displayScale = height / viewport.height\n break\n case 'ratio':\n this.displayScale = Math.min(width / viewport.width, height / viewport.height)\n break\n default:\n this.displayScale = 1\n }\n\n this.scaleIndex = SCALE_LEVELS.findIndex(level => this.displayScale <= level)\n if (this.scaleIndex === -1) {\n this.scaleIndex = SCALE_LEVELS.length - 1\n }\n\n if (fit === 'ratio') {\n this.offsetX = (width - viewport.width * this.displayScale) / 2\n this.offsetY = (height - viewport.height * this.displayScale) / 2\n } else {\n this.offsetX = 0\n this.offsetY = 0\n }\n\n this.renderPDF()\n }\n\n async renderPDF() {\n if (!this.pdfDoc || !this.page || !this.documentSize) {\n return\n }\n\n const { width, height } = this.documentSize\n\n let scale = SCALE_LEVELS[this.scaleIndex]\n\n let newWidth = Math.min(width * scale, MAX_CANVAS_SIZE)\n let newHeight = Math.min(height * scale, MAX_CANVAS_SIZE)\n\n if (newWidth === MAX_CANVAS_SIZE || newHeight === MAX_CANVAS_SIZE) {\n const ratio = width / height\n if (ratio > 1) {\n newHeight = newWidth / ratio\n }\n\n const recalcScale = newHeight / height\n\n this.scaleIndex = SCALE_LEVELS.findIndex(level => recalcScale <= level) - 1\n scale = SCALE_LEVELS[this.scaleIndex]\n\n newWidth = width * scale\n newHeight = height * scale\n }\n\n // Create an `offscreenCanvas` to render in the background while maintaining the existing display\n const offscreenCanvas = document.createElement('canvas')\n offscreenCanvas.width = newWidth\n offscreenCanvas.height = newHeight\n\n const renderContext = {\n canvasContext: offscreenCanvas.getContext('2d')!,\n viewport: this.page.getViewport({ scale })\n }\n\n if (this.renderTask) {\n this.renderTask.cancel()\n try {\n await this.renderTask.promise\n } catch (error) {\n console.warn('Previous render task was canceled.')\n }\n }\n\n this.renderTask = this.page.render(renderContext)\n\n try {\n await this.renderTask.promise\n\n // 🔹 Replace the existing canvas with the new canvas once rendering is complete\n this.canvas = offscreenCanvas\n this.renderTask = null\n\n this.invalidate()\n } catch (error) {\n if (error instanceof Error && error.name !== 'RenderingCancelledException') {\n console.error('Failed to render PDF:', error)\n }\n }\n }\n\n render(ctx: CanvasRenderingContext2D) {\n super.render(ctx)\n\n if (!this.canvas) {\n return\n }\n\n const { left, top } = this.bounds\n const { width, height } = this.canvas\n\n ctx.clip()\n ctx.drawImage(\n this.canvas,\n left + this.offsetX,\n top + this.offsetY,\n (this.documentSize?.width || width) * this.displayScale,\n (this.documentSize?.height || height) * this.displayScale\n )\n }\n\n onwheel(e: WheelEvent) {\n e.preventDefault()\n var { left, top } = this.bounds\n var { x, y } = this.transcoordC2S(e.offsetX, e.offsetY)\n x -= left\n y -= top\n\n const zoomIn = e.deltaY < 0\n const zoomFactor = zoomIn ? 1.05 : 0.95\n const oldScale = this.displayScale\n\n this.displayScale = Math.min(Math.max(0.5, this.displayScale * zoomFactor), SCALE_LEVELS[SCALE_LEVELS.length - 1])\n\n const scaleLevel = SCALE_LEVELS.findIndex(level => this.displayScale <= level)\n const newScaleIndex = scaleLevel === -1 ? SCALE_LEVELS.length - 1 : scaleLevel\n\n // Calculate zoom reference point (zoom in/out based on mouse position)\n const scaleRatio = this.displayScale / oldScale\n this.offsetX = x - (x - this.offsetX) * scaleRatio\n this.offsetY = y - (y - this.offsetY) * scaleRatio\n\n this.invalidate()\n\n if (newScaleIndex !== this.scaleIndex) {\n this.scaleIndex = newScaleIndex\n this.renderPDF()\n }\n }\n\n ondragstart(e: MouseEvent) {\n this.position = this.transcoordC2S(e.offsetX, e.offsetY)\n }\n\n ondragmove(e: MouseEvent) {\n var { x, y } = this.transcoordC2S(e.offsetX, e.offsetY)\n const dx = x - this.position.x\n const dy = y - this.position.y\n this.offsetX += dx\n this.offsetY += dy\n this.position = { x, y }\n this.invalidate()\n }\n\n ondblclick(e: MouseEvent) {\n this.fit()\n }\n\n get nature() {\n return NATURE\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"pdf-page.js","sourceRoot":"","sources":["../src/pdf-page.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAA4D,MAAM,YAAY,CAAA;AACvH,OAAO,EAAyB,QAAQ,EAAE,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAA;AAE/F,mBAAmB,CAAC,SAAS,GAAG,mDAAmD,CAAA;AAEnF,MAAM,MAAM,GAAG;IACb,OAAO,EAAE,KAAK;IACd,SAAS,EAAE,IAAI;IACf,SAAS,EAAE,IAAI;IACf,UAAU,EAAE;QACV;YACE,IAAI,EAAE,QAAQ;YACd,KAAK,EAAE,OAAO;YACd,IAAI,EAAE,OAAO;YACb,QAAQ,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE;SACrB;QACD;YACE,IAAI,EAAE,qBAAqB;YAC3B,KAAK,EAAE,UAAU;YACjB,I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,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAA;QACzB,IAAI,CAAC,OAAO,IAAI,EAAE,CAAA;QAClB,IAAI,CAAC,OAAO,IAAI,EAAE,CAAA;QAClB,IAAI,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAA;QACnB,IAAI,CAAC,UAAU,EAAE,CAAA;IACnB,CAAC;IAED,UAAU,CAAC,CAAa;QACtB,IAAI,CAAC,GAAG,EAAE,CAAA;IACZ,CAAC;IAED,IAAI,MAAM;QACR,OAAO,MAAM,CAAA;IACf,CAAC;CACF,CAAA;AApPoB,OAAO;IAD3B,cAAc,CAAC,UAAU,CAAC;GACN,OAAO,CAoP3B;eApPoB,OAAO","sourcesContent":["import { getDocument, GlobalWorkerOptions, PageViewport, PDFDocumentProxy, PDFPageProxy, RenderTask } from 'pdfjs-dist'\nimport { Component, Properties, RectPath, Shape, sceneComponent } from '@hatiolab/things-scene'\n\nGlobalWorkerOptions.workerSrc = '/node_modules/pdfjs-dist/build/pdf.worker.min.mjs'\n\nconst NATURE = {\n mutable: false,\n resizable: true,\n rotatable: true,\n properties: [\n {\n type: 'number',\n label: 'round',\n name: 'round',\n property: { min: 0 }\n },\n {\n type: 'attachment-selector',\n label: 'pdf-file',\n name: 'file',\n property: {\n displayField: 'fullpath',\n displayFullUrl: true,\n storageFilters: { type: Array, value: [{ name: 'category', value: 'application' }] },\n useUpload: true\n }\n },\n {\n type: 'number',\n label: 'pdf-page',\n name: 'page'\n },\n {\n type: 'select',\n label: 'pdf-fit',\n name: 'fit',\n property: {\n options: ['none', 'ratio', 'width', 'height']\n }\n }\n ],\n help: 'scene/component/pdf-viewer'\n}\n\nconst SCALE_LEVELS = [2, 4, 8, 16, 32]\nconst MAX_CANVAS_SIZE = 16000\n\n@sceneComponent('pdf-page')\nexport default class PDFPage extends RectPath(Shape) {\n private scaleIndex: number = 0\n private displayScale: number = 1\n private offsetX: number = 0\n private offsetY: number = 0\n private pdfDoc: PDFDocumentProxy | null = null\n private canvas: HTMLCanvasElement | null = document.createElement('canvas')\n private renderTask: RenderTask | null = null\n private page: PDFPageProxy | null = null\n private documentSize: PageViewport | null = null\n private pos: { x: number; y: number } = { x: 0, y: 0 }\n\n async ready() {\n this.loadPDF()\n }\n\n dispose() {\n super.dispose()\n\n if (this.renderTask) {\n this.renderTask.cancel()\n }\n this.pdfDoc = null\n this.canvas = null\n }\n\n async loadPDF() {\n const { file, page: pageNo = 1 } = this.state\n if (!file) return\n\n try {\n console.log(`📌 Loading PDF: ${file}`)\n this.pdfDoc = await getDocument(file).promise\n\n const numPages = this.pdfDoc.numPages\n this.page = await this.pdfDoc.getPage(pageNo > numPages ? 1 : pageNo)\n this.documentSize = this.page.getViewport({ scale: 1 })\n\n this.fit()\n } catch (error) {\n console.error('🚨 Failed to load PDF:', error)\n }\n }\n\n onchange(after: Properties, before: Properties) {\n if ('file' in after || 'fit' in after || 'page' in after) {\n this.loadPDF()\n }\n }\n\n /**\n * Adjusts the PDF display scale and offsets based on the fit mode.\n *\n * @async\n * @method fit\n * @memberof PdfPage\n * @returns {Promise<void>}\n *\n * @description\n * This method adjusts the display scale of the PDF page based on the `fit` mode specified in the component's state.\n * It supports the following fit modes:\n * - 'width': Scales the PDF to fit the width of the container.\n * - 'height': Scales the PDF to fit the height of the container.\n * - 'ratio': Scales the PDF to fit both the width and height of the container while maintaining the aspect ratio.\n * - 'none': Displays the PDF at its original size.\n *\n * The method also calculates the appropriate offsets to center the PDF within the container when the fit mode is 'ratio'.\n * Finally, it triggers the rendering of the PDF.\n */\n async fit() {\n if (!this.documentSize) {\n return\n }\n\n const { fit = 'none' } = this.state\n const { width, height } = this.bounds\n const viewport = this.documentSize\n\n switch (fit) {\n case 'none':\n this.displayScale = 1\n break\n case 'width':\n this.displayScale = width / viewport.width\n break\n case 'height':\n this.displayScale = height / viewport.height\n break\n case 'ratio':\n this.displayScale = Math.min(width / viewport.width, height / viewport.height)\n break\n default:\n this.displayScale = 1\n }\n\n this.scaleIndex = SCALE_LEVELS.findIndex(level => this.displayScale <= level)\n if (this.scaleIndex === -1) {\n this.scaleIndex = SCALE_LEVELS.length - 1\n }\n\n if (fit === 'ratio') {\n this.offsetX = (width - viewport.width * this.displayScale) / 2\n this.offsetY = (height - viewport.height * this.displayScale) / 2\n } else {\n this.offsetX = 0\n this.offsetY = 0\n }\n\n this.renderPDF()\n }\n\n async renderPDF() {\n if (!this.pdfDoc || !this.page || !this.documentSize) {\n return\n }\n\n const { width, height } = this.documentSize\n\n let scale = SCALE_LEVELS[this.scaleIndex]\n\n let newWidth = Math.min(width * scale, MAX_CANVAS_SIZE)\n let newHeight = Math.min(height * scale, MAX_CANVAS_SIZE)\n\n if (newWidth === MAX_CANVAS_SIZE || newHeight === MAX_CANVAS_SIZE) {\n const ratio = width / height\n if (ratio > 1) {\n newHeight = newWidth / ratio\n }\n\n const recalcScale = newHeight / height\n\n this.scaleIndex = SCALE_LEVELS.findIndex(level => recalcScale <= level) - 1\n scale = SCALE_LEVELS[this.scaleIndex]\n\n newWidth = width * scale\n newHeight = height * scale\n }\n\n // Create an `offscreenCanvas` to render in the background while maintaining the existing display\n const offscreenCanvas = document.createElement('canvas')\n offscreenCanvas.width = newWidth\n offscreenCanvas.height = newHeight\n\n const renderContext = {\n canvasContext: offscreenCanvas.getContext('2d')!,\n viewport: this.page.getViewport({ scale })\n }\n\n if (this.renderTask) {\n this.renderTask.cancel()\n try {\n await this.renderTask.promise\n } catch (error) {\n console.warn('Previous render task was canceled.')\n }\n }\n\n this.renderTask = this.page.render(renderContext)\n\n try {\n await this.renderTask.promise\n\n // 🔹 Replace the existing canvas with the new canvas once rendering is complete\n this.canvas = offscreenCanvas\n this.renderTask = null\n\n this.invalidate()\n } catch (error) {\n if (error instanceof Error && error.name !== 'RenderingCancelledException') {\n console.error('Failed to render PDF:', error)\n }\n }\n }\n\n render(ctx: CanvasRenderingContext2D) {\n super.render(ctx)\n\n if (!this.canvas) {\n return\n }\n\n const { left, top } = this.bounds\n const { width, height } = this.canvas\n\n ctx.clip()\n ctx.drawImage(\n this.canvas,\n left + this.offsetX,\n top + this.offsetY,\n (this.documentSize?.width || width) * this.displayScale,\n (this.documentSize?.height || height) * this.displayScale\n )\n }\n\n onwheel(e: WheelEvent) {\n e.preventDefault()\n var { left, top } = this.bounds\n var { x, y } = this.transcoordC2S(e.offsetX, e.offsetY)\n x -= left\n y -= top\n\n const zoomIn = e.deltaY < 0\n const zoomFactor = zoomIn ? 1.05 : 0.95\n const oldScale = this.displayScale\n\n this.displayScale = Math.min(Math.max(0.5, this.displayScale * zoomFactor), SCALE_LEVELS[SCALE_LEVELS.length - 1])\n\n const scaleLevel = SCALE_LEVELS.findIndex(level => this.displayScale <= level)\n const newScaleIndex = scaleLevel === -1 ? SCALE_LEVELS.length - 1 : scaleLevel\n\n // Calculate zoom reference point (zoom in/out based on mouse pos)\n const scaleRatio = this.displayScale / oldScale\n this.offsetX = x - (x - this.offsetX) * scaleRatio\n this.offsetY = y - (y - this.offsetY) * scaleRatio\n\n this.invalidate()\n\n if (newScaleIndex !== this.scaleIndex) {\n this.scaleIndex = newScaleIndex\n this.renderPDF()\n }\n }\n\n ondragstart(e: MouseEvent) {\n this.pos = this.transcoordC2S(e.offsetX, e.offsetY)\n }\n\n ondragmove(e: MouseEvent) {\n var { x, y } = this.transcoordC2S(e.offsetX, e.offsetY)\n const dx = x - this.pos.x\n const dy = y - this.pos.y\n this.offsetX += dx\n this.offsetY += dy\n this.pos = { x, y }\n this.invalidate()\n }\n\n ondblclick(e: MouseEvent) {\n this.fit()\n }\n\n get nature() {\n return NATURE\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@operato/scene-pdf",
|
|
3
3
|
"description": "pdf component for operato-scene",
|
|
4
4
|
"author": "heartyoh",
|
|
5
|
-
"version": "9.0.0
|
|
5
|
+
"version": "9.0.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "dist/index.js",
|
|
8
8
|
"module": "dist/index.js",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"migration": "things-factory-migration"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@hatiolab/things-scene": "^9.0.0
|
|
31
|
+
"@hatiolab/things-scene": "^9.0.0",
|
|
32
32
|
"lit": "^3.1.2",
|
|
33
33
|
"pdfjs-dist": "^4.10.38"
|
|
34
34
|
},
|
|
@@ -60,5 +60,5 @@
|
|
|
60
60
|
"prettier --write"
|
|
61
61
|
]
|
|
62
62
|
},
|
|
63
|
-
"gitHead": "
|
|
63
|
+
"gitHead": "0c295810b93773e6373f96d69fe62e930d71c0cd"
|
|
64
64
|
}
|