code-block/utils
Functions
-
canBeCodeBlock( schema, element ) → boolean
internalmodule:code-block/utils~canBeCodeBlock
Checks if an Element can become a code block.
Parameters
schema : ModelSchema
Model's schema.
element : ModelElement
The element to be checked.
Returns
boolean
Check result.
-
getCodeBlockAriaAnnouncement( t, languageDefs, element, direction ) → string
internalmodule:code-block/utils~getCodeBlockAriaAnnouncement
Get the translated message read by the screen reader when you enter or exit an element with your cursor.
Parameters
t : LocaleTranslate
languageDefs : Array<CodeBlockLanguageDefinition>
element : ModelElement
direction : 'enter' | 'leave'
Returns
string
-
getIndentOutdentPositions( model ) → Array<ModelPosition>
internalmodule:code-block/utils~getIndentOutdentPositions
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>[]foo</codeBlock> -> <codeBlock>^foo</codeBlock> <codeBlock>foo[]bar</codeBlock> -> <codeBlock>foo^bar</codeBlock>
It also works across other non–code blocks:
<codeBlock>[]foo</codeBlock> -> <codeBlock>^foo</codeBlock> <codeBlock>foo[]bar</codeBlock> -> <codeBlock>foo^bar</codeBlock>
Note: The positions are in reverse order so they do not get outdated when iterating over them and the writer inserts or removes elements at the same time.
Note: The position is located after the leading white spaces in the text node.
Parameters
model : Model
Returns
Array<ModelPosition>
-
getLeadingWhiteSpaces( textNode ) → string
internalmodule:code-block/utils~getLeadingWhiteSpaces
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
textNode : ModelText
Returns
string
-
getNormalizedAndLocalizedLanguageDefinitions( editor ) → Array<CodeBlockLanguageDefinition>
internalmodule:code-block/utils~getNormalizedAndLocalizedLanguageDefinitions
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 uset
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>
- To consider the editor localization, i.e. to display
-
getPropertyAssociation( languageDefs, key, value ) → Record<string, string>
internalmodule:code-block/utils~getPropertyAssociation
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:
const definitions = { { language: 'php', class: 'language-php', label: 'PHP' }, { language: 'javascript', class: 'js', label: 'JavaScript' }, }; getPropertyAssociation( definitions, 'class', 'language' );
and
const definitions = { { language: 'php', class: 'language-php', label: 'PHP' }, { language: 'javascript', class: 'js', label: 'JavaScript' }, }; getPropertyAssociation( definitions, 'class', 'language' );
returns:
const definitions = { { language: 'php', class: 'language-php', label: 'PHP' }, { language: 'javascript', class: 'js', label: 'JavaScript' }, }; getPropertyAssociation( definitions, 'class', 'language' );
Parameters
languageDefs : Array<CodeBlockLanguageDefinition>
key : keyof CodeBlockLanguageDefinition
value : keyof CodeBlockLanguageDefinition
Returns
Record<string, string>
-
getTextNodeAtLineStart( position, model ) → null | ModelText
internalmodule:code-block/utils~getTextNodeAtLineStart
For given position, finds the closest position that is at the beginning of a line of code and returns a text node that is at the beginning of the line (or
null
if there's no text node at the beginning of a given line).Line beings at the start of a code block element and after each
softBreak
element.Note: even though code block doesn't allow inline elements other than
<softBreak>
by default, some features may overwrite this rule, so such inline elements are taken into account.Some examples of expected results:
<codeBlock>^</codeBlock> -> null <codeBlock>^foobar</codeBlock> -> <codeBlock>[foobar]</codeBlock> <codeBlock>foobar^</codeBlock> -> <codeBlock>[foobar]</codeBlock> <codeBlock>foo^bar</codeBlock> -> <codeBlock>[foobar]</codeBlock> <codeBlock>foo^<softBreak />bar</codeBlock> -> <codeBlock>[foo]<softBreak />bar</codeBlock> <codeBlock>foo<softBreak />bar^</codeBlock> -> <codeBlock>foo<softBreak />[bar]</codeBlock> <codeBlock>foo<softBreak />b^ar</codeBlock> -> <codeBlock>foo<softBreak />[bar]</codeBlock> <codeBlock>foo<softBreak />^bar</codeBlock> -> <codeBlock>foo<softBreak />[bar]</codeBlock> <codeBlock>^<element /></codeBlock> -> null <codeBlock><element />^</codeBlock> -> null <codeBlock>foo^<element /></codeBlock> -> <codeBlock>[foo]<element /></codeBlock> <codeBlock>foo<element />^</codeBlock> -> <codeBlock>[foo]<element /></codeBlock> <codeBlock>foo<element />bar^</codeBlock> -> <codeBlock>[foo]<element />bar</codeBlock> <codeBlock><element />bar^</codeBlock> -> null <codeBlock>foo<softBreak />^<softBreak /></codeBlock> -> null <codeBlock>foo<softBreak />^<element /></codeBlock> -> null <codeBlock>foo<softBreak /><element />^</codeBlock> -> null <codeBlock>foo<softBreak />bar<element />^</codeBlock> -> <codeBlock>foo<softBreak />[bar]<element /></codeBlock> <codeBlock>foo<softBreak /><element />ba^r</codeBlock> -> null
Parameters
position : ModelPosition
model : Model
Returns
null | ModelText
-
isModelSelectionInCodeBlock( selection ) → boolean
internalmodule:code-block/utils~isModelSelectionInCodeBlock
Checks if any of the blocks within the model selection is a code block.
Parameters
selection : ModelDocumentSelection
Returns
boolean
-
rawSnippetTextToViewDocumentFragment( writer, text ) → ViewDocumentFragment
internalmodule:code-block/utils~rawSnippetTextToViewDocumentFragment
For plain text containing the code (a snippet), it returns a document fragment containing view text nodes separated by
<br>
elements (in place of new line characters "\n"), for instance:Input:
"foo()\n bar()"
Output:
"foo()\n bar()"
Parameters
writer : ViewUpcastWriter
text : string
The raw code text to be converted.
Returns