A set of generic utility functions used in this project.
Members
isOnScreen
TinyMCE sometimes stores elements off screen, but still part of the document, so if we search for elements by selector, we will find them, even though they are invisible and should not be taken into account as part of the user's document content. This function checks to see if a given DOM nodes is really a visible, relevant part of the document or not.
Source
Methods
appURL()
Return the URL for this app. It is taken directly from the browser's
navigation bar, but it excludes any query string that may be present.
It will be of the form protocol://domain.name/path/to/file.html
.
See
- autoOpenLink()
Source
copyWithoutPrototype(object) → {Object}
From any JavaScript object, we can create another object by first constructing a new instance of the Object prototype, then copying over all of the "own" properties from the original object (by reference). This is like the original object, but without its prototype or any other inherited data or methods. This function does that.
Parameters
-
object
Object
any JavaScript object
Returns
-
Object
a shallow copy of the original object, but without copying its prototype information
Source
editorForNode(node) → {tinymce.Editor}
Given a DOM Node, find the TinyMCE editor containing it.
Parameters
-
node
Node
HTML node for which to find the editor
Returns
-
tinymce.Editor
the editor whose document contains the given node
Source
escapeHTML(text) → {string}
Escape a string so that it can be safely inserted into an HTML document and
still represent the plain text within the given string (not interpreting the
string as HTML itself). For example, the string "x < a and a > b"
should
appear exactly that way in the rendered HTML, meaning that the <
and >
will need to be escaped so that "<a and a>"
does not appear to be a tag.
Parameters
-
text
string
text to escape for insertion into HTML
Returns
-
string
the same text, but with the characters
"&"
,"<"
,">"
,"'"
, and'"'
replaced with character references instead
Source
loadScript(url) → {Promise}
Create a script element to load a script from the given URL, append that
script element to the page's head, and notify us (via a returned Promise
)
when the script completes loading successfully or fails with an error.
Example use:
loadScript( 'https://some.cdn.org/script.js' ).then( () => {
// Run code that depends on the script having loaded.
} )
Note that if this function has already been called on this URL, so that there already is a script tag with this source, then the promise resolves immediately without doing anything first.
Parameters
-
url
String
URL of the script to load
Returns
-
Promise
a promise that is resolved if the script finishes loading or rejected if the script encounters an error
Source
onlyBefore(nodes, point) → {Array.<Node>}
Given an ordered set of HTML Nodes in an array, and a node in the same
document, return just the subset of nodes
that appear before point
.
Because the given set of nodes are in order, this subset will always be an
initial segment of the given array. It can be empty (if none precede point
)
and it can be the whole array (if all preceded point
).
While this could be done with a simple array filter, that could be slow on larger arrays; this uses a binary search. Furthermore, node comparisons are a tedious process that uses an enum, so this function is simpler.
Parameters
-
nodes
Array.<Node>
ordered array of Nodes to filter
-
point
Node
the node that will determine which subset of
nodes
gets rerturned
Returns
-
Array.<Node>
some initial segment of
nodes
, including precisely those that appear beforepoint
Source
removeScriptTags(html) → {string}
The following function takes as input an string containing HTML code and removes from it all script tags, so that the code can be used safely within the app itself, knowing that no malicious code will be executed.
Parameters
-
html
string
the HTML code from which to remove script tags
Returns
-
string
the same HTML code, but with all script tags removed
Source
simpleHTMLTable(…rows) → {string}
This function makes it easy to construct two-column tables of HTML content, which is something that several types of Atoms will want to do. The arguments to the function are the rows of the table, and they are treated as follows.
- Any string is treated as the content for a row of the table spanning both
columns (using
colspan
) and in bold font. - An array of strings containing just one entry is treated the same as a single string.
- An array of two strings is treated as the contents of the two cells in the row.
- An array of three strings is treated as two rows, first a two-cell row, and then an optional error row (only if the third entry is not falsy) that places the error message in red font in the second cell.
Example use:
simpleHTMLTable(
'Here is the information you entered:',
[ 'Your name:', 'Frederick the Great' ],
[ 'Your age:', '42' ],
[ 'Your favorite color:', color,
!isColor(color) && 'That is not a valid color.' ]
)
Parameters
-
rows
any
<repeatable>
the data representing the rows of the table to construct
Returns
-
string
the HTML code for the table
Source
checkExtension(name, extopt) → {string}
Checks a filename to see if it has the right extension and adds the extension if it doesn't. The default extension is 'js'.
Parameters
-
name
string
The filename to check.
-
ext
string
<optional>
'js'The extension to add if missing.
Returns
-
string
- The filename with the correct extension.
Source
checkFolder(folder) → {string}
Checks a foldername to see if it has a trailing '/' and adds it if if it doesn't.
Parameters
-
folder
string
The folder name to check.
Returns
-
string
- The foldername with the correct extension.
Source
commonInitialSlice(…arrays) → {Array}
Find the longest common initial slice of a set of arrays.
Parameters
-
arrays
Array
<repeatable>
The arrays to compare.
Returns
-
Array
- The longest common initial slice of the arrays.
Source
indent(s, n) → {string}
Indent string $s$ with a tab of $n$ spaces. If $s$ contains multiple lines, indent each line.
Parameters
-
s
string
the string to indent
-
n
number
the number of spaces to indent
Returns
-
string
the resulting string
Source
lineNum(n, widthopt, suffixopt) → {string}
Returns a right justified line number consisting of a minimum width padded on the right with a given suffix.
Parameters
-
n
number
The line number.
-
width
number
<optional>
4The minimum width of the line number.
-
suffix
string
<optional>
': 'The suffix to be added to the line number.
Returns
-
string
The right justified line number.
Source
rgb2hex()
Convert RGB to HEX
Source
subscript(n)
Convert the integer $n$ to a string consisting of the corresponding unicode subscript.
Parameters
-
n
number
The integer to be converted.
Source
tab(n, charopt) → {string}
Return a string of $n$ spaces (or other character).
Parameters
-
n
number
the length of the string
-
char
string
<optional>
' 'the character to use
Returns
-
string
the resulting string
Source
timer()
Report the time it took to execute function f
, passed as an argument.