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/node.js CHANGED
@@ -47966,7 +47966,9 @@ function registerItem({
47966
47966
  const { itemType } = defaultData2;
47967
47967
  itemFactories[itemType] = createItemFactory(item, defaultData2);
47968
47968
  itemValidators[itemType] = createItemValidator(defaultData2);
47969
- registeredTools[toolData.name] = toolData.tool;
47969
+ if (toolData) {
47970
+ registeredTools[toolData.name] = toolData.tool;
47971
+ }
47970
47972
  itemCommandFactories[itemType] = createItemCommandFactory(itemType);
47971
47973
  }
47972
47974
  function createItemFactory(item, defaultData2) {
@@ -48280,6 +48282,323 @@ registerItem({
48280
48282
  defaultData: defaultCounterData,
48281
48283
  toolData: { name: "AddCounter", tool: AddCounter }
48282
48284
  });
48285
+ // src/utils.ts
48286
+ function throttle(func, delay) {
48287
+ let lastCall = 0;
48288
+ let timeoutId = null;
48289
+ return function(...args) {
48290
+ const now = Date.now();
48291
+ if (lastCall + delay <= now) {
48292
+ lastCall = now;
48293
+ func(...args);
48294
+ if (timeoutId) {
48295
+ clearTimeout(timeoutId);
48296
+ timeoutId = null;
48297
+ }
48298
+ } else if (!timeoutId) {
48299
+ timeoutId = setTimeout(() => {
48300
+ lastCall = Date.now();
48301
+ timeoutId = null;
48302
+ func(...args);
48303
+ }, delay - (now - lastCall));
48304
+ }
48305
+ };
48306
+ }
48307
+
48308
+ // src/Items/Examples/CardGame/Card/Card.ts
48309
+ var defaultCardData = {
48310
+ itemType: "Card",
48311
+ isOpen: false,
48312
+ faceUrl: "",
48313
+ backsideUrl: "",
48314
+ isInDeck: false
48315
+ };
48316
+ var CARD_DIMENSIONS = { width: 250, height: 400 };
48317
+
48318
+ class Card extends BaseItem {
48319
+ subject = new Subject;
48320
+ faceUrl = "";
48321
+ backsideUrl = "";
48322
+ isOpen = false;
48323
+ isInDeck = false;
48324
+ throttledBringToFront;
48325
+ face = null;
48326
+ backside = null;
48327
+ imageToRender = null;
48328
+ shouldUseCustomRender = false;
48329
+ constructor(board, id = "", urls) {
48330
+ super(board, id, defaultCardData);
48331
+ if (urls) {
48332
+ this.faceUrl = urls.faceUrl;
48333
+ this.backsideUrl = urls.backsideUrl;
48334
+ this.createImages();
48335
+ }
48336
+ this.throttledBringToFront = throttle(() => {
48337
+ this.board.bringToFront(this);
48338
+ }, 1000);
48339
+ this.transformation.subject.subscribe(() => {
48340
+ this.throttledBringToFront();
48341
+ this.updateMbr();
48342
+ this.updateImageToRender();
48343
+ this.subject.publish(this);
48344
+ });
48345
+ this.updateMbr();
48346
+ }
48347
+ createImages() {
48348
+ this.face = conf.documentFactory.createElement("img");
48349
+ this.backside = conf.documentFactory.createElement("img");
48350
+ this.face.src = this.faceUrl;
48351
+ this.backside.src = this.backsideUrl;
48352
+ this.face.onload = () => {
48353
+ this.subject.publish(this);
48354
+ };
48355
+ this.backside.onload = () => {
48356
+ this.subject.publish(this);
48357
+ };
48358
+ }
48359
+ setIsInDeck(isInDeck) {
48360
+ this.emit({
48361
+ class: "Card",
48362
+ method: "setIsInDeck",
48363
+ item: [this.getId()],
48364
+ newData: { isInDeck },
48365
+ prevData: { isInDeck: this.isInDeck }
48366
+ });
48367
+ }
48368
+ updateImageToRender() {
48369
+ this.imageToRender = this.backside;
48370
+ if (this.isOpen) {
48371
+ this.imageToRender = this.face;
48372
+ }
48373
+ }
48374
+ render(context, deckRenderData) {
48375
+ if (this.transformationRenderBlock || this.isInDeck && !deckRenderData) {
48376
+ return;
48377
+ }
48378
+ const ctx = context.ctx;
48379
+ if (this.imageToRender && this.imageToRender.complete) {
48380
+ ctx.save();
48381
+ let left = this.left;
48382
+ let top = this.top;
48383
+ if (deckRenderData) {
48384
+ left = deckRenderData.left + 2 * deckRenderData.cardPosition;
48385
+ top = deckRenderData.top;
48386
+ }
48387
+ ctx.drawImage(this.imageToRender, left, top, CARD_DIMENSIONS.width, CARD_DIMENSIONS.height);
48388
+ ctx.restore();
48389
+ }
48390
+ }
48391
+ updateMbr() {
48392
+ const { translateX, translateY, scaleX, scaleY } = this.transformation.matrix;
48393
+ this.left = translateX;
48394
+ this.top = translateY;
48395
+ this.right = this.left + CARD_DIMENSIONS.width * scaleX;
48396
+ this.bottom = this.top + CARD_DIMENSIONS.height * scaleY;
48397
+ }
48398
+ getMbr() {
48399
+ if (this.isInDeck) {
48400
+ return new Mbr(1e4, 1e4, 1e4, 1e4);
48401
+ }
48402
+ return super.getMbr();
48403
+ }
48404
+ getPath() {
48405
+ return new Path(this.getMbr().getLines());
48406
+ }
48407
+ renderHTML(documentFactory) {
48408
+ const div = documentFactory.createElement("card-item");
48409
+ return div;
48410
+ }
48411
+ deserialize(data) {
48412
+ super.deserialize(data);
48413
+ this.updateMbr();
48414
+ this.createImages();
48415
+ this.subject.publish(this);
48416
+ return this;
48417
+ }
48418
+ toggleIsOpen() {
48419
+ this.emit({
48420
+ class: "Card",
48421
+ method: "setIsOpen",
48422
+ item: [this.getId()],
48423
+ newData: { isOpen: !this.isOpen },
48424
+ prevData: { isOpen: this.isOpen }
48425
+ });
48426
+ }
48427
+ apply(op) {
48428
+ super.apply(op);
48429
+ switch (op.class) {
48430
+ case "Card":
48431
+ switch (op.method) {
48432
+ case "setIsOpen":
48433
+ this.isOpen = op.newData.isOpen;
48434
+ this.updateImageToRender();
48435
+ break;
48436
+ case "setIsInDeck":
48437
+ this.isInDeck = op.newData.isInDeck;
48438
+ break;
48439
+ }
48440
+ break;
48441
+ }
48442
+ this.subject.publish(this);
48443
+ }
48444
+ }
48445
+ registerItem({
48446
+ item: Card,
48447
+ defaultData: defaultCardData
48448
+ });
48449
+ // src/Items/Examples/CardGame/Deck/Deck.ts
48450
+ var defaultDeckData = {
48451
+ itemType: "Deck",
48452
+ cardIds: []
48453
+ };
48454
+
48455
+ class Deck extends BaseItem {
48456
+ subject = new Subject;
48457
+ shouldUseCustomRender = false;
48458
+ cardIds = [];
48459
+ cards = [];
48460
+ constructor(board, id = "", defaultData2, cards) {
48461
+ super(board, id, defaultDeckData);
48462
+ if (cards) {
48463
+ this.cards = cards;
48464
+ cards.forEach((card) => card.setIsInDeck(true));
48465
+ this.transformation.matrix = cards[cards.length - 1].transformation.matrix;
48466
+ this.cardIds = cards.map((card) => card.getId());
48467
+ }
48468
+ this.transformation.subject.subscribe(() => {
48469
+ this.updateMbr();
48470
+ this.subject.publish(this);
48471
+ });
48472
+ this.updateMbr();
48473
+ }
48474
+ getDeck() {
48475
+ return this.cards;
48476
+ }
48477
+ getTopCard() {
48478
+ const cardId = this.cardIds[this.cardIds.length - 1];
48479
+ return this.getCards([cardId])[0];
48480
+ }
48481
+ getBottomCard() {
48482
+ const cardId = this.cardIds[0];
48483
+ return this.getCards([cardId])[0];
48484
+ }
48485
+ getRandomCard() {
48486
+ const cardId = this.cardIds[Math.ceil(Math.random() * this.cardIds.length) - 1];
48487
+ return this.getCards([cardId])[0];
48488
+ }
48489
+ getCards(cardIds) {
48490
+ const cards = this.findCardsOnBoard(cardIds);
48491
+ this.removeCards(cards);
48492
+ return cards;
48493
+ }
48494
+ findCardsOnBoard(cardIds) {
48495
+ return cardIds.map((cardId) => {
48496
+ return this.board.items.getById(cardId);
48497
+ }).filter((card) => !!card);
48498
+ }
48499
+ updateCards() {
48500
+ if (this.cardIds.length === this.cards.length) {
48501
+ return this.cards;
48502
+ }
48503
+ this.cards = this.findCardsOnBoard(this.cardIds);
48504
+ return this.cards;
48505
+ }
48506
+ shuffleDeck() {
48507
+ const shuffled = [...this.cardIds];
48508
+ for (let i = shuffled.length - 1;i > 0; i--) {
48509
+ const j = Math.floor(Math.random() * (i + 1));
48510
+ [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
48511
+ }
48512
+ const cards = this.findCardsOnBoard(shuffled);
48513
+ this.addCards(cards, true);
48514
+ }
48515
+ addCards(cards, shouldReplaceExistingCards = false) {
48516
+ cards.forEach((card) => {
48517
+ card.setIsInDeck(true);
48518
+ });
48519
+ this.board.bringToFront(cards);
48520
+ this.emit({
48521
+ class: "Deck",
48522
+ method: "addCards",
48523
+ item: [this.getId()],
48524
+ newData: {
48525
+ cardIds: cards.map((card) => card.getId()),
48526
+ shouldReplaceExistingCards
48527
+ },
48528
+ prevData: { cardIds: this.cardIds, shouldReplaceExistingCards }
48529
+ });
48530
+ }
48531
+ removeCards(cards) {
48532
+ cards.forEach((card) => {
48533
+ card.setIsInDeck(false);
48534
+ });
48535
+ this.emit({
48536
+ class: "Deck",
48537
+ method: "removeCards",
48538
+ item: [this.getId()],
48539
+ newData: { cardIds: cards.map((card) => card.getId()) },
48540
+ prevData: { cardIds: this.cardIds }
48541
+ });
48542
+ }
48543
+ apply(op) {
48544
+ super.apply(op);
48545
+ switch (op.class) {
48546
+ case "Deck":
48547
+ switch (op.method) {
48548
+ case "addCards":
48549
+ if (op.newData.shouldReplaceExistingCards) {
48550
+ this.cardIds = op.newData.cardIds;
48551
+ this.cards = this.findCardsOnBoard(this.cardIds);
48552
+ } else {
48553
+ this.cardIds = [
48554
+ ...op.newData.cardIds,
48555
+ ...this.cardIds
48556
+ ];
48557
+ this.updateCards();
48558
+ this.updateMbr();
48559
+ }
48560
+ break;
48561
+ case "removeCards":
48562
+ this.cardIds = this.cardIds.filter((card) => {
48563
+ return !op.newData.cardIds.includes(card);
48564
+ });
48565
+ this.updateCards();
48566
+ this.updateMbr();
48567
+ break;
48568
+ }
48569
+ break;
48570
+ }
48571
+ this.subject.publish(this);
48572
+ }
48573
+ updateMbr() {
48574
+ const { translateX, translateY, scaleX, scaleY } = this.transformation.matrix;
48575
+ this.left = translateX;
48576
+ this.top = translateY;
48577
+ this.right = this.left + CARD_DIMENSIONS.width * scaleX + 2 * this.cardIds.length;
48578
+ this.bottom = this.top + CARD_DIMENSIONS.height * scaleY;
48579
+ }
48580
+ deserialize(data) {
48581
+ super.deserialize(data);
48582
+ this.updateCards();
48583
+ return this;
48584
+ }
48585
+ render(context) {
48586
+ if (this.transformationRenderBlock) {
48587
+ return;
48588
+ }
48589
+ this.cards.forEach((card, index2) => {
48590
+ card.render(context, {
48591
+ top: this.top,
48592
+ left: this.left,
48593
+ cardPosition: index2 + 1
48594
+ });
48595
+ });
48596
+ }
48597
+ }
48598
+ registerItem({
48599
+ item: Deck,
48600
+ defaultData: defaultDeckData
48601
+ });
48283
48602
  // src/Pointer/Cursor.ts
48284
48603
  var defaultCursors = [
48285
48604
  "default",
@@ -48792,27 +49111,6 @@ class Camera {
48792
49111
  requestAnimationFrame(animate);
48793
49112
  }
48794
49113
  }
48795
- function throttle(func, delay) {
48796
- let lastCall = 0;
48797
- let timeoutId = null;
48798
- return function(...args) {
48799
- const now = Date.now();
48800
- if (lastCall + delay <= now) {
48801
- lastCall = now;
48802
- func(...args);
48803
- if (timeoutId) {
48804
- clearTimeout(timeoutId);
48805
- timeoutId = null;
48806
- }
48807
- } else if (!timeoutId) {
48808
- timeoutId = setTimeout(() => {
48809
- lastCall = Date.now();
48810
- timeoutId = null;
48811
- func(...args);
48812
- }, delay - (now - lastCall));
48813
- }
48814
- };
48815
- }
48816
49114
  // src/hotkeys.json
48817
49115
  var hotkeys_default = {
48818
49116
  select: {
@@ -58417,6 +58715,7 @@ export {
58417
58715
  Drawing,
58418
58716
  DefaultTransformationData,
58419
58717
  DefaultShapeData,
58718
+ Deck,
58420
58719
  DEFAULT_SHAPE,
58421
58720
  CubicBezier,
58422
58721
  Counter,
@@ -58424,6 +58723,7 @@ export {
58424
58723
  Connector2 as Connector,
58425
58724
  ConnectionLineWidths,
58426
58725
  Comment,
58726
+ Card,
58427
58727
  Camera,
58428
58728
  CURSORS_IDLE_CLEANUP_DELAY,
58429
58729
  CURSORS_ANIMATION_DURATION,
@@ -85,4 +85,3 @@ export declare class Camera {
85
85
  onWindowResize(): void;
86
86
  smoothTranslateTo(keyboard: Keyboard, shouldTranslate: boolean): void;
87
87
  }
88
- export declare function throttle<T extends (...args: any[]) => unknown>(func: T, delay: number): (...args: Parameters<T>) => void;
@@ -0,0 +1,46 @@
1
+ import { BaseItem, BaseItemData, SerializedItemData } from "Items/BaseItem/BaseItem";
2
+ import { Board } from "Board";
3
+ import { DrawingContext } from "Items/DrawingContext";
4
+ import { DocumentFactory } from "api/DocumentFactory";
5
+ import { Path } from "Items/Path/Path";
6
+ import { Subject } from "Subject";
7
+ import { Paths } from "Items/Path/Paths";
8
+ import { CardOperation } from "Items/Examples/CardGame/Card/CardOperation";
9
+ import { Mbr } from "Items/Mbr/Mbr";
10
+ export type DeckRenderData = {
11
+ left: number;
12
+ top: number;
13
+ cardPosition: number;
14
+ };
15
+ export declare const defaultCardData: BaseItemData;
16
+ export declare const CARD_DIMENSIONS: {
17
+ width: number;
18
+ height: number;
19
+ };
20
+ export declare class Card extends BaseItem {
21
+ readonly subject: Subject<Card>;
22
+ private faceUrl;
23
+ private backsideUrl;
24
+ private isOpen;
25
+ private isInDeck;
26
+ private throttledBringToFront;
27
+ face: HTMLImageElement | null;
28
+ backside: HTMLImageElement | null;
29
+ private imageToRender;
30
+ shouldUseCustomRender: boolean;
31
+ constructor(board: Board, id?: string, urls?: {
32
+ faceUrl: string;
33
+ backsideUrl: string;
34
+ });
35
+ createImages(): void;
36
+ setIsInDeck(isInDeck: boolean): void;
37
+ updateImageToRender(): void;
38
+ render(context: DrawingContext, deckRenderData?: DeckRenderData): void;
39
+ updateMbr(): void;
40
+ getMbr(): Mbr;
41
+ getPath(): Path | Paths;
42
+ renderHTML(documentFactory: DocumentFactory): HTMLElement;
43
+ deserialize(data: SerializedItemData): this;
44
+ toggleIsOpen(): void;
45
+ apply(op: CardOperation): void;
46
+ }
@@ -0,0 +1,14 @@
1
+ import { BaseOperation } from "Events/EventsOperations";
2
+ export type CardOperation = SetIsOpen | SetIsInDeck;
3
+ export interface SetIsOpen extends BaseOperation<{
4
+ isOpen: boolean;
5
+ }> {
6
+ class: "Card";
7
+ method: "setIsOpen";
8
+ }
9
+ export interface SetIsInDeck extends BaseOperation<{
10
+ isInDeck: boolean;
11
+ }> {
12
+ class: "Card";
13
+ method: "setIsInDeck";
14
+ }
@@ -0,0 +1 @@
1
+ export { Card } from "./Card";
@@ -0,0 +1,28 @@
1
+ import { BaseItem, BaseItemData, SerializedItemData } from "Items/BaseItem/BaseItem";
2
+ import { Board } from "Board";
3
+ import { Subject } from "Subject";
4
+ import { Card } from "Items/Examples/CardGame/Card/Card";
5
+ import { DrawingContext } from "Items/DrawingContext";
6
+ import { DeckOperation } from "Items/Examples/CardGame/Deck/DeckOperation";
7
+ export declare const defaultDeckData: BaseItemData;
8
+ export declare class Deck extends BaseItem {
9
+ readonly subject: Subject<Deck>;
10
+ shouldUseCustomRender: boolean;
11
+ cardIds: string[];
12
+ cards: Card[];
13
+ constructor(board: Board, id?: string, defaultData?: BaseItemData, cards?: Card[]);
14
+ getDeck(): Card[];
15
+ getTopCard(): Card | undefined;
16
+ getBottomCard(): Card | undefined;
17
+ getRandomCard(): Card | undefined;
18
+ private getCards;
19
+ private findCardsOnBoard;
20
+ updateCards(): Card[];
21
+ shuffleDeck(): void;
22
+ addCards(cards: Card[], shouldReplaceExistingCards?: boolean): void;
23
+ removeCards(cards: Card[]): void;
24
+ apply(op: DeckOperation): void;
25
+ updateMbr(): void;
26
+ deserialize(data: SerializedItemData): this;
27
+ render(context: DrawingContext): void;
28
+ }
@@ -0,0 +1,15 @@
1
+ import { BaseOperation } from "Events/EventsOperations";
2
+ export type DeckOperation = AddCards | RemoveCards;
3
+ export interface AddCards extends BaseOperation<{
4
+ cardIds: string[];
5
+ shouldReplaceExistingCards: boolean;
6
+ }> {
7
+ class: "Deck";
8
+ method: "addCards";
9
+ }
10
+ export interface RemoveCards extends BaseOperation<{
11
+ cardIds: string[];
12
+ }> {
13
+ class: "Deck";
14
+ method: "removeCards";
15
+ }
@@ -0,0 +1 @@
1
+ export { Deck } from "./Deck";
@@ -3,7 +3,7 @@ import { BaseItemData } from "Items/BaseItem/BaseItem";
3
3
  type RegisterItemArgs = {
4
4
  item: any;
5
5
  defaultData: BaseItemData;
6
- toolData: {
6
+ toolData?: {
7
7
  name: string;
8
8
  tool: typeof CustomTool;
9
9
  };
@@ -23,5 +23,7 @@ export type { Item, ItemType, ItemData } from "./Item";
23
23
  export { registerItem } from "./RegisterItem";
24
24
  export { Star } from "./Examples/Star";
25
25
  export { Counter } from "./Examples/Counter";
26
+ export { Card } from "./Examples/CardGame/Card";
27
+ export { Deck } from "./Examples/CardGame/Deck";
26
28
  export { Comment } from "./Comment";
27
29
  export type { HorisontalAlignment, VerticalAlignment } from "./Alignment";
@@ -0,0 +1 @@
1
+ export declare function throttle<T extends (...args: any[]) => unknown>(func: T, delay: number): (...args: Parameters<T>) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "microboard-temp",
3
- "version": "0.4.10",
3
+ "version": "0.4.12",
4
4
  "description": "A flexible interactive whiteboard library",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",