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/esm/index.js CHANGED
@@ -45431,7 +45431,9 @@ function registerItem({
45431
45431
  const { itemType } = defaultData2;
45432
45432
  itemFactories[itemType] = createItemFactory(item, defaultData2);
45433
45433
  itemValidators[itemType] = createItemValidator(defaultData2);
45434
- registeredTools[toolData.name] = toolData.tool;
45434
+ if (toolData) {
45435
+ registeredTools[toolData.name] = toolData.tool;
45436
+ }
45435
45437
  itemCommandFactories[itemType] = createItemCommandFactory(itemType);
45436
45438
  }
45437
45439
  function createItemFactory(item, defaultData2) {
@@ -45745,6 +45747,318 @@ registerItem({
45745
45747
  defaultData: defaultCounterData,
45746
45748
  toolData: { name: "AddCounter", tool: AddCounter }
45747
45749
  });
45750
+ // src/utils.ts
45751
+ function throttle(func, delay) {
45752
+ let lastCall = 0;
45753
+ let timeoutId = null;
45754
+ return function(...args) {
45755
+ const now = Date.now();
45756
+ if (lastCall + delay <= now) {
45757
+ lastCall = now;
45758
+ func(...args);
45759
+ if (timeoutId) {
45760
+ clearTimeout(timeoutId);
45761
+ timeoutId = null;
45762
+ }
45763
+ } else if (!timeoutId) {
45764
+ timeoutId = setTimeout(() => {
45765
+ lastCall = Date.now();
45766
+ timeoutId = null;
45767
+ func(...args);
45768
+ }, delay - (now - lastCall));
45769
+ }
45770
+ };
45771
+ }
45772
+
45773
+ // src/Items/Examples/CardGame/Card/Card.ts
45774
+ var defaultCardData = {
45775
+ itemType: "Card",
45776
+ isOpen: false,
45777
+ name: "",
45778
+ isInDeck: false
45779
+ };
45780
+ var CARD_DIMENSIONS = { width: 250, height: 400 };
45781
+
45782
+ class Card extends BaseItem {
45783
+ subject = new Subject;
45784
+ name = "";
45785
+ isOpen = false;
45786
+ isInDeck = false;
45787
+ throttledBringToFront;
45788
+ image = null;
45789
+ backside = null;
45790
+ imageToRender = null;
45791
+ shouldUseCustomRender = false;
45792
+ constructor(board, id = "", defaultData2, name) {
45793
+ super(board, id, defaultCardData);
45794
+ this.name = name;
45795
+ this.createImages();
45796
+ this.throttledBringToFront = throttle(() => {
45797
+ this.board.bringToFront(this);
45798
+ }, 1000);
45799
+ this.transformation.subject.subscribe(() => {
45800
+ this.throttledBringToFront();
45801
+ this.updateMbr();
45802
+ this.updateImageToRender();
45803
+ this.subject.publish(this);
45804
+ });
45805
+ this.updateMbr();
45806
+ }
45807
+ createImages() {
45808
+ this.image = conf.documentFactory.createElement("img");
45809
+ this.backside = conf.documentFactory.createElement("img");
45810
+ this.image.src = `/Assets/Cards/${this.name}.png`;
45811
+ this.backside.src = `/Assets/Cards/backside.png`;
45812
+ this.image.onload = () => {
45813
+ this.subject.publish(this);
45814
+ };
45815
+ this.backside.onload = () => {
45816
+ this.subject.publish(this);
45817
+ };
45818
+ }
45819
+ setIsInDeck(isInDeck) {
45820
+ this.emit({
45821
+ class: "Card",
45822
+ method: "setIsInDeck",
45823
+ item: [this.getId()],
45824
+ newData: { isInDeck },
45825
+ prevData: { isInDeck: this.isInDeck }
45826
+ });
45827
+ }
45828
+ updateImageToRender() {
45829
+ this.imageToRender = this.backside;
45830
+ if (this.isOpen) {
45831
+ this.imageToRender = this.image;
45832
+ }
45833
+ }
45834
+ render(context, deckRenderData) {
45835
+ if (this.transformationRenderBlock || this.isInDeck && !deckRenderData) {
45836
+ return;
45837
+ }
45838
+ const ctx = context.ctx;
45839
+ if (this.imageToRender && this.imageToRender.complete) {
45840
+ ctx.save();
45841
+ let left = this.left;
45842
+ let top = this.top;
45843
+ if (deckRenderData) {
45844
+ left = deckRenderData.left + 2 * deckRenderData.cardPosition;
45845
+ top = deckRenderData.top;
45846
+ }
45847
+ ctx.drawImage(this.imageToRender, left, top, CARD_DIMENSIONS.width, CARD_DIMENSIONS.height);
45848
+ ctx.restore();
45849
+ }
45850
+ }
45851
+ updateMbr() {
45852
+ const { translateX, translateY, scaleX, scaleY } = this.transformation.matrix;
45853
+ this.left = translateX;
45854
+ this.top = translateY;
45855
+ this.right = this.left + CARD_DIMENSIONS.width * scaleX;
45856
+ this.bottom = this.top + CARD_DIMENSIONS.height * scaleY;
45857
+ }
45858
+ getMbr() {
45859
+ if (this.isInDeck) {
45860
+ return new Mbr(1e4, 1e4, 1e4, 1e4);
45861
+ }
45862
+ return super.getMbr();
45863
+ }
45864
+ getPath() {
45865
+ return new Path(this.getMbr().getLines());
45866
+ }
45867
+ renderHTML(documentFactory) {
45868
+ const div = documentFactory.createElement("card-item");
45869
+ return div;
45870
+ }
45871
+ deserialize(data) {
45872
+ super.deserialize(data);
45873
+ this.updateMbr();
45874
+ this.createImages();
45875
+ this.subject.publish(this);
45876
+ return this;
45877
+ }
45878
+ toggleIsOpen() {
45879
+ this.emit({
45880
+ class: "Card",
45881
+ method: "setIsOpen",
45882
+ item: [this.getId()],
45883
+ newData: { isOpen: !this.isOpen },
45884
+ prevData: { isOpen: this.isOpen }
45885
+ });
45886
+ }
45887
+ apply(op) {
45888
+ super.apply(op);
45889
+ switch (op.class) {
45890
+ case "Card":
45891
+ switch (op.method) {
45892
+ case "setIsOpen":
45893
+ this.isOpen = op.newData.isOpen;
45894
+ this.updateImageToRender();
45895
+ break;
45896
+ case "setIsInDeck":
45897
+ this.isInDeck = op.newData.isInDeck;
45898
+ break;
45899
+ }
45900
+ break;
45901
+ }
45902
+ this.subject.publish(this);
45903
+ }
45904
+ }
45905
+ registerItem({
45906
+ item: Card,
45907
+ defaultData: defaultCardData
45908
+ });
45909
+ // src/Items/Examples/CardGame/Deck/Deck.ts
45910
+ var defaultDeckData = {
45911
+ itemType: "Deck",
45912
+ cardIds: []
45913
+ };
45914
+
45915
+ class Deck extends BaseItem {
45916
+ subject = new Subject;
45917
+ shouldUseCustomRender = false;
45918
+ cardIds = [];
45919
+ cards = [];
45920
+ constructor(board, id = "", defaultData2, cards) {
45921
+ super(board, id, defaultDeckData);
45922
+ if (cards) {
45923
+ this.cards = cards;
45924
+ cards.forEach((card) => card.setIsInDeck(true));
45925
+ this.transformation.matrix = cards[cards.length - 1].transformation.matrix;
45926
+ this.cardIds = cards.map((card) => card.getId());
45927
+ }
45928
+ this.transformation.subject.subscribe(() => {
45929
+ this.updateMbr();
45930
+ this.subject.publish(this);
45931
+ });
45932
+ this.updateMbr();
45933
+ }
45934
+ getDeck() {
45935
+ return this.cards;
45936
+ }
45937
+ getTopCard() {
45938
+ const cardId = this.cardIds[this.cardIds.length - 1];
45939
+ return this.getCards([cardId])[0];
45940
+ }
45941
+ getBottomCard() {
45942
+ const cardId = this.cardIds[0];
45943
+ return this.getCards([cardId])[0];
45944
+ }
45945
+ getRandomCard() {
45946
+ const cardId = this.cardIds[Math.ceil(Math.random() * this.cardIds.length) - 1];
45947
+ return this.getCards([cardId])[0];
45948
+ }
45949
+ getCards(cardIds) {
45950
+ const cards = this.findCardsOnBoard(cardIds);
45951
+ this.removeCards(cards);
45952
+ return cards;
45953
+ }
45954
+ findCardsOnBoard(cardIds) {
45955
+ return cardIds.map((cardId) => {
45956
+ return this.board.items.getById(cardId);
45957
+ }).filter((card) => !!card);
45958
+ }
45959
+ updateCards() {
45960
+ if (this.cardIds.length === this.cards.length) {
45961
+ return this.cards;
45962
+ }
45963
+ this.cards = this.findCardsOnBoard(this.cardIds);
45964
+ return this.cards;
45965
+ }
45966
+ shuffleDeck() {
45967
+ const shuffled = [...this.cardIds];
45968
+ for (let i = shuffled.length - 1;i > 0; i--) {
45969
+ const j = Math.floor(Math.random() * (i + 1));
45970
+ [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
45971
+ }
45972
+ const cards = this.findCardsOnBoard(shuffled);
45973
+ this.addCards(cards, true);
45974
+ }
45975
+ addCards(cards, shouldReplaceExistingCards = false) {
45976
+ cards.forEach((card) => {
45977
+ card.setIsInDeck(true);
45978
+ });
45979
+ this.board.bringToFront(cards);
45980
+ this.emit({
45981
+ class: "Deck",
45982
+ method: "addCards",
45983
+ item: [this.getId()],
45984
+ newData: {
45985
+ cardIds: cards.map((card) => card.getId()),
45986
+ shouldReplaceExistingCards
45987
+ },
45988
+ prevData: { cardIds: this.cardIds, shouldReplaceExistingCards }
45989
+ });
45990
+ }
45991
+ removeCards(cards) {
45992
+ cards.forEach((card) => {
45993
+ card.setIsInDeck(false);
45994
+ });
45995
+ this.emit({
45996
+ class: "Deck",
45997
+ method: "removeCards",
45998
+ item: [this.getId()],
45999
+ newData: { cardIds: cards.map((card) => card.getId()) },
46000
+ prevData: { cardIds: this.cardIds }
46001
+ });
46002
+ }
46003
+ apply(op) {
46004
+ super.apply(op);
46005
+ switch (op.class) {
46006
+ case "Deck":
46007
+ switch (op.method) {
46008
+ case "addCards":
46009
+ if (op.newData.shouldReplaceExistingCards) {
46010
+ this.cardIds = op.newData.cardIds;
46011
+ this.cards = this.findCardsOnBoard(this.cardIds);
46012
+ } else {
46013
+ this.cardIds = [
46014
+ ...op.newData.cardIds,
46015
+ ...this.cardIds
46016
+ ];
46017
+ this.updateCards();
46018
+ this.updateMbr();
46019
+ }
46020
+ break;
46021
+ case "removeCards":
46022
+ this.cardIds = this.cardIds.filter((card) => {
46023
+ return !op.newData.cardIds.includes(card);
46024
+ });
46025
+ this.updateCards();
46026
+ this.updateMbr();
46027
+ break;
46028
+ }
46029
+ break;
46030
+ }
46031
+ this.subject.publish(this);
46032
+ }
46033
+ updateMbr() {
46034
+ const { translateX, translateY, scaleX, scaleY } = this.transformation.matrix;
46035
+ this.left = translateX;
46036
+ this.top = translateY;
46037
+ this.right = this.left + CARD_DIMENSIONS.width * scaleX + 2 * this.cardIds.length;
46038
+ this.bottom = this.top + CARD_DIMENSIONS.height * scaleY;
46039
+ }
46040
+ deserialize(data) {
46041
+ super.deserialize(data);
46042
+ this.updateCards();
46043
+ return this;
46044
+ }
46045
+ render(context) {
46046
+ if (this.transformationRenderBlock) {
46047
+ return;
46048
+ }
46049
+ this.cards.forEach((card, index2) => {
46050
+ card.render(context, {
46051
+ top: this.top,
46052
+ left: this.left,
46053
+ cardPosition: index2 + 1
46054
+ });
46055
+ });
46056
+ }
46057
+ }
46058
+ registerItem({
46059
+ item: Deck,
46060
+ defaultData: defaultDeckData
46061
+ });
45748
46062
  // src/Pointer/Cursor.ts
45749
46063
  var defaultCursors = [
45750
46064
  "default",
@@ -46257,27 +46571,6 @@ class Camera {
46257
46571
  requestAnimationFrame(animate);
46258
46572
  }
46259
46573
  }
46260
- function throttle(func, delay) {
46261
- let lastCall = 0;
46262
- let timeoutId = null;
46263
- return function(...args) {
46264
- const now = Date.now();
46265
- if (lastCall + delay <= now) {
46266
- lastCall = now;
46267
- func(...args);
46268
- if (timeoutId) {
46269
- clearTimeout(timeoutId);
46270
- timeoutId = null;
46271
- }
46272
- } else if (!timeoutId) {
46273
- timeoutId = setTimeout(() => {
46274
- lastCall = Date.now();
46275
- timeoutId = null;
46276
- func(...args);
46277
- }, delay - (now - lastCall));
46278
- }
46279
- };
46280
- }
46281
46574
  // src/hotkeys.json
46282
46575
  var hotkeys_default = {
46283
46576
  select: {
@@ -55782,6 +56075,7 @@ export {
55782
56075
  Drawing,
55783
56076
  DefaultTransformationData,
55784
56077
  DefaultShapeData,
56078
+ Deck,
55785
56079
  DEFAULT_SHAPE,
55786
56080
  CubicBezier,
55787
56081
  Counter,
@@ -55789,6 +56083,7 @@ export {
55789
56083
  Connector2 as Connector,
55790
56084
  ConnectionLineWidths,
55791
56085
  Comment,
56086
+ Card,
55792
56087
  Camera,
55793
56088
  CURSORS_IDLE_CLEANUP_DELAY,
55794
56089
  CURSORS_ANIMATION_DURATION,