@8btc/xcanvas 0.0.14-beta.0 → 0.0.14-beta.2

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/index.d.ts CHANGED
@@ -58,6 +58,18 @@ export declare interface XCanvasAPI {
58
58
  width: number;
59
59
  height: number;
60
60
  }) => string | undefined;
61
+ /**
62
+ * 判断当前是否可以裁切图片
63
+ */
64
+ predicateCropImage: () => boolean;
65
+ /**
66
+ * 裁切图片
67
+ */
68
+ cropImage: () => void;
69
+ /**
70
+ * 删除选区里的全部元素
71
+ */
72
+ deleteSelection: () => void;
61
73
  }
62
74
 
63
75
  export declare interface XcanvasProps extends ExcalidrawProps {
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@ import { jsx, jsxs } from "react/jsx-runtime";
2
2
  import { atom, useAtom, useAtomValue, useSetAtom } from "jotai";
3
3
  import { Excalidraw, convertToExcalidrawElements, exportToCanvas } from "@8btc/excalidraw";
4
4
  import { useEffect } from "react";
5
- const version = "0.0.14-beta.0";
5
+ const version = "0.0.14-beta.2";
6
6
  const packageJson = {
7
7
  version
8
8
  };
@@ -153,6 +153,13 @@ function ApiFactory() {
153
153
  animate: currentElements.length === 0 ? false : true
154
154
  });
155
155
  };
