-
Notifications
You must be signed in to change notification settings - Fork 2.9k
UFM: JavaScript-like Expressions #19685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
074954d
Adds `markedExtension` extension-type
leekelleher 66e837a
Relocates the Component and Filter extension-type interface code files
leekelleher b91a214
Moves `UfmPlugin` type to its own referencable file
leekelleher f4e5b50
Adds UFM support for JS expressions
leekelleher 72f5cc8
Merge branch 'main' into v16/feature/ufm-js-expressions
leekelleher 31818b5
Modified regex pattern to match nested braces
leekelleher 72a651f
try/catch for invalid JS expressions
leekelleher 7a6ccc3
Merge remote-tracking branch 'origin/main' into v16/feature/ufm-js-ex…
leekelleher 5a71bb5
Merge branch 'main' into v16/feature/ufm-js-expressions
leekelleher 3d13e4f
Capitalizing the JS in `UmbUfmJsMarkedExtensionApi` class name
leekelleher e36f247
Abstracted out `ufmjs()` to its own Marked extension file
leekelleher 1b5de99
Fixed up types in UFM context
leekelleher b6640a5
Adds a generic Least Recently Used (LRU) cache implementation
leekelleher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
1 change: 1 addition & 0 deletions
1
src/Umbraco.Web.UI.Client/src/external/heximal-expressions/index.ts
This file contains hidden or 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 @@ | ||
| export * from '@heximal/expressions'; |
This file contains hidden or 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 @@ | ||
| export * from './lru-cache.js'; |
40 changes: 40 additions & 0 deletions
40
src/Umbraco.Web.UI.Client/src/packages/core/cache/lru-cache.ts
This file contains hidden or 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,40 @@ | ||
| /** | ||
| * @description | ||
| * This class provides a Least Recently Used (LRU) cache implementation. | ||
| * It is designed to store key-value pairs and automatically remove the least recently used items when the cache exceeds a maximum size. | ||
| */ | ||
| export class UmbLruCache<K, V> { | ||
| #cache = new Map<K, V>(); | ||
|
|
||
| #maxSize: number; | ||
|
|
||
| constructor(maxSize: number) { | ||
| this.#maxSize = maxSize; | ||
| } | ||
|
|
||
| get(key: K): V | undefined { | ||
| if (!this.#cache.has(key)) return undefined; | ||
| const value = this.#cache.get(key)!; | ||
| this.#cache.delete(key); | ||
| this.#cache.set(key, value); | ||
| return value; | ||
| } | ||
|
|
||
| set(key: K, value: V): void { | ||
| if (this.#cache.has(key)) { | ||
| this.#cache.delete(key); | ||
| } else if (this.#cache.size >= this.#maxSize) { | ||
| const oldestKey = this.#cache.keys().next().value; | ||
| if (oldestKey) { | ||
| this.#cache.delete(oldestKey); | ||
| } | ||
| } | ||
| this.#cache.set(key, value); | ||
| } | ||
|
|
||
| has(key: K): boolean { | ||
| return this.#cache.has(key); | ||
| } | ||
| } | ||
|
|
||
| export default UmbLruCache; |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from './ufm-render/index.js'; | ||
| export * from './ufm-component-base.js'; | ||
| export * from './ufm-element-base.js'; | ||
| export * from './ufm-js-expression.element.js'; |
67 changes: 67 additions & 0 deletions
67
src/Umbraco.Web.UI.Client/src/packages/ufm/components/ufm-js-expression.element.ts
This file contains hidden or 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,67 @@ | ||
| import { UMB_UFM_CONTEXT } from '../contexts/ufm.context.js'; | ||
| import { UMB_UFM_RENDER_CONTEXT } from './ufm-render/ufm-render.context.js'; | ||
| import { customElement, state } from '@umbraco-cms/backoffice/external/lit'; | ||
| import { EvalAstFactory, Parser } from '@umbraco-cms/backoffice/external/heximal-expressions'; | ||
| import { UmbLruCache } from '@umbraco-cms/backoffice/cache'; | ||
| import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element'; | ||
| import type { Expression, Scope } from '@umbraco-cms/backoffice/external/heximal-expressions'; | ||
|
|
||
| const astFactory = new EvalAstFactory(); | ||
| const expressionCache = new UmbLruCache<string, Expression | undefined>(1000); | ||
|
|
||
| @customElement('umb-ufm-js-expression') | ||
| export class UmbUfmJsExpressionElement extends UmbLitElement { | ||
| #ufmContext?: typeof UMB_UFM_CONTEXT.TYPE; | ||
|
|
||
| @state() | ||
| value?: unknown; | ||
|
|
||
| constructor() { | ||
| super(); | ||
|
|
||
| this.consumeContext(UMB_UFM_CONTEXT, (ufmContext) => { | ||
| this.#ufmContext = ufmContext; | ||
| }); | ||
|
|
||
| this.consumeContext(UMB_UFM_RENDER_CONTEXT, (context) => { | ||
| this.observe( | ||
| context?.value, | ||
| (value) => { | ||
| this.value = this.#labelTemplate(this.textContent ?? '', value); | ||
| }, | ||
| 'observeValue', | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| #labelTemplate(expression: string, model?: any): string { | ||
| const filters = this.#ufmContext?.getFilters() ?? []; | ||
| const functions = Object.fromEntries(filters.map((x) => [x.alias, x.filter])); | ||
| const scope: Scope = { ...model, ...functions }; | ||
|
|
||
| let ast = expressionCache.get(expression); | ||
|
|
||
| if (ast === undefined && !expressionCache.has(expression)) { | ||
| try { | ||
| ast = new Parser(expression, astFactory).parse(); | ||
| } catch { | ||
| console.error(`Error parsing expression: \`${expression}\``); | ||
leekelleher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| expressionCache.set(expression, ast); | ||
| } | ||
|
|
||
| return ast?.evaluate(scope) ?? ''; | ||
| } | ||
|
|
||
| override render() { | ||
| return (Array.isArray(this.value) ? this.value : [this.value]).join(', '); | ||
| } | ||
| } | ||
|
|
||
| export default UmbUfmJsExpressionElement; | ||
|
|
||
| declare global { | ||
| interface HTMLElementTagNameMap { | ||
| 'umb-ufm-js-expression': UmbUfmJsExpressionElement; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
11 changes: 6 additions & 5 deletions
11
src/Umbraco.Web.UI.Client/src/packages/ufm/extensions/types.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| export type * from './marked-extension.extension.js'; | ||
| export type * from './ufm-filter.extension.js'; | ||
| export type * from './ufm-component.extension.js'; | ||
|
|
||
| export type { UmbUfmMarkedExtensionApi } from './ufm-marked-extension.api.js'; | ||
| export type * from './marked-extension.extension.js'; | ||
| export type * from './ufm-filter.extension.js'; | ||
| export type * from './ufm-component.extension.js'; | ||
|
|
||
| export type { UmbUfmMarkedExtensionApi } from './ufm-marked-extension.api.js'; | ||
| export type { UmbUfmJsMarkedExtensionApi } from './ufmjs-marked-extension.api.js'; |
14 changes: 14 additions & 0 deletions
14
src/Umbraco.Web.UI.Client/src/packages/ufm/extensions/ufmjs-marked-extension.api.ts
This file contains hidden or 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,14 @@ | ||
| import { ufmjs } from '../plugins/marked-ufmjs.plugin.js'; | ||
| import type { UmbMarkedExtensionApi } from './marked-extension.extension.js'; | ||
| import type { Marked } from '@umbraco-cms/backoffice/external/marked'; | ||
| import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api'; | ||
|
|
||
| export class UmbUfmJsMarkedExtensionApi implements UmbMarkedExtensionApi { | ||
| constructor(_host: UmbControllerHost, marked: Marked) { | ||
| marked.use(ufmjs()); | ||
| } | ||
|
|
||
| destroy() {} | ||
| } | ||
|
|
||
| export default UmbUfmJsMarkedExtensionApi; |
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export { ufm } from './marked-ufm.plugin.js'; | ||
| export { ufmjs } from './marked-ufmjs.plugin.js'; |
36 changes: 36 additions & 0 deletions
36
src/Umbraco.Web.UI.Client/src/packages/ufm/plugins/marked-ufmjs.plugin.ts
This file contains hidden or 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,36 @@ | ||
| import type { MarkedExtension, Tokens } from '@umbraco-cms/backoffice/external/marked'; | ||
|
|
||
| /** | ||
| * @returns {MarkedExtension} A Marked extension object. | ||
| */ | ||
| export function ufmjs(): MarkedExtension { | ||
| return { | ||
| extensions: [ | ||
| { | ||
| name: 'ufmjs', | ||
| level: 'inline', | ||
| start: (src: string) => src.search(/(?<!\\)\$\{/), | ||
| tokenizer: (src: string) => { | ||
| const pattern = /^\$\{((?:[^{}]|\{[^{}]*\})*)\}/; | ||
| const regex = new RegExp(pattern); | ||
| const match = src.match(regex); | ||
|
|
||
| if (match) { | ||
| const [raw, text] = match; | ||
| return { | ||
| type: 'ufmjs', | ||
| raw: raw, | ||
| tokens: [], | ||
| text: text.trim(), | ||
| }; | ||
| } | ||
|
|
||
| return undefined; | ||
| }, | ||
| renderer: (token: Tokens.Generic) => { | ||
| return `<umb-ufm-js-expression>${token.text}</umb-ufm-js-expression>`; | ||
| }, | ||
| }, | ||
| ], | ||
| }; | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.