Skip to content

nss_libs - NssSimpleTextEditor ​

A section-based rich text editor with headline/text types, alignment options, and validation. NUI

Getting Started ​

NssSimpleTextEditor provides a lightweight editor where users can create multiple text sections, each with a type (headline or text) and alignment (left, center, right). Sections can be added, removed, and reordered via toolbar buttons.

js
import {NssSimpleTextEditor} from "nui://nss_libs/ui/NssSimpleTextEditor/NssSimpleTextEditor.js";

const editor = new NssSimpleTextEditor(document.getElementById('editor-container'));
editor.setPlaceholderText('Enter your text here...').start();

Constructor ​

new NssSimpleTextEditor(target_el) ​

Creates a new text editor instance bound to a container element.

Parameters ​

  • target_el : HTMLElement — The parent element the editor will be rendered into.

Example ​

js
const editor = new NssSimpleTextEditor(document.getElementById('editor-wrapper'));

Methods ​

start() ​

Initializes the editor DOM, creates a default headline section and a text section, and renders into the target element.

Syntax ​

js
editor.start()

Parameters ​

None.

Return Value ​

Returns undefined.

Example ​

js
const editor = new NssSimpleTextEditor(container_el);
editor.setPlaceholderText('Write something...').start();

setPlaceholderText(text) ​

Sets the placeholder text shown in empty sections.

Syntax ​

js
editor.setPlaceholderText(text)

Parameters ​

  • text : string — The placeholder text.

Return Value ​

Returns NssSimpleTextEditor — the instance for chaining.

Example ​

js
editor.setPlaceholderText('Enter your message...');

getPlaceholderText() ​

Returns the current placeholder text.

Syntax ​

js
editor.getPlaceholderText()

Parameters ​

None.

Return Value ​

Returns string — the placeholder text.

Example ​

js
const placeholder = editor.getPlaceholderText();

addSection(parent_section) ​

Adds a new text section to the editor.

Syntax ​

js
editor.addSection(parent_section)

Parameters ​

  • parent_section : NssSimpleTextEditorSection (Optional, default: undefined) — If provided, the new section is inserted after this section. Otherwise appended at the end.

Return Value ​

Returns undefined.

Example ​

js
// Add a section at the end
editor.addSection();

removeSection(section) ​

Removes a section from the editor and destroys its DOM.

Syntax ​

js
editor.removeSection(section)

Parameters ​

  • section : NssSimpleTextEditorSection — The section instance to remove.

Return Value ​

Returns undefined.

Example ​

js
// Sections handle their own removal via toolbar buttons internally

getSectionsComputed() ​

Returns the content of all sections as a plain-object array, ready for serialization.

Syntax ​

js
editor.getSectionsComputed()

Parameters ​

None.

Return Value ​

Returns Array<Object> — an array of objects with the shape:

js
{
    type: 'headline' | 'text',
    text: 'The section content',
    align: 'left' | 'center' | 'right'
}

Example ​

js
const sections = editor.getSectionsComputed();
// [
//   { type: 'headline', text: 'My Title', align: 'center' },
//   { type: 'text', text: 'Some content here.', align: 'left' }
// ]
nss_client.post('save', {sections: sections});

isErrorHandlingSuccessful() ​

Validates all sections. Sections with empty text are marked with an error state (shake animation + error CSS class).

Syntax ​

js
editor.isErrorHandlingSuccessful()

Parameters ​

None.

Return Value ​

Returns boolean — true if all sections are valid, false if any section has empty text.

Example ​

js
function save() {
    if (!editor.isErrorHandlingSuccessful()) {
        return; // validation failed — error states are shown automatically
    }

    const sections = editor.getSectionsComputed();
    nss_client.post('save_content', {sections: sections});
}

Events / Callbacks ​

NssSimpleTextEditor does not expose external callbacks. Interaction is handled internally:

  • Tab key on the last section automatically creates a new section.
  • Toolbar buttons per section: toggle type (headline ↔ text), toggle alignment (left → center → right), remove section, add section after.
  • Remove button is disabled when only one section remains.

CSS ​

Classes ​

ClassDescription
.nss-simple-text-editor__sectionA single editable section container.
.nss-simple-text-editor__section--headlineSection in headline mode (larger font).
.nss-simple-text-editor__section--textSection in text mode (normal font).
.nss-simple-text-editor__section--align-leftLeft-aligned text.
.nss-simple-text-editor__section--align-centerCenter-aligned text.
.nss-simple-text-editor__section--align-rightRight-aligned text.
.nss-simple-text-editor__section--errorError state with shake animation.
.nss-simple-text-editor__section-optionsFloating toolbar (visible on hover).
.nss-simple-text-editor__section-footerFloating footer with add-section button.
.nss-simple-text-editor__section-textThe contenteditable text area.
.nss-simple-text-editor__btnGeneric toolbar button.
.nss-simple-text-editor__section-text-btnSwitch-to-text-type button.
.nss-simple-text-editor__section-headline-btnSwitch-to-headline-type button.
.nss-simple-text-editor__section-remove-btn--disabledDisabled state for the remove button.

Custom Properties ​

PropertyScopeDefaultDescription
--translateX__section-options0Horizontal offset of the options toolbar.
--translateY__section-options-75%Vertical offset of the options toolbar.
--translateX__section-footer-50%Horizontal offset of the footer.
--translateY__section-footer50%Vertical offset of the footer.

Animations ​

KeyframeDescription
nss-simple-text-editor-anim__shakeHorizontal shake animation for error state (0.82s).

Dependencies ​

  • NssUiComponent — base class (which initializes NssResponsive).

Things to Know ​

  • Sections use contenteditable divs, not <textarea> or <input> elements.
  • The placeholder text is implemented manually — it clears on focus and restores on blur if the section is empty.
  • getSectionsComputed() returns plain objects (not class instances), suitable for direct serialization via JSON.stringify or sending via NssClient.post().
  • Tab on the last section creates a new section — this only works on the last section in the list.
  • The active type/alignment button in the toolbar is visually disabled (dimmed) to indicate the current state.

NIGHTSHIFT STUDIO Documentation