diff --git a/.github/workflows/quailty.yml b/.github/workflows/quality.yml similarity index 98% rename from .github/workflows/quailty.yml rename to .github/workflows/quality.yml index 0548a5911..0f0e1325f 100644 --- a/.github/workflows/quailty.yml +++ b/.github/workflows/quality.yml @@ -7,7 +7,7 @@ on: branches: [dev] jobs: - quailty: + quality: runs-on: ubuntu-latest strategy: diff --git a/README.md b/README.md index d6b54cc9b..966b57c13 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # [GrapesJS](http://grapesjs.com) -[![Build Status](https://github.com/GrapesJS/grapesjs/actions/workflows/build.yml/badge.svg)](https://github.com/GrapesJS/grapesjs/actions) +[![Build Status](https://github.com/GrapesJS/grapesjs/actions/workflows/quality.yml/badge.svg)](https://github.com/GrapesJS/grapesjs/actions) [![Chat](https://img.shields.io/badge/chat-discord-7289da.svg)](https://discord.gg/QAbgGXq) [![CDNJS](https://img.shields.io/cdnjs/v/grapesjs.svg)](https://cdnjs.com/libraries/grapesjs) [![npm](https://img.shields.io/npm/v/grapesjs.svg)](https://www.npmjs.com/package/grapesjs) diff --git a/docs/api/component.md b/docs/api/component.md index f335ce9b4..e3f7c3da1 100644 --- a/docs/api/component.md +++ b/docs/api/component.md @@ -187,6 +187,24 @@ console.log(allImages[0]) // prints the first found component Returns **[Array][5]\** +## findFirstType + +Find the first inner component by component type. +If no component is found, it returns `undefined`. + +### Parameters + +* `type` **[String][1]** Component type + +### Examples + +```javascript +const image = component.findFirstType('image'); +if (image) console.log(image) +``` + +Returns **Component** Found component, otherwise `undefined` + ## closest Find the closest parent component by query string. diff --git a/src/dom_components/model/Component.ts b/src/dom_components/model/Component.ts index a75e7df00..d450d34c9 100644 --- a/src/dom_components/model/Component.ts +++ b/src/dom_components/model/Component.ts @@ -507,6 +507,21 @@ export default class Component extends StyleableModel { return result; } + /** + * Find the first inner component by component type. + * If no component is found, it returns `undefined`. + * @param {String} type Component type + * @returns {Component|undefined} + * @example + * const image = component.findFirstType('image'); + * if (image) { + * console.log(image); + * } + */ + findFirstType(type: string): Component | undefined { + return this.findType(type).at(0); + } + /** * Find the closest parent component by query string. * **ATTENTION**: this method works only with already rendered component diff --git a/test/specs/dom_components/model/Component.ts b/test/specs/dom_components/model/Component.ts index f55c8c2d9..ad18fb641 100644 --- a/test/specs/dom_components/model/Component.ts +++ b/test/specs/dom_components/model/Component.ts @@ -273,6 +273,32 @@ describe('Component', () => { expect(result.class).toEqual(undefined); }); + test('findFirstType returns first component of specified type', () => { + const image1 = new ComponentImage({}, compOpts); + const text = new ComponentText({}, compOpts); + const image2 = new ComponentImage({}, compOpts); + + obj.append([image1, text, image2]); + + const result = obj.findFirstType('image'); + expect(result).toBe(image1); + expect(result instanceof ComponentImage).toBe(true); + }); + + test('findFirstType returns undefined for non-existent type', () => { + const text = new ComponentText({}, compOpts); + + obj.append(text); + + const result = obj.findFirstType('image'); + expect(result).toBeUndefined(); + }); + + test('findFirstType returns undefined for empty component', () => { + const result = obj.findFirstType('div'); + expect(result).toBeUndefined(); + }); + test('setAttributes', () => { obj.setAttributes({ id: 'test',