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

fix(xml-editor): prettify xml even without root node

- update js dependencies to latest
- add indentWithTab to xml-editor
- tailwindcss 3.1: replace withOpacity with <alpha-value>
parent c9fb987f
Loading
Loading
Loading
Loading
Loading
+43 −10
Original line number Diff line number Diff line
import { basicSetup } from "@codemirror/basic-setup";
import { indentWithTab } from "@codemirror/commands";
import { xml } from "@codemirror/lang-xml";
import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import {
  defaultHighlightStyle,
  syntaxHighlighting,
} from "@codemirror/language";
import { Compartment, EditorState } from "@codemirror/state";
import { keymap } from "@codemirror/view";
import { basicSetup, EditorView } from "codemirror";
import { css, html, LitElement, TemplateResult } from "lit";
import { customElement, queryAssignedNodes, state } from "lit/decorators.js";
import prettifyXML from "xml-formatter";

const language = new Compartment();

@customElement("xml-editor")
export class XMLEditor extends LitElement {
  @queryAssignedNodes("textarea", true)
  @queryAssignedNodes({ slot: "textarea" })
  _textarea!: NodeListOf<HTMLTextAreaElement>;

  @state()
@@ -24,13 +31,39 @@ export class XMLEditor extends LitElement {
      },
    });

    this.editorState = EditorState.create({
      doc: this._textarea[0].value
        ? prettifyXML(this._textarea[0].value, {
    let editorContents = "";
    if (this._textarea[0].value) {
      try {
        editorContents = prettifyXML(this._textarea[0].value, {
          indentation: "  ",
          })
        : "",
      extensions: [basicSetup, xml(), minHeightEditor],
        });
      } catch (e) {
        // xml doesn't have a root node
        editorContents = prettifyXML(
          "<root>" + this._textarea[0].value + "</root>",
          {
            indentation: "  ",
          }
        );
        // remove root, unnecessary lines and indents
        editorContents = editorContents
          .replace(/^<root>/, "")
          .replace(/<\/root>$/, "")
          .replace(/^\s*[\r\n]/gm, "")
          .replace(/[\r\n] {2}/gm, "\r\n")
          .trim();
      }
    }

    this.editorState = EditorState.create({
      doc: editorContents,
      extensions: [
        basicSetup,
        keymap.of([indentWithTab]),
        language.of(xml()),
        minHeightEditor,
        syntaxHighlighting(defaultHighlightStyle),
      ],
    });

    this.editorView = new EditorView({
+2 −2
Original line number Diff line number Diff line
@@ -29,8 +29,8 @@ podcasters around&nbsp;the&nbsp;world!
- 🌱 &nbsp;Free & open-source (AGPL v3 License)
- 🔐 &nbsp;Focused on data sovereignty: your content, audience, and analytics
  belong to you, and&nbsp;you&nbsp;only
- 🪄 &nbsp;Podcasting 2.0 features: GUID, locked, transcripts, funding, chapters,
  location, persons, soundbites, …
- 🪄 &nbsp;Podcasting 2.0 features: GUID, locked, transcripts, funding,
  chapters, location, persons, soundbites, …
- 💬 &nbsp;Built-in social network:
  - 🚀 &nbsp;Castopod is part of the Fediverse, a decentralized social network
  - ❤️ &nbsp;Create posts, share, favourite, and comment on episodes
+2 −2
Original line number Diff line number Diff line
@@ -29,8 +29,8 @@ podcasters around&nbsp;the&nbsp;world!
- 🌱 &nbsp;Free & open-source (AGPL v3 License)
- 🔐 &nbsp;Focused on data sovereignty: your content, audience, and analytics
  belong to you, and&nbsp;you&nbsp;only
- 🪄 &nbsp;Podcasting 2.0 features: GUID, locked, transcripts, funding, chapters,
  location, persons, soundbites, …
- 🪄 &nbsp;Podcasting 2.0 features: GUID, locked, transcripts, funding,
  chapters, location, persons, soundbites, …
- 💬 &nbsp;Built-in social network:
  - 🚀 &nbsp;Castopod is part of the Fediverse, a decentralized social network
  - ❤️ &nbsp;Create posts, share, favourite, and comment on episodes
+2 −2
Original line number Diff line number Diff line
@@ -29,8 +29,8 @@ Podcastenden auf &nbsp;der&nbsp; ganzen Welt verwendet!
- 🌱 Kostenlos & Open-Source (AGPL v3 Lizenz)
- 🔐 &nbsp;Fokussiert auf die Datensouveränität: Ihre Inhalte, Ihr Publikum und
  Analysen gehören zu Ihnen, und&nbsp;nur Ihnen&nbsp;
- 🪄 &nbsp;Podcasting 2.0 features: GUID, locked, transcripts, funding, chapters,
  location, persons, soundbites, …
- 🪄 &nbsp;Podcasting 2.0 features: GUID, locked, transcripts, funding,
  chapters, location, persons, soundbites, …
- 💬 &nbsp;Built-in social network:
  - 🚀 &nbsp;Castopod is part of the Fediverse, a decentralized social network
  - ❤️ &nbsp;Create posts, share, favourite, and comment on episodes
+2 −2
Original line number Diff line number Diff line
@@ -29,8 +29,8 @@ podcasters around&nbsp;the&nbsp;world!
- 🌱 &nbsp;Free & open-source (AGPL v3 License)
- 🔐 &nbsp;Focused on data sovereignty: your content, audience, and analytics
  belong to you, and&nbsp;you&nbsp;only
- 🪄 &nbsp;Podcasting 2.0 features: GUID, locked, transcripts, funding, chapters,
  location, persons, soundbites, …
- 🪄 &nbsp;Podcasting 2.0 features: GUID, locked, transcripts, funding,
  chapters, location, persons, soundbites, …
- 💬 &nbsp;Built-in social network:
  - 🚀 &nbsp;Castopod is part of the Fediverse, a decentralized social network
  - ❤️ &nbsp;Create posts, share, favourite, and comment on episodes
Loading