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.
@@ -746,6 +746,7 @@ __export(exports_browser, {
746
746
  Drawing: () => Drawing,
747
747
  DefaultTransformationData: () => DefaultTransformationData,
748
748
  DefaultShapeData: () => DefaultShapeData,
749
+ Deck: () => Deck,
749
750
  DEFAULT_SHAPE: () => DEFAULT_SHAPE,
750
751
  CubicBezier: () => CubicBezier,
751
752
  Counter: () => Counter,
@@ -753,6 +754,7 @@ __export(exports_browser, {
753
754
  Connector: () => Connector2,
754
755
  ConnectionLineWidths: () => ConnectionLineWidths,
755
756
  Comment: () => Comment,
757
+ Card: () => Card,
756
758
  Camera: () => Camera,
757
759
  CURSORS_IDLE_CLEANUP_DELAY: () => CURSORS_IDLE_CLEANUP_DELAY,
758
760
  CURSORS_ANIMATION_DURATION: () => CURSORS_ANIMATION_DURATION,
@@ -45585,7 +45587,9 @@ function registerItem({
45585
45587
  const { itemType } = defaultData2;
45586
45588
  itemFactories[itemType] = createItemFactory(item, defaultData2);
45587
45589
  itemValidators[itemType] = createItemValidator(defaultData2);
45588
- registeredTools[toolData.name] = toolData.tool;
45590
+ if (toolData) {
45591
+ registeredTools[toolData.name] = toolData.tool;
45592
+ }
45589
45593
  itemCommandFactories[itemType] = createItemCommandFactory(itemType);
45590
45594
  }
45591
45595
  function createItemFactory(item, defaultData2) {
@@ -45899,6 +45903,323 @@ registerItem({
45899
45903
  defaultData: defaultCounterData,
45900
45904
  toolData: { name: "AddCounter", tool: AddCounter }
45901
45905
  });
45906
+ // src/utils.ts
45907
+ function throttle(func, delay) {
45908
+ let lastCall = 0;
45909
+ let timeoutId = null;
45910
+ return function(...args) {
45911
+ const now = Date.now();
45912
+ if (lastCall + delay <= now) {
45913
+ lastCall = now;
45914
+ func(...args);
45915
+ if (timeoutId) {
45916
+ clearTimeout(timeoutId);
45917
+ timeoutId = null;
45918
+ }
45919
+ } else if (!timeoutId) {
45920
+ timeoutId = setTimeout(() => {
45921
+ lastCall = Date.now();
45922
+ timeoutId = null;
45923
+ func(...args);
45924
+ }, delay - (now - lastCall));
45925
+ }
45926
+ };
45927
+ }
45928
+
45929
+ // src/Items/Examples/CardGame/Card/Card.ts
45930
+ var defaultCardData = {
45931
+ itemType: "Card",
45932
+ isOpen: false,
45933
+ faceUrl: "",
45934
+ backsideUrl: "",
45935
+ isInDeck: false
45936
+ };
45937
+ var CARD_DIMENSIONS = { width: 250, height: 400 };
45938
+
45939
+ class Card extends BaseItem {
45940
+ subject = new Subject;
45941
+ faceUrl = "";
45942
+ backsideUrl = "";
45943
+ isOpen = false;
45944
+ isInDeck = false;
45945
+ throttledBringToFront;
45946
+ face = null;
45947
+ backside = null;
45948
+ imageToRender = null;
45949
+ shouldUseCustomRender = false;
45950
+ constructor(board, id = "", urls) {
45951
+ super(board, id, defaultCardData);
45952
+ if (urls) {
45953
+ this.faceUrl = urls.faceUrl;
45954
+ this.backsideUrl = urls.backsideUrl;
45955
+ this.createImages();
45956
+ }
45957
+ this.throttledBringToFront = throttle(() => {
45958
+ this.board.bringToFront(this);
45959
+ }, 1000);
45960
+ this.transformation.subject.subscribe(() => {
45961
+ this.throttledBringToFront();
45962
+ this.updateMbr();
45963
+ this.updateImageToRender();
45964
+ this.subject.publish(this);
45965
+ });
45966
+ this.updateMbr();
45967
+ }
45968
+ createImages() {
45969
+ this.face = conf.documentFactory.createElement("img");
45970
+ this.backside = conf.documentFactory.createElement("img");
45971
+ this.face.src = this.faceUrl;
45972
+ this.backside.src = this.backsideUrl;
45973
+ this.face.onload = () => {
45974
+ this.subject.publish(this);
45975
+ };
45976
+ this.backside.onload = () => {
45977
+ this.subject.publish(this);
45978
+ };
45979
+ }
45980
+ setIsInDeck(isInDeck) {
45981
+ this.emit({
45982
+ class: "Card",
45983
+ method: "setIsInDeck",
45984
+ item: [this.getId()],
45985
+ newData: { isInDeck },
45986
+ prevData: { isInDeck: this.isInDeck }
45987
+ });
45988
+ }
45989
+ updateImageToRender() {
45990
+ this.imageToRender = this.backside;
45991
+ if (this.isOpen) {
45992
+ this.imageToRender = this.face;
45993
+ }
45994
+ }
45995
+ render(context, deckRenderData) {
45996
+ if (this.transformationRenderBlock || this.isInDeck && !deckRenderData) {
45997
+ return;
45998
+ }
45999
+ const ctx = context.ctx;
46000
+ if (this.imageToRender && this.imageToRender.complete) {
46001
+ ctx.save();
46002
+ let left = this.left;
46003
+ let top = this.top;
46004
+ if (deckRenderData) {
46005
+ left = deckRenderData.left + 2 * deckRenderData.cardPosition;
46006
+ top = deckRenderData.top;
46007
+ }
46008
+ ctx.drawImage(this.imageToRender, left, top, CARD_DIMENSIONS.width, CARD_DIMENSIONS.height);
46009
+ ctx.restore();
46010
+ }
46011
+ }
46012
+ updateMbr() {
46013
+ const { translateX, translateY, scaleX, scaleY } = this.transformation.matrix;
46014
+ this.left = translateX;
46015
+ this.top = translateY;
46016
+ this.right = this.left + CARD_DIMENSIONS.width * scaleX;
46017
+ this.bottom = this.top + CARD_DIMENSIONS.height * scaleY;
46018
+ }
46019
+ getMbr() {
46020
+ if (this.isInDeck) {
46021
+ return new Mbr(1e4, 1e4, 1e4, 1e4);
46022
+ }
46023
+ return super.getMbr();
46024
+ }
46025
+ getPath() {
46026
+ return new Path(this.getMbr().getLines());
46027
+ }
46028
+ renderHTML(documentFactory) {
46029
+ const div = documentFactory.createElement("card-item");
46030
+ return div;
46031
+ }
46032
+ deserialize(data) {
46033
+ super.deserialize(data);
46034
+ this.updateMbr();
46035
+ this.createImages();
46036
+ this.subject.publish(this);
46037
+ return this;
46038
+ }
46039
+ toggleIsOpen() {
46040
+ this.emit({
46041
+ class: "Card",
46042
+ method: "setIsOpen",
46043
+ item: [this.getId()],
46044
+ newData: { isOpen: !this.isOpen },
46045
+ prevData: { isOpen: this.isOpen }
46046
+ });
46047
+ }
46048
+ apply(op) {
46049
+ super.apply(op);
46050
+ switch (op.class) {
46051
+ case "Card":
46052
+ switch (op.method) {
46053
+ case "setIsOpen":
46054
+ this.isOpen = op.newData.isOpen;
46055
+ this.updateImageToRender();
46056
+ break;
46057
+ case "setIsInDeck":
46058
+ this.isInDeck = op.newData.isInDeck;
46059
+ break;
46060
+ }
46061
+ break;
46062
+ }
46063
+ this.subject.publish(this);
46064
+ }
46065
+ }
46066
+ registerItem({
46067
+ item: Card,
46068
+ defaultData: defaultCardData
46069
+ });
46070
+ // src/Items/Examples/CardGame/Deck/Deck.ts
46071
+ var defaultDeckData = {
46072
+ itemType: "Deck",
46073
+ cardIds: []
46074
+ };
46075
+
46076
+ class Deck extends BaseItem {
46077
+ subject = new Subject;
46078
+ shouldUseCustomRender = false;
46079
+ cardIds = [];
46080
+ cards = [];
46081
+ constructor(board, id = "", defaultData2, cards) {
46082
+ super(board, id, defaultDeckData);
46083
+ if (cards) {
46084
+ this.cards = cards;
46085
+ cards.forEach((card) => card.setIsInDeck(true));
46086
+ this.transformation.matrix = cards[cards.length - 1].transformation.matrix;
46087
+ this.cardIds = cards.map((card) => card.getId());
46088
+ }
46089
+ this.transformation.subject.subscribe(() => {
46090
+ this.updateMbr();
46091
+ this.subject.publish(this);
46092
+ });
46093
+ this.updateMbr();
46094
+ }
46095
+ getDeck() {
46096
+ return this.cards;
46097
+ }
46098
+ getTopCard() {
46099
+ const cardId = this.cardIds[this.cardIds.length - 1];
46100
+ return this.getCards([cardId])[0];
46101
+ }
46102
+ getBottomCard() {
46103
+ const cardId = this.cardIds[0];
46104
+ return this.getCards([cardId])[0];
46105
+ }
46106
+ getRandomCard() {
46107
+ const cardId = this.cardIds[Math.ceil(Math.random() * this.cardIds.length) - 1];
46108
+ return this.getCards([cardId])[0];
46109
+ }
46110
+ getCards(cardIds) {
46111
+ const cards = this.findCardsOnBoard(cardIds);
46112
+ this.removeCards(cards);
46113
+ return cards;
46114
+ }
46115
+ findCardsOnBoard(cardIds) {
46116
+ return cardIds.map((cardId) => {
46117
+ return this.board.items.getById(cardId);
46118
+ }).filter((card) => !!card);
46119
+ }
46120
+ updateCards() {
46121
+ if (this.cardIds.length === this.cards.length) {
46122
+ return this.cards;
46123
+ }
46124
+ this.cards = this.findCardsOnBoard(this.cardIds);
46125
+ return this.cards;
46126
+ }
46127
+ shuffleDeck() {
46128
+ const shuffled = [...this.cardIds];
46129
+ for (let i = shuffled.length - 1;i > 0; i--) {
46130
+ const j = Math.floor(Math.random() * (i + 1));
46131
+ [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
46132
+ }
46133
+ const cards = this.findCardsOnBoard(shuffled);
46134
+ this.addCards(cards, true);
46135
+ }
46136
+ addCards(cards, shouldReplaceExistingCards = false) {
46137
+ cards.forEach((card) => {
46138
+ card.setIsInDeck(true);
46139
+ });
46140
+ this.board.bringToFront(cards);
46141
+ this.emit({
46142
+ class: "Deck",
46143
+ method: "addCards",
46144
+ item: [this.getId()],
46145
+ newData: {
46146
+ cardIds: cards.map((card) => card.getId()),
46147
+ shouldReplaceExistingCards
46148
+ },
46149
+ prevData: { cardIds: this.cardIds, shouldReplaceExistingCards }
46150
+ });
46151
+ }
46152
+ removeCards(cards) {
46153
+ cards.forEach((card) => {
46154
+ card.setIsInDeck(false);
46155
+ });
46156
+ this.emit({
46157
+ class: "Deck",
46158
+ method: "removeCards",
46159
+ item: [this.getId()],
46160
+ newData: { cardIds: cards.map((card) => card.getId()) },
46161
+ prevData: { cardIds: this.cardIds }
46162
+ });
46163
+ }
46164
+ apply(op) {
46165
+ super.apply(op);
46166
+ switch (op.class) {
46167
+ case "Deck":
46168
+ switch (op.method) {
46169
+ case "addCards":
46170
+ if (op.newData.shouldReplaceExistingCards) {
46171
+ this.cardIds = op.newData.cardIds;
46172
+ this.cards = this.findCardsOnBoard(this.cardIds);
46173
+ } else {
46174
+ this.cardIds = [
46175
+ ...op.newData.cardIds,
46176
+ ...this.cardIds
46177
+ ];
46178
+ this.updateCards();
46179
+ this.updateMbr();
46180
+ }
46181
+ break;
46182
+ case "removeCards":
46183
+ this.cardIds = this.cardIds.filter((card) => {
46184
+ return !op.newData.cardIds.includes(card);
46185
+ });
46186
+ this.updateCards();
46187
+ this.updateMbr();
46188
+ break;
46189
+ }
46190
+ break;
46191
+ }
46192
+ this.subject.publish(this);
46193
+ }
46194
+ updateMbr() {
46195
+ const { translateX, translateY, scaleX, scaleY } = this.transformation.matrix;
46196
+ this.left = translateX;
46197
+ this.top = translateY;
46198
+ this.right = this.left + CARD_DIMENSIONS.width * scaleX + 2 * this.cardIds.length;
46199
+ this.bottom = this.top + CARD_DIMENSIONS.height * scaleY;
46200
+ }
46201
+ deserialize(data) {
46202
+ super.deserialize(data);
46203
+ this.updateCards();
46204
+ return this;
46205
+ }
46206
+ render(context) {
46207
+ if (this.transformationRenderBlock) {
46208
+ return;
46209
+ }
46210
+ this.cards.forEach((card, index2) => {
46211
+ card.render(context, {
46212
+ top: this.top,
46213
+ left: this.left,
46214
+ cardPosition: index2 + 1
46215
+ });
46216
+ });
46217
+ }
46218
+ }
46219
+ registerItem({
46220
+ item: Deck,
46221
+ defaultData: defaultDeckData
46222
+ });
45902
46223
  // src/Pointer/Cursor.ts
45903
46224
  var defaultCursors = [
45904
46225
  "default",
@@ -46411,27 +46732,6 @@ class Camera {
46411
46732
  requestAnimationFrame(animate);
46412
46733
  }
46413
46734
  }
46414
- function throttle(func, delay) {
46415
- let lastCall = 0;
46416
- let timeoutId = null;
46417
- return function(...args) {
46418
- const now = Date.now();
46419
- if (lastCall + delay <= now) {
46420
- lastCall = now;
46421
- func(...args);
46422
- if (timeoutId) {
46423
- clearTimeout(timeoutId);
46424
- timeoutId = null;
46425
- }
46426
- } else if (!timeoutId) {
46427
- timeoutId = setTimeout(() => {
46428
- lastCall = Date.now();
46429
- timeoutId = null;
46430
- func(...args);
46431
- }, delay - (now - lastCall));
46432
- }
46433
- };
46434
- }
46435
46735
  // src/hotkeys.json
46436
46736
  var hotkeys_default = {
46437
46737
  select: {