From 917bb233738afad252f3fa54bae8c3ab5b55179f Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Sun, 12 Jan 2020 15:20:09 +0100 Subject: [PATCH] Update getName function --- src/dom_components/model/Component.js | 25 +++++++++++++++++-------- src/utils/mixins.js | 2 +- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/dom_components/model/Component.js b/src/dom_components/model/Component.js index 0c7eab4f8..eef7d2abb 100644 --- a/src/dom_components/model/Component.js +++ b/src/dom_components/model/Component.js @@ -11,7 +11,7 @@ import { result, keys } from 'underscore'; -import { shallowDiff } from 'utils/mixins'; +import { shallowDiff, capitalize } from 'utils/mixins'; import Styleable from 'domain_abstract/model/Styleable'; import Backbone from 'backbone'; import Components from './Components'; @@ -867,16 +867,25 @@ const Component = Backbone.Model.extend(Styleable).extend( getName() { const { em } = this; const { type, tagName } = this.attributes; - const customName = this.get('custom-name'); const cName = this.get('name'); - let tag = tagName; - tag = tag == 'div' ? 'box' : tag; - let name = type || tag; - name = name.charAt(0).toUpperCase() + name.slice(1); + const isDiv = tagName == 'div'; + const tag = isDiv ? 'box' : tagName; + const defName = type || tag; + const nameTag = !type && tagName && !isDiv && tagName; const i18nPfx = 'domComponents.names.'; - const i18nStr = + const i18nName = cName && em && em.t(`${i18nPfx}${cName}`); + const i18nNameTag = nameTag && em && em.t(`${i18nPfx}${nameTag}`); + const i18nDefName = em && (em.t(`${i18nPfx}${type}`) || em.t(`${i18nPfx}${tagName}`)); - return customName || i18nStr || cName || name; + return ( + this.get('custom-name') || // Used in Layers (when the user changes the name) + i18nName || + cName || // Component name (check if there is a i18n string for it) + i18nNameTag || + capitalize(nameTag) || // Try name by tag if there is no valid type + i18nDefName || + capitalize(defName) // Use the default name + ); }, /** diff --git a/src/utils/mixins.js b/src/utils/mixins.js index 0b516ac74..fe7fec1af 100644 --- a/src/utils/mixins.js +++ b/src/utils/mixins.js @@ -208,7 +208,7 @@ const getKeyCode = ev => ev.which || ev.keyCode; const getKeyChar = ev => String.fromCharCode(getKeyCode(ev)); const isEscKey = ev => getKeyCode(ev) === 27; -const capitalize = str => str.charAt(0).toUpperCase() + str.substring(1); +const capitalize = str => str && str.charAt(0).toUpperCase() + str.substring(1); const isComponent = obj => obj && obj.toHTML; const isRule = obj => obj && obj.toCSS;