microboard-temp 0.4.10 → 0.4.11

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/cjs/node.js CHANGED
@@ -1783,6 +1783,7 @@ __export(exports_node, {
1783
1783
  Drawing: () => Drawing,
1784
1784
  DefaultTransformationData: () => DefaultTransformationData,
1785
1785
  DefaultShapeData: () => DefaultShapeData,
1786
+ Deck: () => Deck,
1786
1787
  DEFAULT_SHAPE: () => DEFAULT_SHAPE,
1787
1788
  CubicBezier: () => CubicBezier,
1788
1789
  Counter: () => Counter,
@@ -1790,6 +1791,7 @@ __export(exports_node, {
1790
1791
  Connector: () => Connector2,
1791
1792
  ConnectionLineWidths: () => ConnectionLineWidths,
1792
1793
  Comment: () => Comment,
1794
+ Card: () => Card,
1793
1795
  Camera: () => Camera,
1794
1796
  CURSORS_IDLE_CLEANUP_DELAY: () => CURSORS_IDLE_CLEANUP_DELAY,
1795
1797
  CURSORS_ANIMATION_DURATION: () => CURSORS_ANIMATION_DURATION,
@@ -48125,7 +48127,9 @@ function registerItem({
48125
48127
  const { itemType } = defaultData2;
48126
48128
  itemFactories[itemType] = createItemFactory(item, defaultData2);
48127
48129
  itemValidators[itemType] = createItemValidator(defaultData2);
48128
- registeredTools[toolData.name] = toolData.tool;
48130
+ if (toolData) {
48131
+ registeredTools[toolData.name] = toolData.tool;
48132
+ }
48129
48133
  itemCommandFactories[itemType] = createItemCommandFactory(itemType);
48130
48134
  }
48131
48135
  function createItemFactory(item, defaultData2) {
@@ -48439,6 +48443,318 @@ registerItem({
48439
48443
  defaultData: defaultCounterData,
48440
48444
  toolData: { name: "AddCounter", tool: AddCounter }
48441
48445
  });
48446
+ // src/utils.ts
48447
+ function throttle(func, delay) {
48448
+ let lastCall = 0;
48449
+ let timeoutId = null;
48450
+ return function(...args) {
48451
+ const now = Date.now();
48452
+ if (lastCall + delay <= now) {
48453
+ lastCall = now;
48454
+ func(...args);
48455
+ if (timeoutId) {
48456
+ clearTimeout(timeoutId);
48457
+ timeoutId = null;
48458
+ }
48459
+ } else if (!timeoutId) {
48460
+ timeoutId = setTimeout(() => {
48461
+ lastCall = Date.now();
48462
+ timeoutId = null;
48463
+ func(...args);
48464
+ }, delay - (now - lastCall));
48465
+ }
48466
+ };
48467
+ }
48468
+
48469
+ // src/Items/Examples/CardGame/Card/Card.ts
48470
+ var defaultCardData = {
48471
+ itemType: "Card",
48472
+ isOpen: false,
48473
+ name: "",
48474
+ isInDeck: false
48475
+ };
48476
+ var CARD_DIMENSIONS = { width: 250, height: 400 };
48477
+
48478
+ class Card extends BaseItem {
48479
+ subject = new Subject;
48480
+ name = "";
48481
+ isOpen = false;
48482
+ isInDeck = false;
48483
+ throttledBringToFront;
48484
+ image = null;
48485
+ backside = null;
48486
+ imageToRender = null;
48487
+ shouldUseCustomRender = false;
48488
+ constructor(board, id = "", defaultData2, name) {
48489
+ super(board, id, defaultCardData);
48490
+ this.name = name;
48491
+ this.createImages();
48492
+ this.throttledBringToFront = throttle(() => {
48493
+ this.board.bringToFront(this);
48494
+ }, 1000);
48495
+ this.transformation.subject.subscribe(() => {
48496
+ this.throttledBringToFront();
48497
+ this.updateMbr();
48498
+ this.updateImageToRender();
48499
+ this.subject.publish(this);
48500
+ });
48501
+ this.updateMbr();
48502
+ }
48503
+ createImages() {
48504
+ this.image = conf.documentFactory.createElement("img");
48505
+ this.backside = conf.documentFactory.createElement("img");
48506
+ this.image.src = `/Assets/Cards/${this.name}.png`;
48507
+ this.backside.src = `/Assets/Cards/backside.png`;
48508
+ this.image.onload = () => {
48509
+ this.subject.publish(this);
48510
+ };
48511
+ this.backside.onload = () => {
48512
+ this.subject.publish(this);
48513
+ };
48514
+ }
48515
+ setIsInDeck(isInDeck) {
48516
+ this.emit({
48517
+ class: "Card",
48518
+ method: "setIsInDeck",
48519
+ item: [this.getId()],
48520
+ newData: { isInDeck },
48521
+ prevData: { isInDeck: this.isInDeck }
48522
+ });
48523
+ }
48524
+ updateImageToRender() {
48525
+ this.imageToRender = this.backside;
48526
+ if (this.isOpen) {
48527
+ this.imageToRender = this.image;
48528
+ }
48529
+ }
48530
+ render(context, deckRenderData) {
48531
+ if (this.transformationRenderBlock || this.isInDeck && !deckRenderData) {
48532
+ return;
48533
+ }
48534
+ const ctx = context.ctx;
48535
+ if (this.imageToRender && this.imageToRender.complete) {
48536
+ ctx.save();
48537
+ let left = this.left;
48538
+ let top = this.top;
48539
+ if (deckRenderData) {
48540
+ left = deckRenderData.left + 2 * deckRenderData.cardPosition;
48541
+ top = deckRenderData.top;
48542
+ }
48543
+ ctx.drawImage(this.imageToRender, left, top, CARD_DIMENSIONS.width, CARD_DIMENSIONS.height);
48544
+ ctx.restore();
48545
+ }
48546
+ }
48547
+ updateMbr() {
48548
+ const { translateX, translateY, scaleX, scaleY } = this.transformation.matrix;
48549
+ this.left = translateX;
48550
+ this.top = translateY;
48551
+ this.right = this.left + CARD_DIMENSIONS.width * scaleX;
48552
+ this.bottom = this.top + CARD_DIMENSIONS.height * scaleY;
48553
+ }
48554
+ getMbr() {
48555
+ if (this.isInDeck) {
48556
+ return new Mbr(1e4, 1e4, 1e4, 1e4);
48557
+ }
48558
+ return super.getMbr();
48559
+ }
48560
+ getPath() {
48561
+ return new Path(this.getMbr().getLines());
48562
+ }
48563
+ renderHTML(documentFactory) {
48564
+ const div = documentFactory.createElement("card-item");
48565
+ return div;
48566
+ }
48567
+ deserialize(data) {
48568
+ super.deserialize(data);
48569
+ this.updateMbr();
48570
+ this.createImages();
48571
+ this.subject.publish(this);
48572
+ return this;
48573
+ }
48574
+ toggleIsOpen() {
48575
+ this.emit({
48576
+ class: "Card",
48577
+ method: "setIsOpen",
48578
+ item: [this.getId()],
48579
+ newData: { isOpen: !this.isOpen },
48580
+ prevData: { isOpen: this.isOpen }
48581
+ });
48582
+ }
48583
+ apply(op) {
48584
+ super.apply(op);
48585
+ switch (op.class) {
48586
+ case "Card":
48587
+ switch (op.method) {
48588
+ case "setIsOpen":
48589
+ this.isOpen = op.newData.isOpen;
48590
+ this.updateImageToRender();
48591
+ break;
48592
+ case "setIsInDeck":
48593
+ this.isInDeck = op.newData.isInDeck;
48594
+ break;
48595
+ }
48596
+ break;
48597
+ }
48598
+ this.subject.publish(this);
48599
+ }
48600
+ }
48601
+ registerItem({
48602
+ item: Card,
48603
+ defaultData: defaultCardData
48604
+ });
48605
+ // src/Items/Examples/CardGame/Deck/Deck.ts
48606
+ var defaultDeckData = {
48607
+ itemType: "Deck",
48608
+ cardIds: []
48609
+ };
48610
+
48611
+ class Deck extends BaseItem {
48612
+ subject = new Subject;
48613
+ shouldUseCustomRender = false;
48614
+ cardIds = [];
48615
+ cards = [];
48616
+ constructor(board, id = "", defaultData2, cards) {
48617
+ super(board, id, defaultDeckData);
48618
+ if (cards) {
48619
+ this.cards = cards;
48620
+ cards.forEach((card) => card.setIsInDeck(true));
48621
+ this.transformation.matrix = cards[cards.length - 1].transformation.matrix;
48622
+ this.cardIds = cards.map((card) => card.getId());
48623
+ }
48624
+ this.transformation.subject.subscribe(() => {
48625
+ this.updateMbr();
48626
+ this.subject.publish(this);
48627
+ });
48628
+ this.updateMbr();
48629
+ }
48630
+ getDeck() {
48631
+ return this.cards;
48632
+ }
48633
+ getTopCard() {
48634
+ const cardId = this.cardIds[this.cardIds.length - 1];
48635
+ return this.getCards([cardId])[0];
48636
+ }
48637
+ getBottomCard() {
48638
+ const cardId = this.cardIds[0];
48639
+ return this.getCards([cardId])[0];
48640
+ }
48641
+ getRandomCard() {
48642
+ const cardId = this.cardIds[Math.ceil(Math.random() * this.cardIds.length) - 1];
48643
+ return this.getCards([cardId])[0];
48644
+ }
48645
+ getCards(cardIds) {
48646
+ const cards = this.findCardsOnBoard(cardIds);
48647
+ this.removeCards(cards);
48648
+ return cards;
48649
+ }
48650
+ findCardsOnBoard(cardIds) {
48651
+ return cardIds.map((cardId) => {
48652
+ return this.board.items.getById(cardId);
48653
+ }).filter((card) => !!card);
48654
+ }
48655
+ updateCards() {
48656
+ if (this.cardIds.length === this.cards.length) {
48657
+ return this.cards;
48658
+ }
48659
+ this.cards = this.findCardsOnBoard(this.cardIds);
48660
+ return this.cards;
48661
+ }
48662
+ shuffleDeck() {
48663
+ const shuffled = [...this.cardIds];
48664
+ for (let i = shuffled.length - 1;i > 0; i--) {
48665
+ const j = Math.floor(Math.random() * (i + 1));
48666
+ [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
48667
+ }
48668
+ const cards = this.findCardsOnBoard(shuffled);
48669
+ this.addCards(cards, true);
48670
+ }
48671
+ addCards(cards, shouldReplaceExistingCards = false) {
48672
+ cards.forEach((card) => {
48673
+ card.setIsInDeck(true);
48674
+ });
48675
+ this.board.bringToFront(cards);
48676
+ this.emit({
48677
+ class: "Deck",
48678
+ method: "addCards",
48679
+ item: [this.getId()],
48680
+ newData: {
48681
+ cardIds: cards.map((card) => card.getId()),
48682
+ shouldReplaceExistingCards
48683
+ },
48684
+ prevData: { cardIds: this.cardIds, shouldReplaceExistingCards }
48685
+ });
48686
+ }
48687
+ removeCards(cards) {
48688
+ cards.forEach((card) => {
48689
+ card.setIsInDeck(false);
48690
+ });
48691
+ this.emit({
48692
+ class: "Deck",
48693
+ method: "removeCards",
48694
+ item: [this.getId()],
48695
+ newData: { cardIds: cards.map((card) => card.getId()) },
48696
+ prevData: { cardIds: this.cardIds }
48697
+ });
48698
+ }
48699
+ apply(op) {
48700
+ super.apply(op);
48701
+ switch (op.class) {
48702
+ case "Deck":
48703
+ switch (op.method) {
48704
+ case "addCards":
48705
+ if (op.newData.shouldReplaceExistingCards) {
48706
+ this.cardIds = op.newData.cardIds;
48707
+ this.cards = this.findCardsOnBoard(this.cardIds);
48708
+ } else {
48709
+ this.cardIds = [
48710
+ ...op.newData.cardIds,
48711
+ ...this.cardIds
48712
+ ];
48713
+ this.updateCards();
48714
+ this.updateMbr();
48715
+ }
48716
+ break;
48717
+ case "removeCards":
48718
+ this.cardIds = this.cardIds.filter((card) => {
48719
+ return !op.newData.cardIds.includes(card);
48720
+ });
48721
+ this.updateCards();
48722
+ this.updateMbr();
48723
+ break;
48724
+ }
48725
+ break;
48726
+ }
48727
+ this.subject.publish(this);
48728
+ }
48729
+ updateMbr() {
48730
+ const { translateX, translateY, scaleX, scaleY } = this.transformation.matrix;
48731
+ this.left = translateX;
48732
+ this.top = translateY;
48733
+ this.right = this.left + CARD_DIMENSIONS.width * scaleX + 2 * this.cardIds.length;
48734
+ this.bottom = this.top + CARD_DIMENSIONS.height * scaleY;
48735
+ }
48736
+ deserialize(data) {
48737
+ super.deserialize(data);
48738
+ this.updateCards();
48739
+ return this;
48740
+ }
48741
+ render(context) {
48742
+ if (this.transformationRenderBlock) {
48743
+ return;
48744
+ }
48745
+ this.cards.forEach((card, index2) => {
48746
+ card.render(context, {
48747
+ top: this.top,
48748
+ left: this.left,
48749
+ cardPosition: index2 + 1
48750
+ });
48751
+ });
48752
+ }
48753
+ }
48754
+ registerItem({
48755
+ item: Deck,
48756
+ defaultData: defaultDeckData
48757
+ });
48442
48758
  // src/Pointer/Cursor.ts
48443
48759
  var defaultCursors = [
48444
48760
  "default",
@@ -48951,27 +49267,6 @@ class Camera {
48951
49267
  requestAnimationFrame(animate);
48952
49268
  }
48953
49269
  }
48954
- function throttle(func, delay) {
48955
- let lastCall = 0;
48956
- let timeoutId = null;
48957
- return function(...args) {
48958
- const now = Date.now();
48959
- if (lastCall + delay <= now) {
48960
- lastCall = now;
48961
- func(...args);
48962
- if (timeoutId) {
48963
- clearTimeout(timeoutId);
48964
- timeoutId = null;
48965
- }
48966
- } else if (!timeoutId) {
48967
- timeoutId = setTimeout(() => {
48968
- lastCall = Date.now();
48969
- timeoutId = null;
48970
- func(...args);
48971
- }, delay - (now - lastCall));
48972
- }
48973
- };
48974
- }
48975
49270
  // src/hotkeys.json
48976
49271
  var hotkeys_default = {
48977
49272
  select: {