From e3b6f6475d954c794c94f10af74671c3830ecfb3 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 8 May 2024 08:51:09 +0200 Subject: [PATCH] enh(useElementSize): track the size of an element Signed-off-by: Max --- .../composables/useElementSize.cy.ts | 58 +++++++++++++++++++ .../NcRichText/NcReferenceWidget.vue | 21 +------ src/composables/index.js | 1 + src/composables/useElementSize/index.ts | 42 ++++++++++++++ 4 files changed, 104 insertions(+), 18 deletions(-) create mode 100644 cypress/component/composables/useElementSize.cy.ts create mode 100644 src/composables/useElementSize/index.ts diff --git a/cypress/component/composables/useElementSize.cy.ts b/cypress/component/composables/useElementSize.cy.ts new file mode 100644 index 0000000000..ee0e8db2e0 --- /dev/null +++ b/cypress/component/composables/useElementSize.cy.ts @@ -0,0 +1,58 @@ +/** + * SPDX-FileCopyrightText: 2024 Max + * SPDX-License-Identifier: @license AGPL-3.0-or-later + * + * This is a cypress test because useElementSize needs a proper browser. + * The size of elements cannot be measured in environments like jsdom + * as they only stub geometry related functions such as resizeObserver. + */ + + +import { computed, isRef, ref } from 'vue' +import { useElementSize } from '../../../src/composables/useElementSize/index.ts' + +const StyleableComponent = { + props: { style: Object }, + setup() { + const widgetRoot = ref() + const { width, height } = useElementSize(widgetRoot) + return { widgetRoot, width, height } + }, + template: '
', +} + +describe('useElementSize', () => { + it('returns refs', () => { + const { width, height } = useElementSize() + expect(isRef(width)).to.be.true + expect(isRef(height)).to.be.true + }) + + it('defaults to 0', () => { + const { width, height } = useElementSize() + expect(width.value).to.be.equal(0) + expect(height.value).to.be.equal(0) + }) + + it('measures the size', () => { + const style = { width: '100px', height: '200px' } + cy.mount(StyleableComponent, { propsData: { style } }) + .its('component').as('component') + cy.get('@component').its('width').should('equal', 100) + cy.get('@component').its('height').should('equal', 200) + }) + + it('updates the size', () => { + const style = ref({ width: '123px', height: '200px' }) + cy.mount(StyleableComponent, { propsData: { style } }) + .its('component').as('component') + cy.get('@component').its('width').should('equal', 123) + cy.get('@component').its('height').should('equal', 200) + cy.then(() => style.value.width = '246px') + cy.then(() => style.value.height = '400px') + cy.get('@component').its('width').should('equal', 246) + cy.get('@component').its('height').should('equal', 400) + }) + +}) + diff --git a/src/components/NcRichText/NcReferenceWidget.vue b/src/components/NcRichText/NcReferenceWidget.vue index b0550c9044..e13cf3a84e 100644 --- a/src/components/NcRichText/NcReferenceWidget.vue +++ b/src/components/NcRichText/NcReferenceWidget.vue @@ -29,13 +29,14 @@