add snippet utils

This commit is contained in:
Julia Gru
2023-04-21 10:31:43 +02:00
parent 83001fbcc1
commit d1a865a44d
2 changed files with 68 additions and 21 deletions

View File

@@ -1,8 +1,9 @@
import { UUITextStyles } from '@umbraco-ui/uui-css';
import { PropertyValueMap, css, html } from 'lit';
import { css, html } from 'lit';
import { customElement, state } from 'lit/decorators.js';
import { UmbModalBaseElement } from '@umbraco-cms/internal/modal';
import { UUIComboboxElement, UUIInputElement } from '@umbraco-ui/uui';
import { getUmbracoFieldSnippet } from '../utils';
import { UmbModalBaseElement } from '@umbraco-cms/internal/modal';
@customElement('umb-insert-value-sidebar')
export default class UmbInsertValueSidebarElement extends UmbModalBaseElement<object, string> {
@@ -60,7 +61,7 @@ export default class UmbInsertValueSidebarElement extends UmbModalBaseElement<ob
output = '';
protected willUpdate(): void {
this.output = this.generateOutputSample();
this.output = this.field ? getUmbracoFieldSnippet(this.field, this.defaultValue, this.recursive) : '';
}
#setField(event: Event) {
@@ -73,25 +74,7 @@ export default class UmbInsertValueSidebarElement extends UmbModalBaseElement<ob
this.defaultValue = target.value === '' ? null : (target.value as string);
}
generateOutputSample() {
let fallback = null;
if (this.recursive !== false && this.defaultValue !== null) {
fallback = 'Fallback.To(Fallback.Ancestors, Fallback.DefaultValue)';
} else if (this.recursive !== false) {
fallback = 'Fallback.ToAncestors';
} else if (this.defaultValue !== null) {
fallback = 'Fallback.ToDefaultValue';
}
const field = `${this.field !== null ? `@Model.Value("${this.field}"` : ''}, ${
fallback !== null ? `fallback: ${fallback}` : ''
}, ${this.defaultValue !== null ? `defaultValue: new HtmlString("${this.defaultValue}")` : ''}${
this.field ? ')' : ''
}`;
return field;
}
render() {
return html`

View File

@@ -3,3 +3,67 @@ export const urlFriendlyPathFromServerFilePath = (path: string) => encodeURIComp
// TODO: we can try and make pretty urls if we want to
export const serverFilePathFromUrlFriendlyPath = (unique: string) => decodeURIComponent(unique.replace('-', '.'));
//Below are a copy of
export const getInsertDictionarySnippet = (nodeName: string) => {
return `@Umbraco.GetDictionaryValue("${nodeName}")`;
}
export const getInsertPartialSnippet = (nodeName: string, parentId = '') => {
let partialViewName = nodeName.replace(".cshtml", "");
if(parentId) {
partialViewName = parentId + "/" + partialViewName;
}
return `@await Html.PartialAsync("${partialViewName}")`
}
export const getQuerySnippet = (queryExpression: string) => {
let code = "\n@{\n" + "\tvar selection = " + queryExpression + ";\n}\n";
code += "<ul>\n" +
"\t@foreach (var item in selection)\n" +
"\t{\n" +
"\t\t<li>\n" +
"\t\t\t<a href=\"@item.Url()\">@item.Name()</a>\n" +
"\t\t</li>\n" +
"\t}\n" +
"</ul>\n\n";
return code;
}
export const getRenderBodySnippet = () => "@RenderBody()";
export const getRenderSectionSnippet = (sectionName: string, isMandatory: boolean) => `@RenderSection("${sectionName}", ${isMandatory})`;
export const getAddSectionSnippet = (sectionName: string) => `@section ${sectionName}
{
}`
export const getUmbracoFieldSnippet = (field: string, defaultValue: string | null = null, recursive = false) => {
let fallback = null;
if (recursive !== false && defaultValue !== null) {
fallback = 'Fallback.To(Fallback.Ancestors, Fallback.DefaultValue)';
} else if (recursive !== false) {
fallback = 'Fallback.ToAncestors';
} else if (defaultValue !== null) {
fallback = 'Fallback.ToDefaultValue';
}
const value = `${field !== null ? `@Model.Value("${field}"` : ''}, ${
fallback !== null ? `fallback: ${fallback}` : ''
}, ${defaultValue !== null ? `defaultValue: new HtmlString("${defaultValue}")` : ''}${
field ? ')' : ''
}`;
return value;
}