A collection of helper functions and utility methods related to the rich text editor

Properties

#decoder: HTMLTextAreaElement = ...

A singleton text area used for HTML decoding.

_PARAGRAPH_ELEMENTS: Set<string> = ...

A list of elements that are retained when truncating HTML.

Methods

  • Create a Rich Text Editor. The current implementation uses TinyMCE

    Parameters

    • options: {
          engine: string;
      } = {}

      Configuration options provided to the Editor init

      • engine: string

        Which rich text editor engine to use, "tinymce" or "prosemirror". TinyMCE is deprecated and will be removed in a later version.

    • content: string = ""

      Initial HTML or text content to populate the editor with

    Returns Promise<any>

    The editor instance.

  • Safely decode an HTML string, removing invalid tags and converting entities back to unicode characters.

    Parameters

    • html: string

      The original encoded HTML string

    Returns string

    The decoded unicode string

  • Enrich HTML content by replacing or augmenting components of it

    Parameters

    • content: string

      The original HTML content (as a string)

    • Optional options: EnrichmentOptions = {}

      Additional options which configure how HTML is enriched

    Returns Promise<string>

    The enriched HTML content

  • Preview an HTML fragment by constructing a substring of a given length from its inner text.

    Parameters

    • content: string

      The raw HTML to preview

    • length: number = 250

      The desired length

    Returns string

    The previewed HTML

  • Sanitises an HTML fragment and removes any non-paragraph-style text.

    Parameters

    • html: HTMLElement

      The root HTML element.

    Returns HTMLElement

  • Truncate a fragment of text to a maximum number of characters.

    Parameters

    • text: string

      The original text fragment that should be truncated to a maximum length

    • Optional options: {
          maxLength: number;
          splitWords: boolean;
          suffix: string;
      } = {}

      Options which affect the behavior of text truncation

      • maxLength: number

        The maximum allowed length of the truncated string.

      • splitWords: boolean

        Whether to truncate by splitting on white space (if true) or breaking words.

      • suffix: string

        A suffix string to append to denote that the text was truncated.

    Returns string

    The truncated text string

  • Helper method to create an anchor element.

    Parameters

    • Optional options: Partial<any> = {}

      Options to configure the anchor's construction.

    Returns HTMLAnchorElement

  • Activate interaction listeners for the interior content of the editor frame.

    Returns void

  • Extract JSON data from a drag/drop event.

    Parameters

    • event: DragEvent

      The drag event which contains JSON data.

    Returns any

    The extracted JSON data. The object will be empty if the DragEvent did not contain JSON-parseable data.

  • Given a Drop event, returns a Content link if possible such as @Actor[ABC123], else null

    Parameters

    • eventData: any

      The parsed object of data provided by the transfer event

    • Optional options: {
          relativeTo: ClientDocument;
          label: string;
      } = {}

      Additional options to configure link creation.

      • relativeTo: ClientDocument

        A document to generate the link relative to.

      • label: string

        A custom label to use instead of the document's name.

    Returns Promise<string>

  • Internal

    Upload an image to a document's asset path.

    Parameters

    • uuid: string

      The document's UUID.

    • file: File

      The image file to upload.

    Returns Promise<string>

    The path to the uploaded image.

  • Protected

    Create a TinyMCE editor instance.

    Parameters

    • Optional options: any = {}

      Configuration options passed to the editor.

    • Optional content: string = ""

      Initial HTML or text content to populate the editor with.

    Returns Promise<Editor>

    The TinyMCE editor instance.

  • Protected

    Scan for compendium UUIDs and retrieve Documents in batches so that they are in cache when enrichment proceeds.

    Parameters

    • text: Text[]

      The text nodes to scan.

    Returns Promise<void>

  • Protected

    Convert text of the form @UUID[uuid]{name} to anchor elements.

    Parameters

    • text: Text[]

      The existing text content

    • Optional options: EnrichmentOptions = {}

      Options provided to customize text enrichment

    Returns Promise<boolean>

    Whether any content links were replaced and the text nodes need to be updated.

  • Protected

    Handle embedding Document content with @Embed[uuid]{label} text.

    Parameters

    • text: Text[]

      The existing text content.

    • Optional options: EnrichmentOptions = {}

      Options provided to customize text enrichment.

    Returns Promise<boolean>

    Whether any embeds were replaced and the text nodes need to be updated.

  • Protected

    Convert URLs into anchor elements.

    Parameters

    • text: Text[]

      The existing text content

    • Optional options: EnrichmentOptions = {}

      Options provided to customize text enrichment

    Returns Promise<boolean>

    Whether any hyperlinks were replaced and the text nodes need to be updated

  • Protected

    Convert text of the form [[roll]] to anchor elements.

    Parameters

    • rollData: any

      The data object providing context for inline rolls.

    • text: Text[]

      The existing text content.

    Returns Promise<boolean>

    Whether any inline rolls were replaced and the text nodes need to be updated.

  • Protected

    Match any custom registered regex patterns and apply their replacements.

    Parameters

    Returns Promise<boolean>

    Whether any replacements were made, requiring the text nodes to be updated.

  • Protected

    Create a dynamic document link from a regular expression match

    Parameters

    • match: RegExpMatchArray

      The regular expression match

    • Optional options: {
          relativeTo: Document;
      } = {}

      Additional options to configure enrichment behaviour

      • relativeTo: Document

        A document to resolve relative UUIDs against.

    Returns Promise<HTMLAnchorElement>

    An HTML element for the document link.

  • Protected

    Embed content from another Document.

    Parameters

    • match: RegExpMatchArray

      The regular expression match.

    • Optional options: EnrichmentOptions = {}

      Options provided to customize text enrichment.

    Returns Promise<HTMLElement>

    A representation of the Document as HTML content, or null if the Document could not be embedded.

  • Protected

    Parse the embed configuration to be passed to ClientDocument#toEmbed. The return value will be an object of any key=value pairs included with the configuration, as well as a separate values property that contains all the options supplied that were not in key=value format. If a uuid key is supplied it is used as the Document's UUID, otherwise the first supplied UUID is used.

    Parameters

    • raw: string

      The raw matched config string.

    • Optional options: any = {}

      Options forwarded to parseUuid.

    Returns Record<string, string | number | boolean>

    Example: Example configurations.

    TextEditor._parseEmbedConfig('uuid=Actor.xyz caption="Example Caption" cite=false');
    // Returns: { uuid: "Actor.xyz", caption: "Example Caption", cite: false, values: [] }

    TextEditor._parseEmbedConfig('Actor.xyz caption="Example Caption" inline');
    // Returns: { uuid: "Actor.xyz", caption: "Example Caption", values: ["inline"] }
  • Protected

    Replace an inline roll formula with a rollable <a> element or an eagerly evaluated roll result

    Parameters

    • match: RegExpMatchArray

      The regular expression match array

    • rollData: any

      Provided roll data for use in roll evaluation

    Returns Promise<HTMLAnchorElement>

    The replaced match. Returns null if the contained command is not a valid roll expression.

  • Protected

    Handle actions in embedded content.

    Parameters

    • event: PointerEvent

      The originating event.

    Returns Promise<void>

  • Private

    Recursively identify the text nodes within a parent HTML node for potential content replacement.

    Parameters

    • parent: HTMLElement

      The parent HTML Element

    Returns Text[]

    An array of contained Text nodes

  • Private

    Facilitate the replacement of text node content using a matching regex rule and a provided replacement function.

    Parameters

    • text: Text[]

      The text nodes to match and replace.

    • rgx: RegExp

      The provided regular expression for matching and replacement

    • func: TextContentReplacer

      The replacement function

    • Optional options: TextReplacementOptions = {}

      Options to configure text replacement behavior.

    Returns boolean

    Whether a replacement was made.

  • Private

    Replace a matched portion of a Text node with a replacement Node

    Parameters

    • text: Text

      The Text node containing the match.

    • match: RegExpMatchArray

      The regular expression match.

    • replacement: Node

      The replacement Node.

    • Optional options: TextReplacementOptions = {}

      Options to configure text replacement behavior.

    Returns void

  • Private

    Create a dynamic document link from an old-form document link expression.

    Parameters

    • type: string

      The matched document type, or "Compendium".

    • target: string

      The requested match target (_id or name).

    • name: string

      A customized or overridden display name for the link.

    • data: any

      Data containing the properties of the resulting link element.

    Returns boolean

    Whether the resulting link is broken or not.

  • Private

    Replace a hyperlink-like string with an actual HTML <a> tag

    Parameters

    • match: RegExpMatchArray

      The regular expression match

    Returns Promise<HTMLAnchorElement>

    An HTML element for the document link

  • Private

    Handle click events on Document Links

    Parameters

    • event: Event

    Returns Promise<any>

  • Private

    Handle left-mouse clicks on an inline roll, dispatching the formula or displaying the tooltip

    Parameters

    • event: MouseEvent

      The initiating click event

    Returns Promise<any>

  • Private

    Begin a Drag+Drop workflow for a dynamic content link

    Parameters

    • event: Event

      The originating drag event

    Returns boolean

  • Private

    Handle dropping of transferred data onto the active rich text editor

    Parameters

    • event: DragEvent

      The originating drop event which triggered the data transfer

    • editor: TinyMCE

      The TinyMCE editor instance being dropped on

    Returns Promise<void>