From cfe7b6275b0c0e9184665d4d28d467b071e2d705 Mon Sep 17 00:00:00 2001 From: Zachary Zhang Date: Thu, 13 Jun 2024 12:40:54 -0400 Subject: fix bug with auto resize --- src/client/views/nodes/DiagramBox.scss | 50 ++++++----- src/client/views/nodes/DiagramBox.tsx | 154 +++++++++++++++++++++++++++++++-- 2 files changed, 176 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/client/views/nodes/DiagramBox.scss b/src/client/views/nodes/DiagramBox.scss index 1b6efe01d..5b12c6f89 100644 --- a/src/client/views/nodes/DiagramBox.scss +++ b/src/client/views/nodes/DiagramBox.scss @@ -1,4 +1,5 @@ .DiagramBox { + overflow:hidden; width: 100%; height: 100%; display: flex; @@ -27,6 +28,7 @@ } .DiagramBox-wrapper { + overflow:hidden; width: 100%; height: 100%; display: flex; @@ -34,6 +36,7 @@ align-items: center; justify-content: center; .content { + overflow: hidden; display: flex; justify-content: center; align-items: center; @@ -59,6 +62,9 @@ } } .search-bar { + overflow:hidden; + position:absolute; + top:0; .backButton{ text-align: center; padding:0; @@ -85,34 +91,36 @@ border-radius: 10px; } } + .loading-circle { + position: absolute; + display:flex; + align-items: center; + justify-content: center; + width: 50px; + height: 50px; + border-radius: 50%; + border: 3px solid #ccc; + border-top-color: #333; + animation: spin 1s infinite linear; + } + @keyframes spin { + 0% { + transform: rotate(0deg); + } + 100% { + transform: rotate(360deg); + } + } .diagramBox{ flex: 1; display: flex; justify-content: center; align-items: center; svg{ + position: relative; + top:25; max-width: none !important; - } - .loading-circle { - position: absolute; - display:flex; - align-items: center; - justify-content: center; - width: 50px; - height: 50px; - border-radius: 50%; - border: 3px solid #ccc; - border-top-color: #333; - animation: spin 1s infinite linear; - } - - @keyframes spin { - 0% { - transform: rotate(0deg); - } - 100% { - transform: rotate(360deg); - } + height: calc(100% - 50px); } } } diff --git a/src/client/views/nodes/DiagramBox.tsx b/src/client/views/nodes/DiagramBox.tsx index 220c43ead..08c889158 100644 --- a/src/client/views/nodes/DiagramBox.tsx +++ b/src/client/views/nodes/DiagramBox.tsx @@ -39,9 +39,11 @@ export class DiagramBox extends ViewBoxAnnotatableComponent() { @observable menuState = menuState.justCreated; @observable renderDiv: React.ReactNode; @observable inputValue = ''; + @observable createInputValue = ''; @observable loading = false; @observable errorMessage = ''; @observable mermaidCode = ''; + @observable isExampleMenuOpen = false; @action handleInputChange = (e: React.ChangeEvent) => { this.inputValue = e.target.value; @@ -115,7 +117,7 @@ export class DiagramBox extends ViewBoxAnnotatableComponent() { const { svg, bindFunctions } = await this.mermaidDiagram(str); return { svg, bindFunctions }; } catch (error) { - console.error('Error rendering mermaid diagram:', error); + // console.error('Error rendering mermaid diagram:', error); return { svg: '', bindFunctions: undefined }; } }; @@ -127,6 +129,7 @@ export class DiagramBox extends ViewBoxAnnotatableComponent() { const dashDiv = document.getElementById('dashDiv' + this.Document.title); if (dashDiv) { dashDiv.innerHTML = svg; + // this.changeHeightWidth(svg); if (bindFunctions) { bindFunctions(dashDiv); } @@ -135,6 +138,20 @@ export class DiagramBox extends ViewBoxAnnotatableComponent() { console.error('Error rendering Mermaid:', error); } } + changeHeightWidth(svgString: string) { + const pattern = /width="([\d.]+)"\s*height="([\d.]+)"/; + + const match = svgString.match(pattern); + + if (match) { + const width = parseFloat(match[1]); + const height = parseFloat(match[2]); + console.log(width); + console.log(height); + this.Document.width = width; + this.Document.height = height; + } + } @action handleRenderClick = () => { this.mermaidCode = ''; if (this.inputValue) { @@ -143,13 +160,17 @@ export class DiagramBox extends ViewBoxAnnotatableComponent() { }; @action async generateMermaidCode() { console.log('Generating Mermaid Code'); + const dashDiv = document.getElementById('dashDiv' + this.Document.title); + if (dashDiv) { + dashDiv.innerHTML = ''; + } this.loading = true; let prompt = ''; prompt = 'Write this in mermaid code and only give me the mermaid code: ' + this.inputValue; console.log('there is no text save'); // } const res = await gptAPICall(prompt, GPTCallType.MERMAID); - this.loading = false; + this.loading = true; if (res === 'Error connecting with API.') { // If GPT call failed console.error('GPT call failed'); @@ -167,7 +188,6 @@ export class DiagramBox extends ViewBoxAnnotatableComponent() { } this.renderMermaidAsync.call(this, this.removeWords(this.mermaidCode)); this.Document.gptMermaidCode = this.removeWords(this.mermaidCode); - this.loading = false; } isValidCode = (html: string) => true; removeWords(inputStrIn: string) { @@ -225,9 +245,9 @@ export class DiagramBox extends ViewBoxAnnotatableComponent() { console.log(linkedDocs.length); if (linkedDocs.length !== 0) { const linkedText = (linkedDocs[0].text as RichTextField).Text; - mermaidCode += Math.abs(rectangle.x) + this.getTextInBox(rectangle, textArray) + '-->|' + linkedText + '|' + Math.abs(rectangle2.x) + this.getTextInBox(rectangle2, textArray) + ';'; + mermaidCode += Math.abs(rectangle.x) + this.getTextInBox(rectangle, textArray) + '---|' + linkedText + '|' + Math.abs(rectangle2.x) + this.getTextInBox(rectangle2, textArray) + ';'; } else { - mermaidCode += Math.abs(rectangle.x) + this.getTextInBox(rectangle, textArray) + '-->' + Math.abs(rectangle2.x) + this.getTextInBox(rectangle2, textArray) + ';'; + mermaidCode += Math.abs(rectangle.x) + this.getTextInBox(rectangle, textArray) + '---' + Math.abs(rectangle2.x) + this.getTextInBox(rectangle2, textArray) + ';'; } } } @@ -336,13 +356,21 @@ export class DiagramBox extends ViewBoxAnnotatableComponent() { Generate -
+ {this.mermaidCode ? ( +
+ ) : ( +
{this.loading ?
:
{this.errorMessage ? this.errorMessage : 'Insert prompt to generate diagram'}
}
+ )}
); } renderMermaidCode(): React.ReactNode { + let title = 'asd'; + if (typeof this.Document.title === 'string') { + title = this.removeWhitespace(this.Document.title); + } return (
@@ -350,18 +378,130 @@ export class DiagramBox extends ViewBoxAnnotatableComponent() { -