|
| 1 | +import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.10/firebase-app.js"; |
| 2 | +import { getDatabase, ref, set, get } from "https://www.gstatic.com/firebasejs/9.6.10/firebase-database.js"; |
| 3 | + |
| 4 | +const firebaseConfig = { |
| 5 | + apiKey: "YOUR-API-KEY", |
| 6 | + authDomain: "YOUR-DOMAIN.firebaseapp.com", |
| 7 | + databaseURL: "https://YOUR-DOMAIN-rtdb.firebaseio.com", |
| 8 | + projectId: "YOUR-PROJECT-ID", |
| 9 | + storageBucket: "YOUR-BUCKET.appspot.com", |
| 10 | + messagingSenderId: "YOUR-SENDER-ID", |
| 11 | + appId: "YOUR-APP-ID" |
| 12 | +}; |
| 13 | + |
| 14 | +// Initialize Firebase |
| 15 | +const app = initializeApp(firebaseConfig); |
| 16 | +const db = getDatabase( app ); |
| 17 | + |
| 18 | +async function getTemplatesFromDB( userId ) { |
| 19 | + const data = await get( ref( db, "templates/" + userId ) ); |
| 20 | + if( data.exists() ) { |
| 21 | + return data.val(); |
| 22 | + } |
| 23 | + return []; |
| 24 | +} |
| 25 | + |
| 26 | +function saveTemplatesToDB( userId, templates ) { |
| 27 | + set( ref( db, "templates/" + userId ), templates ); |
| 28 | +} |
| 29 | + |
| 30 | +let templates = []; |
| 31 | +let filter = ""; |
| 32 | + |
| 33 | +window.saveTemplate = ( userId, name, items ) => { |
| 34 | + // Calculate center |
| 35 | + let totalX = 0, totalY = 0; |
| 36 | + items.forEach( item => { |
| 37 | + totalX += item.x; |
| 38 | + totalY += item.y; |
| 39 | + }); |
| 40 | + const center = { x: totalX / items.length, y: totalY / items.length }; |
| 41 | + |
| 42 | + // Save to the cloud |
| 43 | + templates.push( { name, items, center }); |
| 44 | + saveTemplatesToDB( userId, templates ); |
| 45 | + displayTemplates( userId, templates ); |
| 46 | +} |
| 47 | + |
| 48 | +window.deleteTemplate = ( userId, index ) => { |
| 49 | + templates.splice( index, 1 ); |
| 50 | + saveTemplatesToDB( userId, templates ); |
| 51 | + displayTemplates( userId, templates ); |
| 52 | +} |
| 53 | + |
| 54 | +function displayTemplates( userId, templates ) { |
| 55 | + const templateList = document.getElementById( "templates" ); |
| 56 | + templateList.innerHTML = ""; |
| 57 | + templates.forEach( ( t, i ) => { |
| 58 | + if( !t.name.toLowerCase().includes( filter.toLowerCase() ) ) { return; } |
| 59 | + templateList.innerHTML += |
| 60 | + `<div class="cs1 ce12 miro-draggable" data-index="${i}" style="padding: 5px; margin: 5px; border: 5px solid black;"> |
| 61 | + <h2>${t.name} - <span onclick="deleteTemplate('${userId}', ${i})">x</span></h2> |
| 62 | + </div>`; |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +async function createWidgetFromJson( item, x, y ) { |
| 67 | + const clone = Object.assign( {}, item, { x: item.x + x, y: item.y + y } ); |
| 68 | + switch( clone.type ) { |
| 69 | + case "card": |
| 70 | + await miro.board.createCard( clone ); |
| 71 | + break; |
| 72 | + case "frame": |
| 73 | + await miro.board.createFrame( clone ); |
| 74 | + break; |
| 75 | + case "shape": |
| 76 | + await miro.board.createShape( clone ); |
| 77 | + break; |
| 78 | + case "sticky_note": |
| 79 | + await miro.board.createStickyNote( clone ); |
| 80 | + break; |
| 81 | + case "text": |
| 82 | + await miro.board.createText( clone ); |
| 83 | + break; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +async function init() { |
| 88 | + // Get the user |
| 89 | + const user = await miro.board.getUserInfo(); |
| 90 | + |
| 91 | + // Load templates from the cloud |
| 92 | + templates = await getTemplatesFromDB( user.id ); |
| 93 | + displayTemplates( user.id, templates ); |
| 94 | + |
| 95 | + // Save button handler |
| 96 | + document.getElementById( "save-btn" ).onclick = async () => { |
| 97 | + const name = prompt( "Please enter a name for the template" ); |
| 98 | + // Save items |
| 99 | + const items = await miro.board.getSelection(); |
| 100 | + // Remove unnecessary properties |
| 101 | + items.forEach( item => { |
| 102 | + delete item.id; |
| 103 | + delete item.parentId; |
| 104 | + delete item.height; // Note: only save the width and not the height |
| 105 | + delete item.createdAt; |
| 106 | + delete item.createdBy; |
| 107 | + delete item.modifiedAt; |
| 108 | + delete item.modifiedBy; |
| 109 | + }); |
| 110 | + saveTemplate( user.id, name, items ); |
| 111 | + }; |
| 112 | + |
| 113 | + // Search handler |
| 114 | + document.getElementById( "search-bar" ).addEventListener( "input", (e) => { |
| 115 | + filter = e.target.value; |
| 116 | + displayTemplates( user.id, templates ); |
| 117 | + }); |
| 118 | + |
| 119 | + // Drag-n-drop handler |
| 120 | + miro.board.ui.on( "drop", async ({ x, y, target }) => { |
| 121 | + const index = target.getAttribute( "data-index" ); |
| 122 | + const template = templates[ index ]; |
| 123 | + template.items.forEach( item => { |
| 124 | + createWidgetFromJson( item, x - template.center.x, y - template.center.y ); |
| 125 | + }); |
| 126 | + }); |
| 127 | +} |
| 128 | + |
| 129 | +init(); |
0 commit comments