From e859bc454890d623b4690f4f1fd8ffe82a2bb2ce Mon Sep 17 00:00:00 2001 From: bat Date: Tue, 9 May 2023 06:44:23 +0000 Subject: [PATCH] remove add file button, using menu items for adding files --- file-group.js | 24 +++++++++--------------- file-view.js | 36 ++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/file-group.js b/file-group.js index 8f67bf2..b139a67 100644 --- a/file-group.js +++ b/file-group.js @@ -1,14 +1,11 @@ export class FileGroup extends HTMLElement { - textEn = { - addFile: 'Add File', - } + textEn = {} - textEs = { - addFile: 'AƱadir archivo', - } + textEs = {} constructor() { super() + this.fileCount = {value: 0} this.language = navigator.language this.attachShadow({mode: 'open'}) this.headerEl = document.createElement('div') @@ -20,15 +17,6 @@ export class FileGroup extends HTMLElement { const bGroup = document.createElement( 'm-forms-button-group' ) - bGroup.addPrimary(this.text.addFile, () => { - this.addFile() - const btn = bGroup.primary - if (btn.scrollIntoViewIfNeeded) { - btn.scrollIntoViewIfNeeded() - } else { - btn.scrollIntoView() - } - }) this.shadowRoot.appendChild(bGroup) this.contentEl.addEventListener( 'click-add-above', @@ -36,10 +24,12 @@ export class FileGroup extends HTMLElement { const el = document.createElement( 'm-editor-file-view' ) + el.fileCount = this.fileCount el.codeMirror = this.codeMirror e.target.insertAdjacentElement( 'beforebegin', el ) + this.fileCount.value += 1 }, ) this.contentEl.addEventListener( @@ -48,10 +38,12 @@ export class FileGroup extends HTMLElement { const el = document.createElement( 'm-editor-file-view' ) + el.fileCount = this.fileCount el.codeMirror = this.codeMirror e.target.insertAdjacentElement( 'afterend', el ) + this.fileCount.value += 1 }, ) } @@ -83,6 +75,7 @@ export class FileGroup extends HTMLElement { addFile({name, data, collapsed} = {}) { const el = document.createElement('m-editor-file-view') + el.fileCount = this.fileCount el.codeMirror = this.codeMirror if (name !== undefined) { el.name = name @@ -94,6 +87,7 @@ export class FileGroup extends HTMLElement { el.collapsed = collapsed } this.contentEl.appendChild(el) + this.fileCount.value += 1 return el } diff --git a/file-view.js b/file-view.js index 63b4569..a2f9dc4 100644 --- a/file-view.js +++ b/file-view.js @@ -57,25 +57,12 @@ export class FileView extends HTMLElement { this.menuBtn = document.createElement('button') this.menuBtn.innerHTML = this.icons.menu this.menuBtn.addEventListener('click', () => { - this.menu.open(this.menuBtn) + this.openMenu() }) this.headerEl.appendChild(this.menuBtn) this.menu = document.createElement( 'm-menu-dropdown' ) - this.menu.add(this.text.addAbove, () => { - this.dispatchEvent(new CustomEvent( - 'click-add-above', {bubbles: true} - )) - }) - this.menu.add(this.text.addBelow, () => { - this.dispatchEvent(new CustomEvent( - 'click-add-below', {bubbles: true} - )) - }) - this.menu.add(this.text.delete, () => { - this.remove() - }) this.shadowRoot.appendChild(this.menu) } @@ -127,6 +114,27 @@ export class FileView extends HTMLElement { this.shadowRoot.appendChild(style) } + openMenu() { + this.menu.clear() + this.menu.add(this.text.addAbove, () => { + this.dispatchEvent(new CustomEvent( + 'click-add-above', {bubbles: true} + )) + }) + this.menu.add(this.text.addBelow, () => { + this.dispatchEvent(new CustomEvent( + 'click-add-below', {bubbles: true} + )) + }) + if (this.fileCount.value > 1) { + this.menu.add(this.text.delete, () => { + this.remove() + this.fileCount.value -= 1 + }) + } + this.menu.open(this.menuBtn) + } + set codeMirror(value) { this._codeMirror = value const tagName = (