microboard-temp 0.4.9 → 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/node.js CHANGED
@@ -39709,12 +39709,19 @@ class Shape extends BaseItem {
39709
39709
  this.shapeType = data.shapeType;
39710
39710
  this.initPath();
39711
39711
  }
39712
+ if (data.linkTo) {
39713
+ this.linkTo.deserialize(data.linkTo);
39714
+ }
39712
39715
  this.backgroundColor = data.backgroundColor ?? this.backgroundColor;
39713
39716
  this.backgroundOpacity = data.backgroundOpacity ?? this.backgroundOpacity;
39714
39717
  this.borderColor = data.borderColor ?? this.borderColor;
39715
39718
  this.borderOpacity = data.borderOpacity ?? this.borderOpacity;
39716
39719
  this.borderStyle = data.borderStyle ?? this.borderStyle;
39717
39720
  this.borderWidth = data.borderWidth ?? this.borderWidth;
39721
+ if (data.transformation) {
39722
+ this.transformation.deserialize(data.transformation);
39723
+ this.transformPath();
39724
+ }
39718
39725
  if (data.text) {
39719
39726
  this.text.deserialize(data.text);
39720
39727
  }
@@ -47959,7 +47966,9 @@ function registerItem({
47959
47966
  const { itemType } = defaultData2;
47960
47967
  itemFactories[itemType] = createItemFactory(item, defaultData2);
47961
47968
  itemValidators[itemType] = createItemValidator(defaultData2);
47962
- registeredTools[toolData.name] = toolData.tool;
47969
+ if (toolData) {
47970
+ registeredTools[toolData.name] = toolData.tool;
47971
+ }
47963
47972
  itemCommandFactories[itemType] = createItemCommandFactory(itemType);
47964
47973
  }
47965
47974
  function createItemFactory(item, defaultData2) {
@@ -48273,6 +48282,318 @@ registerItem({
48273
48282
  defaultData: defaultCounterData,
48274
48283
  toolData: { name: "AddCounter", tool: AddCounter }
48275
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
+ name: "",
48313
+ isInDeck: false
48314
+ };
48315
+ var CARD_DIMENSIONS = { width: 250, height: 400 };
48316
+
48317
+ class Card extends BaseItem {
48318
+ subject = new Subject;
48319
+ name = "";
48320
+ isOpen = false;
48321
+ isInDeck = false;
48322
+ throttledBringToFront;
48323
+ image = null;
48324
+ backside = null;
48325
+ imageToRender = null;
48326
+ shouldUseCustomRender = false;
48327
+ constructor(board, id = "", defaultData2, name) {
48328
+ super(board, id, defaultCardData);
48329
+ this.name = name;
48330
+ this.createImages();
48331
+ this.throttledBringToFront = throttle(() => {
48332
+ this.board.bringToFront(this);
48333
+ }, 1000);
48334
+ this.transformation.subject.subscribe(() => {
48335
+ this.throttledBringToFront();
48336
+ this.updateMbr();
48337
+ this.updateImageToRender();
48338
+ this.subject.publish(this);
48339
+ });
48340
+ this.updateMbr();
48341
+ }
48342
+ createImages() {
48343
+ this.image = conf.documentFactory.createElement("img");
48344
+ this.backside = conf.documentFactory.createElement("img");
48345
+ this.image.src = `/Assets/Cards/${this.name}.png`;
48346
+ this.backside.src = `/Assets/Cards/backside.png`;
48347
+ this.image.onload = () => {
48348
+ this.subject.publish(this);
48349
+ };
48350
+ this.backside.onload = () => {
48351
+ this.subject.publish(this);
48352
+ };
48353
+ }
48354
+ setIsInDeck(isInDeck) {
48355
+ this.emit({
48356
+ class: "Card",
48357
+ method: "setIsInDeck",
48358
+ item: [this.getId()],
48359
+ newData: { isInDeck },
48360
+ prevData: { isInDeck: this.isInDeck }
48361
+ });
48362
+ }
48363
+ updateImageToRender() {
48364
+ this.imageToRender = this.backside;
48365
+ if (this.isOpen) {
48366
+ this.imageToRender = this.image;
48367
+ }
48368
+ }
48369
+ render(context, deckRenderData) {
48370
+ if (this.transformationRenderBlock || this.isInDeck && !deckRenderData) {
48371
+ return;
48372
+ }
48373
+ const ctx = context.ctx;
48374
+ if (this.imageToRender && this.imageToRender.complete) {
48375
+ ctx.save();
48376
+ let left = this.left;
48377
+ let top = this.top;
48378
+ if (deckRenderData) {
48379
+ left = deckRenderData.left + 2 * deckRenderData.cardPosition;
48380
+ top = deckRenderData.top;
48381
+ }
48382
+ ctx.drawImage(this.imageToRender, left, top, CARD_DIMENSIONS.width, CARD_DIMENSIONS.height);
48383
+ ctx.restore();
48384
+ }
48385
+ }
48386
+ updateMbr() {
48387
+ const { translateX, translateY, scaleX, scaleY } = this.transformation.matrix;
48388
+ this.left = translateX;
48389
+ this.top = translateY;
48390
+ this.right = this.left + CARD_DIMENSIONS.width * scaleX;
48391
+ this.bottom = this.top + CARD_DIMENSIONS.height * scaleY;
48392
+ }
48393
+ getMbr() {
48394
+ if (this.isInDeck) {
48395
+ return new Mbr(1e4, 1e4, 1e4, 1e4);
48396
+ }
48397
+ return super.getMbr();
48398
+ }
48399
+ getPath() {
48400
+ return new Path(this.getMbr().getLines());
48401
+ }
48402
+ renderHTML(documentFactory) {
48403
+ const div = documentFactory.createElement("card-item");
48404
+ return div;
48405
+ }
48406
+ deserialize(data) {
48407
+ super.deserialize(data);
48408
+ this.updateMbr();
48409
+ this.createImages();
48410
+ this.subject.publish(this);
48411
+ return this;
48412
+ }
48413
+ toggleIsOpen() {
48414
+ this.emit({
48415
+ class: "Card",
48416
+ method: "setIsOpen",
48417
+ item: [this.getId()],
48418
+ newData: { isOpen: !this.isOpen },
48419
+ prevData: { isOpen: this.isOpen }
48420
+ });
48421
+ }
48422
+ apply(op) {
48423
+ super.apply(op);
48424
+ switch (op.class) {
48425
+ case "Card":
48426
+ switch (op.method) {
48427
+ case "setIsOpen":
48428
+ this.isOpen = op.newData.isOpen;
48429
+ this.updateImageToRender();
48430
+ break;
48431
+ case "setIsInDeck":
48432
+ this.isInDeck = op.newData.isInDeck;
48433
+ break;
48434
+ }
48435
+ break;
48436
+ }
48437
+ this.subject.publish(this);
48438
+ }
48439
+ }
48440
+ registerItem({
48441
+ item: Card,
48442
+ defaultData: defaultCardData
48443
+ });
48444
+ // src/Items/Examples/CardGame/Deck/Deck.ts
48445
+ var defaultDeckData = {
48446
+ itemType: "Deck",
48447
+ cardIds: []
48448
+ };
48449
+
48450
+ class Deck extends BaseItem {
48451
+ subject = new Subject;
48452
+ shouldUseCustomRender = false;
48453
+ cardIds = [];
48454
+ cards = [];
48455
+ constructor(board, id = "", defaultData2, cards) {
48456
+ super(board, id, defaultDeckData);
48457
+ if (cards) {
48458
+ this.cards = cards;
48459
+ cards.forEach((card) => card.setIsInDeck(true));
48460
+ this.transformation.matrix = cards[cards.length - 1].transformation.matrix;
48461
+ this.cardIds = cards.map((card) => card.getId());
48462
+ }
48463
+ this.transformation.subject.subscribe(() => {
48464
+ this.updateMbr();
48465
+ this.subject.publish(this);
48466
+ });
48467
+ this.updateMbr();
48468
+ }
48469
+ getDeck() {
48470
+ return this.cards;
48471
+ }
48472
+ getTopCard() {
48473
+ const cardId = this.cardIds[this.cardIds.length - 1];
48474
+ return this.getCards([cardId])[0];
48475
+ }
48476
+ getBottomCard() {
48477
+ const cardId = this.cardIds[0];
48478
+ return this.getCards([cardId])[0];
48479
+ }
48480
+ getRandomCard() {
48481
+ const cardId = this.cardIds[Math.ceil(Math.random() * this.cardIds.length) - 1];
48482
+ return this.getCards([cardId])[0];
48483
+ }
48484
+ getCards(cardIds) {
48485
+ const cards = this.findCardsOnBoard(cardIds);
48486
+ this.removeCards(cards);
48487
+ return cards;
48488
+ }
48489
+ findCardsOnBoard(cardIds) {
48490
+ return cardIds.map((cardId) => {
48491
+ return this.board.items.getById(cardId);
48492
+ }).filter((card) => !!card);
48493
+ }
48494
+ updateCards() {
48495
+ if (this.cardIds.length === this.cards.length) {
48496
+ return this.cards;
48497
+ }
48498
+ this.cards = this.findCardsOnBoard(this.cardIds);
48499
+ return this.cards;
48500
+ }
48501
+ shuffleDeck() {
48502
+ const shuffled = [...this.cardIds];
48503
+ for (let i = shuffled.length - 1;i > 0; i--) {
48504
+ const j = Math.floor(Math.random() * (i + 1));
48505
+ [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
48506
+ }
48507
+ const cards = this.findCardsOnBoard(shuffled);
48508
+ this.addCards(cards, true);
48509
+ }
48510
+ addCards(cards, shouldReplaceExistingCards = false) {
48511
+ cards.forEach((card) => {
48512
+ card.setIsInDeck(true);
48513
+ });
48514
+ this.board.bringToFront(cards);
48515
+ this.emit({
48516
+ class: "Deck",
48517
+ method: "addCards",
48518
+ item: [this.getId()],
48519
+ newData: {
48520
+ cardIds: cards.map((card) => card.getId()),
48521
+ shouldReplaceExistingCards
48522
+ },
48523
+ prevData: { cardIds: this.cardIds, shouldReplaceExistingCards }
48524
+ });
48525
+ }
48526
+ removeCards(cards) {
48527
+ cards.forEach((card) => {
48528
+ card.setIsInDeck(false);
48529
+ });
48530
+ this.emit({
48531
+ class: "Deck",
48532
+ method: "removeCards",
48533
+ item: [this.getId()],
48534
+ newData: { cardIds: cards.map((card) => card.getId()) },
48535
+ prevData: { cardIds: this.cardIds }
48536
+ });
48537
+ }
48538
+ apply(op) {
48539
+ super.apply(op);
48540
+ switch (op.class) {
48541
+ case "Deck":
48542
+ switch (op.method) {
48543
+ case "addCards":
48544
+ if (op.newData.shouldReplaceExistingCards) {
48545
+ this.cardIds = op.newData.cardIds;
48546
+ this.cards = this.findCardsOnBoard(this.cardIds);
48547
+ } else {
48548
+ this.cardIds = [
48549
+ ...op.newData.cardIds,
48550
+ ...this.cardIds
48551
+ ];
48552
+ this.updateCards();
48553
+ this.updateMbr();
48554
+ }
48555
+ break;
48556
+ case "removeCards":
48557
+ this.cardIds = this.cardIds.filter((card) => {
48558
+ return !op.newData.cardIds.includes(card);
48559
+ });
48560
+ this.updateCards();
48561
+ this.updateMbr();
48562
+ break;
48563
+ }
48564
+ break;
48565
+ }
48566
+ this.subject.publish(this);
48567
+ }
48568
+ updateMbr() {
48569
+ const { translateX, translateY, scaleX, scaleY } = this.transformation.matrix;
48570
+ this.left = translateX;
48571
+ this.top = translateY;
48572
+ this.right = this.left + CARD_DIMENSIONS.width * scaleX + 2 * this.cardIds.length;
48573
+ this.bottom = this.top + CARD_DIMENSIONS.height * scaleY;
48574
+ }
48575
+ deserialize(data) {
48576
+ super.deserialize(data);
48577
+ this.updateCards();
48578
+ return this;
48579
+ }
48580
+ render(context) {
48581
+ if (this.transformationRenderBlock) {
48582
+ return;
48583
+ }
48584
+ this.cards.forEach((card, index2) => {
48585
+ card.render(context, {
48586
+ top: this.top,
48587
+ left: this.left,
48588
+ cardPosition: index2 + 1
48589
+ });
48590
+ });
48591
+ }
48592
+ }
48593
+ registerItem({
48594
+ item: Deck,
48595
+ defaultData: defaultDeckData
48596
+ });
48276
48597
  // src/Pointer/Cursor.ts
48277
48598
  var defaultCursors = [
48278
48599
  "default",
@@ -48785,27 +49106,6 @@ class Camera {
48785
49106
  requestAnimationFrame(animate);
48786
49107
  }
48787
49108
  }
48788
- function throttle(func, delay) {
48789
- let lastCall = 0;
48790
- let timeoutId = null;
48791
- return function(...args) {
48792
- const now = Date.now();
48793
- if (lastCall + delay <= now) {
48794
- lastCall = now;
48795
- func(...args);
48796
- if (timeoutId) {
48797
- clearTimeout(timeoutId);
48798
- timeoutId = null;
48799
- }
48800
- } else if (!timeoutId) {
48801
- timeoutId = setTimeout(() => {
48802
- lastCall = Date.now();
48803
- timeoutId = null;
48804
- func(...args);
48805
- }, delay - (now - lastCall));
48806
- }
48807
- };
48808
- }
48809
49109
  // src/hotkeys.json
48810
49110
  var hotkeys_default = {
48811
49111
  select: {
@@ -58410,6 +58710,7 @@ export {
58410
58710
  Drawing,
58411
58711
  DefaultTransformationData,
58412
58712
  DefaultShapeData,
58713
+ Deck,
58413
58714
  DEFAULT_SHAPE,
58414
58715
  CubicBezier,
58415
58716
  Counter,
@@ -58417,6 +58718,7 @@ export {
58417
58718
  Connector2 as Connector,
58418
58719
  ConnectionLineWidths,
58419
58720
  Comment,
58721
+ Card,
58420
58722
  Camera,
58421
58723
  CURSORS_IDLE_CLEANUP_DELAY,
58422
58724
  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,42 @@
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 name;
23
+ private isOpen;
24
+ private isInDeck;
25
+ private throttledBringToFront;
26
+ image: HTMLImageElement | null;
27
+ backside: HTMLImageElement | null;
28
+ private imageToRender;
29
+ shouldUseCustomRender: boolean;
30
+ constructor(board: Board, id: string | undefined, defaultData: BaseItemData | undefined, name: string);
31
+ createImages(): void;
32
+ setIsInDeck(isInDeck: boolean): void;
33
+ updateImageToRender(): void;
34
+ render(context: DrawingContext, deckRenderData?: DeckRenderData): void;
35
+ updateMbr(): void;
36
+ getMbr(): Mbr;
37
+ getPath(): Path | Paths;
38
+ renderHTML(documentFactory: DocumentFactory): HTMLElement;
39
+ deserialize(data: SerializedItemData): this;
40
+ toggleIsOpen(): void;
41
+ apply(op: CardOperation): void;
42
+ }
@@ -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.9",
3
+ "version": "0.4.11",
4
4
  "description": "A flexible interactive whiteboard library",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",