Commit f15f2624 authored by Yassine Doghri's avatar Yassine Doghri
Browse files

feat: add codemirror to display xml editor for custom rss field

- replace hardcoded label values for play_episode_button component
parent 578022b8
......@@ -585,7 +585,8 @@ class Episode extends Entity
*/
public function setCustomRssString(?string $customRssString = null): static
{
if ($customRssString === null) {
if ($customRssString === '') {
$this->attributes['custom_rss'] = null;
return $this;
}
......
......@@ -487,6 +487,7 @@ class Podcast extends Entity
public function setCustomRssString(string $customRssString): static
{
if ($customRssString === '') {
$this->attributes['custom_rss'] = null;
return $this;
}
......
......@@ -467,8 +467,8 @@ if (! function_exists('play_episode_button')) {
podcast="${podcastTitle}"
src="${source}"
mediaType="${mediaType}"
playLabel="Play"
playingLabel="Playing"
playLabel="${playLabel}"
playingLabel="${playingLabel}"
></play-episode-button>
CODE_SAMPLE;
}
......@@ -535,3 +535,28 @@ if (! function_exists('relative_time')) {
}
// ------------------------------------------------------------------------
if (! function_exists('xml_editor')) {
/**
* XML Editor field
*
* @param array<string, mixed> $customData
* @param array<string, mixed> $extra
*/
function xml_editor(array $customData = [], string $value = '', array $extra = []): string
{
$defaultData = [
'slot' => 'textarea',
'rows' => 5,
];
$data = array_merge($defaultData, $customData);
$textarea = form_textarea($data, $value, $extra);
return <<<CODE_SAMPLE
<xml-editor>${textarea}</time-ago>
CODE_SAMPLE;
}
}
// ------------------------------------------------------------------------
......@@ -16,6 +16,7 @@ import Soundbites from "./modules/Soundbites";
import ThemePicker from "./modules/ThemePicker";
import Time from "./modules/Time";
import Tooltip from "./modules/Tooltip";
import "./modules/xml-editor";
Dropdown();
Tooltip();
......
<svg width="410" height="404" viewBox="0 0 410 404" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M399.641 59.5246L215.643 388.545C211.844 395.338 202.084 395.378 198.228 388.618L10.5817 59.5563C6.38087 52.1896 12.6802 43.2665 21.0281 44.7586L205.223 77.6824C206.398 77.8924 207.601 77.8904 208.776 77.6763L389.119 44.8058C397.439 43.2894 403.768 52.1434 399.641 59.5246Z" fill="url(#paint0_linear)"/>
<path d="M292.965 1.5744L156.801 28.2552C154.563 28.6937 152.906 30.5903 152.771 32.8664L144.395 174.33C144.198 177.662 147.258 180.248 150.51 179.498L188.42 170.749C191.967 169.931 195.172 173.055 194.443 176.622L183.18 231.775C182.422 235.487 185.907 238.661 189.532 237.56L212.947 230.446C216.577 229.344 220.065 232.527 219.297 236.242L201.398 322.875C200.278 328.294 207.486 331.249 210.492 326.603L212.5 323.5L323.454 102.072C325.312 98.3645 322.108 94.137 318.036 94.9228L279.014 102.454C275.347 103.161 272.227 99.746 273.262 96.1583L298.731 7.86689C299.767 4.27314 296.636 0.855181 292.965 1.5744Z" fill="url(#paint1_linear)"/>
<defs>
<linearGradient id="paint0_linear" x1="6.00017" y1="32.9999" x2="235" y2="344" gradientUnits="userSpaceOnUse">
<stop stop-color="#41D1FF"/>
<stop offset="1" stop-color="#BD34FE"/>
</linearGradient>
<linearGradient id="paint1_linear" x1="194.651" y1="8.81818" x2="236.076" y2="292.989" gradientUnits="userSpaceOnUse">
<stop stop-color="#FFEA83"/>
<stop offset="0.0833333" stop-color="#FFDD35"/>
<stop offset="1" stop-color="#FFA800"/>
</linearGradient>
</defs>
</svg>
import { basicSetup } from "@codemirror/basic-setup";
import { xml } from "@codemirror/lang-xml";
import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { css, html, LitElement, TemplateResult } from "lit";
import { customElement, queryAssignedNodes, state } from "lit/decorators.js";
import prettifyXML from "xml-formatter";
@customElement("xml-editor")
export class XMLEditor extends LitElement {
@queryAssignedNodes("textarea", true)
_textarea!: NodeListOf<HTMLTextAreaElement>;
@state()
editorState!: EditorState;
@state()
editorView!: EditorView;
firstUpdated(): void {
const minHeightEditor = EditorView.theme({
".cm-content, .cm-gutter": {
minHeight: this._textarea[0].clientHeight + "px",
},
});
this.editorState = EditorState.create({
doc: this._textarea[0].value
? prettifyXML(this._textarea[0].value, {
indentation: " ",
})
: "",
extensions: [basicSetup, xml(), minHeightEditor],
});
this.editorView = new EditorView({
state: this.editorState,
root: this.shadowRoot as ShadowRoot,
parent: this.shadowRoot as ShadowRoot,
});
this._textarea[0].hidden = true;
if (this._textarea[0].form) {
this._textarea[0].form.addEventListener("submit", () => {
this._textarea[0].value = this.editorView.state.doc.toString();
});
}
}
disconnectedCallback(): void {
if (this._textarea[0].form) {
this._textarea[0].form.removeEventListener("submit", () => {
this._textarea[0].value = this.editorView.state.doc.toString();
});
}
}
static styles = css`
.cm-wrap {
border: 1px solid #6b7280;
background-color: #ffffff;
}
`;
render(): TemplateResult<1> {
return html`<slot name="textarea"></slot>`;
}
}
......@@ -226,6 +226,7 @@
[
'id' => 'description_footer',
'name' => 'description_footer',
'rows' => 6
],
old(
'description_footer',
......@@ -401,7 +402,7 @@
lang('Episode.form.custom_rss_hint'),
true,
) ?>
<?= form_textarea([
<?= xml_editor([
'id' => 'custom_rss',
'name' => 'custom_rss',
'class' => 'form-textarea',
......
......@@ -236,6 +236,7 @@
[
'id' => 'description_footer',
'name' => 'description_footer',
'rows' => 6
],
old(
'description_footer',
......@@ -474,7 +475,7 @@
lang('Episode.form.custom_rss_hint'),
true,
) ?>
<?= form_textarea([
<?= xml_editor([
'id' => 'custom_rss',
'name' => 'custom_rss',
'class' => 'form-textarea',
......
......@@ -340,7 +340,7 @@
lang('Podcast.form.custom_rss_hint'),
true,
) ?>
<?= form_textarea([
<?= xml_editor([
'id' => 'custom_rss',
'name' => 'custom_rss',
'class' => 'form-textarea',
......
......@@ -335,7 +335,7 @@
lang('Podcast.form.custom_rss_hint'),
true,
) ?>
<?= form_textarea([
<?= xml_editor([
'id' => 'custom_rss',
'name' => 'custom_rss',
'class' => 'form-textarea',
......
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment