Module

code-block/utils

@ckeditor/ckeditor5-code-block/src/utils

module

Filtering

Functions

  • getIndentOutdentPositions( model ) → Array.<Position>

    static

    Returns an array of all model positions within the selection that represent code block lines.

    If the selection is collapsed, it returns the exact selection anchor position:

    <codeBlock>[]foo</codeBlock>        ->     <codeBlock>^foo</codeBlock>
    <codeBlock>foo[]bar</codeBlock>     ->     <codeBlock>foo^bar</codeBlock>

    Otherwise, it returns positions before each text node belonging to all code blocks contained by the selection:

    <codeBlock>                                <codeBlock>
        foo[bar                                   ^foobar
        <softBreak></softBreak>         ->        <softBreak></softBreak>
        baz]qux                                   ^bazqux
    </codeBlock>                               </codeBlock>

    It also works across other non–code blocks:

    <codeBlock>                                <codeBlock>
        foo[bar                                   ^foobar
    </codeBlock>                               </codeBlock>
    <paragraph>text</paragraph>         ->     <paragraph>text</paragraph>
    <codeBlock>                                <codeBlock>
        baz]qux                                   ^bazqux
    </codeBlock>                               </codeBlock>

    Note: The positions are in reverse order so they do not get outdated when iterating over them and the writer inserts or removes things at the same time.

    Note: The position is situated after the leading white spaces in the text node.

    Parameters

    model : Model

    Returns

    Array.<Position>
  • getLeadingWhiteSpaces( codeLineNodes ) → String

    static

    For a given model text node, it returns white spaces that precede other characters in that node. This corresponds to the indentation part of the code block line.

    Parameters

    codeLineNodes : Text

    Returns

    String
  • getNormalizedAndLocalizedLanguageDefinitions( editor ) → Array.<CodeBlockLanguageDefinition>

    static

    Returns code block languages as defined in config.codeBlock.languages but processed:

    • To consider the editor localization, i.e. to display CodeBlockLanguageDefinition in the correct language. There is no way to use t when the user configuration is defined because the editor does not exist yet.
    • To make sure each definition has a CSS class associated with it even if not specified in the original configuration.

    Parameters

    editor : Editor

    Returns

    Array.<CodeBlockLanguageDefinition>

    .

  • getPropertyAssociation( languageDefs, key, value, )

    static

    Returns an object associating certain language definition properties with others. For instance:

    For:

    const definitions = {
        { language: 'php', class: 'language-php', label: 'PHP' },
        { language: 'javascript', class: 'js', label: 'JavaScript' },
    };
    
    getPropertyAssociation( definitions, 'class', 'language' );

    returns:

    {
        'language-php': 'php'
        'js': 'javascript'
    }

    and

    getPropertyAssociation( definitions, 'language', 'label' );

    returns:

    {
        'php': 'PHP'
        'javascript': 'JavaScript'
    }

    Parameters

    languageDefs : Array.<CodeBlockLanguageDefinition>
    key : String
    value : String
    : Object.<String, String>
  • isModelSelectionInCodeBlock( selection ) → Boolean

    static

    Checks if any of the blocks within the model selection is a code block.

    Parameters

    selection : Selection

    Returns

    Boolean
  • rawSnippetTextToModelDocumentFragment( writer, text )

    static

    For a plain text containing the code (snippet), it returns a document fragment containing model text nodes separated by soft breaks (in place of new line characters "\n"), for instance:

    Input:

    "foo()\n
    bar()"

    Output:

    <DocumentFragment>
        "foo()"
        <softBreak></softBreak>
        "bar()"
    </DocumentFragment>

    Parameters

    writer : Writer
    text : String

    The raw code text to be converted.