156
+ const getSelectedElements = (elements, appState) => {
157
+ const selectedIds = appState.selectedElementIds;
158
+ const selectedElements = elements.filter(
159
+ (element) => selectedIds[element.id]
160
+ );
161
+ return selectedElements;
162
+ };
156
163
  setApi({
157
164
  rawApi: excalidrawAPI,
158
165
  __printCanvasInfo: () => {
@@ -282,11 +289,13 @@ function ApiFactory() {
282
289
  exportSelection: async () => {
283
290
  if (!excalidrawAPI) return;
284
291
  const appState = excalidrawAPI.getAppState();
285
- const selectedIds = appState.selectedElementIds;
286
- if (Object.keys(selectedIds).length === 0) {
292
+ const selectedElements = getSelectedElements(
293
+ excalidrawAPI.getSceneElements(),
294
+ appState
295
+ );
296
+ if (selectedElements.length === 0) {
287
297
  throw "没有选中任何元素";
288
298
  }
289
- const selectedElements = excalidrawAPI.getSceneElements().filter((element) => appState.selectedElementIds[element.id]);
290
299
  const files = excalidrawAPI.getFiles();
291
300
  if (selectedElements.length === 1 && selectedElements[0].type === "image" && selectedElements[0].fileId) {
292
301
  console.log(files[selectedElements[0].fileId].dataURL);
@@ -298,8 +307,7 @@ function ApiFactory() {
298
307
  const canvas = await exportToCanvas({
299
308
  elements: selectedElements,
300
309
  appState: {
301
- ...appState,
302
- selectedElementIds: selectedIds
310
+ ...appState
303
311
  },
304
312
  files,
305
313
  mimeType: "image/png",
@@ -312,11 +320,10 @@ function ApiFactory() {
312
320
  insertCustomElement: (data, positonCalculator) => {
313
321
  if (!excalidrawAPI) return;
314
322
  const appState = excalidrawAPI.getAppState();
315
- const selectedIds = appState.selectedElementIds;
316
- if (Object.keys(selectedIds).length === 0) {
317
- throw "没有选中任何元素";
318
- }
319
- const selectedElements = excalidrawAPI.getSceneElements().filter((element) => appState.selectedElementIds[element.id]);
323
+ const selectedElements = getSelectedElements(
324
+ excalidrawAPI.getSceneElements(),
325
+ appState
326
+ );
320
327
  if (selectedElements.length === 0) {
321
328
  throw "没有选中任何可见元素";
322
329
  }
@@ -383,6 +390,45 @@ function ApiFactory() {
383
390
  elements: [...currentElements, ...el]
384
391
  });
385
392
  return el[0].id;
393
+ },
394
+ predicateCropImage: () => {
395
+ const appState = excalidrawAPI.getAppState();
396
+ const selectedElements = getSelectedElements(
397
+ excalidrawAPI.getSceneElements(),
398
+ appState
399
+ );
400
+ if (!appState.croppingElementId && selectedElements.length === 1 && selectedElements[0].type === "image") {
401
+ return true;
402
+ }
403
+ return false;
404
+ },
405
+ cropImage: () => {
406
+ const appState = excalidrawAPI.getAppState();
407
+ const selectedIds = appState.selectedElementIds;
408
+ const selectedElements = excalidrawAPI.getSceneElements().filter((element) => selectedIds[element.id]);
409
+ if (selectedElements.length !== 1) return;
410
+ excalidrawAPI.updateScene({
411
+ appState: {
412
+ ...appState,
413
+ isCropping: false,
414
+ croppingElementId: selectedElements[0].id
415
+ }
416
+ });
417
+ },
418
+ deleteSelection: () => {
419
+ const appState = excalidrawAPI.getAppState();
420
+ const selectedIds = appState.selectedElementIds;
421
+ const currentElements = excalidrawAPI.getSceneElements();
422
+ const newElements = currentElements.filter((el) => {
423
+ return !selectedIds[el.id];
424
+ });
425
+ excalidrawAPI.updateScene({
426
+ elements: [...newElements],
427
+ appState: {
428
+ ...appState,
429
+ selectedElementIds: {}
430
+ }
431
+ });
386
432
  }
387
433
  });
388
434
  }, [excalidrawAPI, setApi]);
@@ -2,7 +2,7 @@
2
2
  typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("react/jsx-runtime"), require("jotai"), require("@8btc/excalidraw"), require("react")) : typeof define === "function" && define.amd ? define(["exports", "react/jsx-runtime", "jotai", "@8btc/excalidraw", "react"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.XCanvas = {}, global.jsxRuntime, global.Jotai, global.Excalidraw, global.React));
3
3
  })(this, (function(exports2, jsxRuntime, jotai, excalidraw, react) {
4
4
  "use strict";
5
- const version = "0.0.14-beta.0";
5
+ const version = "0.0.14-beta.2";
6
6
  const packageJson = {
7
7
  version
8
8
  };
@@ -153,6 +153,13 @@
153
153
  animate: currentElements.length === 0 ? false : true
154
154
  });
155
155
  };
156
+ const getSelectedElements = (elements, appState) => {
157
+ const selectedIds = appState.selectedElementIds;
158
+ const selectedElements = elements.filter(
159
+ (element) => selectedIds[element.id]
160
+ );
161
+ return selectedElements;
162
+ };
156
163
  setApi({
157
164
  rawApi: excalidrawAPI,
158
165
  __printCanvasInfo: () => {
@@ -282,11 +289,13 @@
282
289
  exportSelection: async () => {
283
290
  if (!excalidrawAPI) return;
284
291
  const appState = excalidrawAPI.getAppState();
285
- const selectedIds = appState.selectedElementIds;
286
- if (Object.keys(selectedIds).length === 0) {
292
+ const selectedElements = getSelectedElements(
293
+ excalidrawAPI.getSceneElements(),
294
+ appState
295
+ );
296
+ if (selectedElements.length === 0) {
287
297
  throw "没有选中任何元素";
288
298
  }
289
- const selectedElements = excalidrawAPI.getSceneElements().filter((element) => appState.selectedElementIds[element.id]);
290
299
  const files = excalidrawAPI.getFiles();
291
300
  if (selectedElements.length === 1 && selectedElements[0].type === "image" && selectedElements[0].fileId) {
292
301
  console.log(files[selectedElements[0].fileId].dataURL);
@@ -298,8 +307,7 @@
298
307
  const canvas = await excalidraw.exportToCanvas({
299
308
  elements: selectedElements,
300
309
  appState: {
301
- ...appState,
302
- selectedElementIds: selectedIds
310
+ ...appState
303
311
  },
304
312
  files,
305
313
  mimeType: "image/png",
@@ -312,11 +320,10 @@
312
320
  insertCustomElement: (data, positonCalculator) => {
313
321
  if (!excalidrawAPI) return;
314
322
  const appState = excalidrawAPI.getAppState();
315
- const selectedIds = appState.selectedElementIds;
316
- if (Object.keys(selectedIds).length === 0) {
317
- throw "没有选中任何元素";
318
- }
319
- const selectedElements = excalidrawAPI.getSceneElements().filter((element) => appState.selectedElementIds[element.id]);
323
+ const selectedElements = getSelectedElements(
324
+ excalidrawAPI.getSceneElements(),
325
+ appState
326
+ );
320
327
  if (selectedElements.length === 0) {
321
328
  throw "没有选中任何可见元素";
322
329
  }
@@ -383,6 +390,45 @@
383
390
  elements: [...currentElements, ...el]
384
391
  });
385
392
  return el[0].id;
393
+ },
394
+ predicateCropImage: () => {
395
+ const appState = excalidrawAPI.getAppState();
396
+ const selectedElements = getSelectedElements(
397
+ excalidrawAPI.getSceneElements(),
398
+ appState
399
+ );
400
+ if (!appState.croppingElementId && selectedElements.length === 1 && selectedElements[0].type === "image") {
401
+ return true;
402
+ }
403
+ return false;
404
+ },
405
+ cropImage: () => {
406
+ const appState = excalidrawAPI.getAppState();
407
+ const selectedIds = appState.selectedElementIds;
408
+ const selectedElements = excalidrawAPI.getSceneElements().filter((element) => selectedIds[element.id]);
409
+ if (selectedElements.length !== 1) return;
410
+ excalidrawAPI.updateScene({
411
+ appState: {
412
+ ...appState,
413
+ isCropping: false,
414
+ croppingElementId: selectedElements[0].id
415
+ }
416
+ });
417
+ },
418
+ deleteSelection: () => {
419
+ const appState = excalidrawAPI.getAppState();
420
+ const selectedIds = appState.selectedElementIds;
421
+ const currentElements = excalidrawAPI.getSceneElements();
422
+ const newElements = currentElements.filter((el) => {
423
+ return !selectedIds[el.id];
424
+ });
425
+ excalidrawAPI.updateScene({
426
+ elements: [...newElements],
427
+ appState: {
428
+ ...appState,
429
+ selectedElementIds: {}
430
+ }
431
+ });
386
432
  }
387
433
  });
388
434
  }, [excalidrawAPI, setApi]);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@8btc/xcanvas",
3
3
  "private": false,
4
- "version": "0.0.14-beta.0",
4
+ "version": "0.0.14-beta.2",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
7
7
  "exports": {