Created
October 2, 2023 11:23
-
-
Save bjoerntx/946f88cf4a9913eef28367b6bd34b3bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ContainerManager { | |
constructor() { | |
this.currentContainer = null; | |
this.storedDocuments = {}; | |
this.attachEventListeners(); | |
} | |
async enableContainer(container, show) { | |
if (this.currentContainer !== null) { | |
await this.saveContainer(this.currentContainer); | |
this.currentContainer.classList.add("inactive"); | |
} | |
container.classList.remove("inactive"); | |
container.innerHTML = ""; | |
container.appendChild(document.getElementById("mainContainer")); | |
await this.replaceContainer(this.currentContainer); | |
await this.loadContainer(container); | |
this.currentContainer = container; | |
if (show === true) { | |
document.getElementById("mainContainer").style.display = "inline-block"; | |
document.getElementById("mainContainer").style.height = "100%"; | |
window.dispatchEvent(new Event('resize')); | |
} | |
} | |
async replaceContainer(container) { | |
return new Promise(resolve => { | |
if (container === null) { | |
resolve(false); | |
} else { | |
TXTextControl.pages.elementAt(0, page => { | |
page.getImage(TXTextControl.ImageFormat.Png, 100, 7, function (image) { | |
const img = document.createElement("img"); | |
img.src = "data:image/png;base64," + image; | |
img.className = "img-thumbnail"; | |
container.innerHTML = ""; | |
container.appendChild(img); | |
resolve(true); | |
}); | |
}); | |
} | |
}); | |
} | |
async saveContainer(container) { | |
return new Promise(resolve => { | |
if (container === null) { | |
resolve(false); | |
} else { | |
var docs = this.storedDocuments; | |
TXTextControl.saveDocument(TXTextControl.StreamType.InternalUnicodeFormat, function (data) { | |
docs[container.id] = data; | |
resolve(true); | |
}); | |
} | |
}); | |
} | |
async loadContainer(container) { | |
return new Promise(resolve => { | |
if (container === null) { | |
resolve(false); | |
} else { | |
console.log(this.storedDocuments[container.id]); | |
if (this.storedDocuments[container.id] !== undefined) { | |
TXTextControl.loadDocument(TXTextControl.StreamType.InternalUnicodeFormat, this.storedDocuments[container.id].data, function () { | |
resolve(true); | |
}); | |
} else { | |
TXTextControl.resetContents(); | |
resolve(false); | |
} | |
} | |
}); | |
} | |
attachEventListeners() { | |
const elements = document.getElementsByClassName("editorContainer"); | |
for (let i = 0; i < elements.length; i++) { | |
elements[i].addEventListener("click", (elem) => { | |
if (elem.target !== this.currentContainer && elem.target.className.includes("editorContainer")) { | |
this.enableContainer(elem.target, true); | |
} | |
}); | |
} | |
} | |
} | |
const containerManager = new ContainerManager(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment