-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve messaging in the page editor (#3403)
- Loading branch information
Showing
4 changed files
with
53 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/app-page-builder/src/editor/contexts/EditorPageElementsProvider/getElementTitle.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { PbEditorPageElementPlugin } from "~/types"; | ||
import { plugins } from "@webiny/plugins"; | ||
|
||
const titlesCache: Record<string, string> = {}; | ||
|
||
/** | ||
* Returns element title from element's plugin. If plugin is not found, it will | ||
* return the element type. A simple cache was added to avoid unnecessary lookups. | ||
*/ | ||
export const getElementTitle = (elementType: string): string => { | ||
if (elementType in titlesCache) { | ||
return titlesCache[elementType]; | ||
} | ||
|
||
titlesCache[elementType] = elementType; | ||
|
||
const elementEditorPlugin = plugins | ||
.byType<PbEditorPageElementPlugin>("pb-editor-page-element") | ||
.find(item => item.elementType === elementType); | ||
|
||
if (!elementEditorPlugin) { | ||
return titlesCache[elementType]; | ||
} | ||
|
||
const toolbarTitle = elementEditorPlugin?.toolbar?.title; | ||
if (typeof toolbarTitle === "string") { | ||
titlesCache[elementType] = toolbarTitle; | ||
} else { | ||
// Upper-case first the type. | ||
titlesCache[elementType] = elementType.charAt(0).toUpperCase() + elementType.slice(1); | ||
} | ||
|
||
return titlesCache[elementType]; | ||
}; |