microboard-temp 0.4.10 → 0.4.12

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,323 @@ 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
+ faceUrl: "",
45778
+ backsideUrl: "",
45779
+ isInDeck: false
45780
+ };
45781
+ var CARD_DIMENSIONS = { width: 250, height: 400 };
45782
+
45783
+ class Card extends BaseItem {
45784
+ subject = new Subject;
45785
+ faceUrl = "";
45786
+ backsideUrl = "";
45787
+ isOpen = false;
45788
+ isInDeck = false;
45789
+ throttledBringToFront;
45790
+ face = null;
45791
+ backside = null;
45792
+ imageToRender = null;
45793
+ shouldUseCustomRender = false;
45794
+ constructor(board, id = "", urls) {
45795
+ super(board, id, defaultCardData);
45796
+ if (urls) {
45797
+ this.faceUrl = urls.faceUrl;
45798
+ this.backsideUrl = urls.backsideUrl;
45799
+ this.createImages();
45800
+ }
45801
+ this.throttledBringToFront = throttle(() => {
45802
+ this.board.bringToFront(this);
45803
+ }, 1000);
45804
+ this.transformation.subject.subscribe(() => {
45805
+ this.throttledBringToFront();
45806
+ this.updateMbr();
45807
+ this.updateImageToRender();
45808
+ this.subject.publish(this);
45809
+ });
45810
+ this.updateMbr();
45811
+ }
45812
+ createImages() {
45813
+ this.face = conf.documentFactory.createElement("img");
45814
+ this.backside = conf.documentFactory.createElement("img");
45815
+ this.face.src = this.faceUrl;
45816
+ this.backside.src = this.backsideUrl;
45817
+ this.face.onload = () => {
45818
+ this.subject.publish(this);
45819
+ };
45820
+ this.backside.onload = () => {
45821
+ this.subject.publish(this);
45822
+ };
45823
+ }
45824
+ setIsInDeck(isInDeck) {
45825
+ this.emit({
45826
+ class: "Card",
45827
+ method: "setIsInDeck",
45828
+ item: [this.getId()],
45829
+ newData: { isInDeck },
45830
+ prevData: { isInDeck: this.isInDeck }
45831
+ });
45832
+ }
45833
+ updateImageToRender() {
45834
+ this.imageToRender = this.backside;
45835
+ if (this.isOpen) {
45836
+ this.imageToRender = this.face;
45837
+ }
45838
+ }
45839
+ render(context, deckRenderData) {
45840
+ if (this.transformationRenderBlock || this.isInDeck && !deckRenderData) {
45841
+ return;
45842
+ }
45843
+ const ctx = context.ctx;
45844
+ if (this.imageToRender && this.imageToRender.complete) {
45845
+ ctx.save();
45846
+ let left = this.left;
45847
+ let top = this.top;
45848
+ if (deckRenderData) {
45849
+ left = deckRenderData.left + 2 * deckRenderData.cardPosition;
45850
+ top = deckRenderData.top;
45851
+ }
45852
+ ctx.drawImage(this.imageToRender, left, top, CARD_DIMENSIONS.width, CARD_DIMENSIONS.height);
45853
+ ctx.restore();
45854
+ }
45855
+ }
45856
+ updateMbr() {
45857
+ const { translateX, translateY, scaleX, scaleY } = this.transformation.matrix;
45858
+ this.left = translateX;
45859
+ this.top = translateY;
45860
+ this.right = this.left + CARD_DIMENSIONS.width * scaleX;
45861
+ this.bottom = this.top + CARD_DIMENSIONS.height * scaleY;
45862
+ }
45863
+ getMbr() {
45864
+ if (this.isInDeck) {
45865
+ return new Mbr(1e4, 1e4, 1e4, 1e4);
45866
+ }
45867
+ return super.getMbr();
45868
+ }
45869
+ getPath() {
45870
+ return new Path(this.getMbr().getLines());
45871
+ }
45872
+ renderHTML(documentFactory) {
45873
+ const div = documentFactory.createElement("card-item");
45874
+ return div;
45875
+ }
45876
+ deserialize(data) {
45877
+ super.deserialize(data);
45878
+ this.updateMbr();
45879
+ this.createImages();
45880
+ this.subject.publish(this);
45881
+ return this;
45882
+ }
45883
+ toggleIsOpen() {
45884
+ this.emit({
45885
+ class: "Card",
45886
+ method: "setIsOpen",
45887
+ item: [this.getId()],
45888
+ newData: { isOpen: !this.isOpen },
45889
+ prevData: { isOpen: this.isOpen }
45890
+ });
45891
+ }
45892
+ apply(op) {
45893
+ super.apply(op);
45894
+ switch (op.class) {
45895
+ case "Card":
45896
+ switch (op.method) {
45897
+ case "setIsOpen":
45898
+ this.isOpen = op.newData.isOpen;
45899
+ this.updateImageToRender();
45900
+ break;
45901
+ case "setIsInDeck":
45902
+ this.isInDeck = op.newData.isInDeck;
45903
+ break;
45904
+ }
45905
+ break;
45906
+ }
45907
+ this.subject.publish(this);
45908
+ }
45909
+ }
45910
+ registerItem({
45911
+ item: Card,
45912
+ defaultData: defaultCardData
45913
+ });
45914
+ // src/Items/Examples/CardGame/Deck/Deck.ts
45915
+ var defaultDeckData = {
45916
+ itemType: "Deck",
45917
+ cardIds: []
45918
+ };
45919
+
45920
+ class Deck extends BaseItem {
45921
+ subject = new Subject;
45922
+ shouldUseCustomRender = false;
45923
+ cardIds = [];
45924
+ cards = [];
45925
+ constructor(board, id = "", defaultData2, cards) {
45926
+ super(board, id, defaultDeckData);
45927
+ if (cards) {
45928
+ this.cards = cards;
45929
+ cards.forEach((card) => card.setIsInDeck(true));
45930
+ this.transformation.matrix = cards[cards.length - 1].transformation.matrix;
45931
+ this.cardIds = cards.map((card) => card.getId());
45932
+ }
45933
+ this.transformation.subject.subscribe(() => {
45934
+ this.updateMbr();
45935
+ this.subject.publish(this);
45936
+ });
45937
+ this.updateMbr();
45938
+ }
45939
+ getDeck() {
45940
+ return this.cards;
45941
+ }
45942
+ getTopCard() {
45943
+ const cardId = this.cardIds[this.cardIds.length - 1];
45944
+ return this.getCards([cardId])[0];
45945
+ }
45946
+ getBottomCard() {
45947
+ const cardId = this.cardIds[0];
45948
+ return this.getCards([cardId])[0];
45949
+ }
45950
+ getRandomCard() {
45951
+ const cardId = this.cardIds[Math.ceil(Math.random() * this.cardIds.length) - 1];
45952
+ return this.getCards([cardId])[0];
45953
+ }
45954
+ getCards(cardIds) {
45955
+ const cards = this.findCardsOnBoard(cardIds);
45956
+ this.removeCards(cards);
45957
+ return cards;
45958
+ }
45959
+ findCardsOnBoard(cardIds) {
45960
+ return cardIds.map((cardId) => {
45961
+ return this.board.items.getById(cardId);
45962
+ }).filter((card) => !!card);
45963
+ }
45964
+ updateCards() {
45965
+ if (this.cardIds.length === this.cards.length) {
45966
+ return this.cards;
45967
+ }
45968
+ this.cards = this.findCardsOnBoard(this.cardIds);
45969
+ return this.cards;
45970
+ }
45971
+ shuffleDeck() {
45972
+ const shuffled = [...this.cardIds];
45973
+ for (let i = shuffled.length - 1;i > 0; i--) {
45974
+ const j = Math.floor(Math.random() * (i + 1));
45975
+ [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
45976
+ }
45977
+ const cards = this.findCardsOnBoard(shuffled);
45978
+ this.addCards(cards, true);
45979
+ }
45980
+ addCards(cards, shouldReplaceExistingCards = false) {
45981
+ cards.forEach((card) => {
45982
+ card.setIsInDeck(true);
45983
+ });
45984
+ this.board.bringToFront(cards);
45985
+ this.emit({
45986
+ class: "Deck",
45987
+ method: "addCards",
45988
+ item: [this.getId()],
45989
+ newData: {
45990
+ cardIds: cards.map((card) => card.getId()),
45991
+ shouldReplaceExistingCards
45992
+ },
45993
+ prevData: { cardIds: this.cardIds, shouldReplaceExistingCards }
45994
+ });
45995
+ }
45996
+ removeCards(cards) {
45997
+ cards.forEach((card) => {
45998
+ card.setIsInDeck(false);
45999
+ });
46000
+ this.emit({
46001
+ class: "Deck",
46002
+ method: "removeCards",
46003
+ item: [this.getId()],
46004
+ newData: { cardIds: cards.map((card) => card.getId()) },
46005
+ prevData: { cardIds: this.cardIds }
46006
+ });
46007
+ }
46008
+ apply(op) {
46009
+ super.apply(op);
46010
+ switch (op.class) {
46011
+ case "Deck":
46012
+ switch (op.method) {
46013
+ case "addCards":
46014
+ if (op.newData.shouldReplaceExistingCards) {
46015
+ this.cardIds = op.newData.cardIds;
46016
+ this.cards = this.findCardsOnBoard(this.cardIds);
46017
+ } else {
46018
+ this.cardIds = [
46019
+ ...op.newData.cardIds,
46020
+ ...this.cardIds
46021
+ ];
46022
+ this.updateCards();
46023
+ this.updateMbr();
46024
+ }
46025
+ break;
46026
+ case "removeCards":
46027
+ this.cardIds = this.cardIds.filter((card) => {
46028
+ return !op.newData.cardIds.includes(card);
46029
+ });
46030
+ this.updateCards();
46031
+ this.updateMbr();
46032
+ break;
46033
+ }
46034
+ break;
46035
+ }
46036
+ this.subject.publish(this);
46037
+ }
46038
+ updateMbr() {
46039
+ const { translateX, translateY, scaleX, scaleY } = this.transformation.matrix;
46040
+ this.left = translateX;
46041
+ this.top = translateY;
46042
+ this.right = this.left + CARD_DIMENSIONS.width * scaleX + 2 * this.cardIds.length;
46043
+ this.bottom = this.top + CARD_DIMENSIONS.height * scaleY;
46044
+ }
46045
+ deserialize(data) {
46046
+ super.deserialize(data);
46047
+ this.updateCards();
46048
+ return this;
46049
+ }
46050
+ render(context) {
46051
+ if (this.transformationRenderBlock) {
46052
+ return;
46053
+ }
46054
+ this.cards.forEach((card, index2) => {
46055
+ card.render(context, {
46056
+ top: this.top,
46057
+ left: this.left,
46058
+ cardPosition: index2 + 1
46059
+ });
46060
+ });
46061
+ }
46062
+ }
46063
+ registerItem({
46064
+ item: Deck,
46065
+ defaultData: defaultDeckData
46066
+ });
45748
46067
  // src/Pointer/Cursor.ts
45749
46068
  var defaultCursors = [
45750
46069
  "default",
@@ -46257,27 +46576,6 @@ class Camera {
46257
46576
  requestAnimationFrame(animate);
46258
46577
  }
46259
46578
  }
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
46579
  // src/hotkeys.json
46282
46580
  var hotkeys_default = {
46283
46581
  select: {
@@ -55782,6 +56080,7 @@ export {
55782
56080
  Drawing,
55783
56081
  DefaultTransformationData,
55784
56082
  DefaultShapeData,
56083
+ Deck,
55785
56084
  DEFAULT_SHAPE,
55786
56085
  CubicBezier,
55787
56086
  Counter,
@@ -55789,6 +56088,7 @@ export {
55789
56088
  Connector2 as Connector,
55790
56089
  ConnectionLineWidths,
55791
56090
  Comment,
56091
+ Card,
55792
56092
  Camera,
55793
56093
  CURSORS_IDLE_CLEANUP_DELAY,
55794
56094
  CURSORS_ANIMATION_DURATION,