PageConsole with TrimPre Test
Basic Usage
First, import the neccessary modules on the page:
And for each TrimPre widget on the page, if it was written as followed:
It comes with the help of PageConsole, to which a variable named pc references. Then, we can write the codes in the TrimPre widget, such as:
The effects of above are:
- Source codes are colorfied with supporting for both dark theme and light theme.
- The source codes would be run automaticlly with the awareness of PageConsole.
- The results of pc.log would be directed to the web page by default.
- A dynamic div is created below the TrimPre widget, in which shows the results of the source codes.
Compared with LiveEditor, it's light-weight, with sources un-editable, and has a broader viewport.
Direct the output
We can add a CONSOLE
in the class attribute, in order to direct the outputs to the console only.
The available attributes are: PAGE
(default setting), CONSOLE
, and BOTH
.
So, we can control the direction of each TrimPre widget independently.
The Freedom to Import
Writing the await import() statements in the block, we can import what we need at will.
The result of importing would be automaticlly resolved, so we avoid being bothered with having to invoke the then method.
Basically, when loading a relative URL, we should have writen:
Since the code is so tedious, the utility has done this for us:
So the simply writing:
would be enough.
Tabular Texts
The font-family of PageConsole is monospace
, so it's a perfect place to output the tabular formatted texts, which is the shortage of the console itself.
Dealing with Exceptions
PageConsole is smart enough to handle various exceptions by default.
And even for Promise rejections.
By invoking the showException in the catch method, we route the exception message from the console to page in a custom formatting.
Dealing with Long Titles and Properties
For arrays with too many elements or objects with too many properties, their titles may be too long and properties may be too many.
These titles would strip some contents add ...
at the end, decided by whether it's on mobile devices or desktop displays.
The child nodes to be shown would be increased by 20 items each time, if the user click on the show next ...
string.
Too many properties in an object:
A sample paragraph.
`)[0]; let cssStyleDeclaration = getComputedStyle(p); pc.log(cssStyleDeclaration);Implementation of the Specification
PageConsole is not the static cloning of the console object, but rather a trying to implement the Console Standard from scratch.
Supporting Format Specifiers
Logging Multiple Values in a Shot
For such multiple values in an invokation, they are grouped into a cell, separated by ,
, with simple data types sharing a line and compound data types occuping the whole line to be freely expanded.
Pre-implementation
The %c specifier would apply the CSS style, yet it is in a state of future in the specification. So we're going ahead little bit here.
String Representations
In the following situations, the representation of strings should be surrounded with "...":
However, under the circumstances of specifiers, strings should not be surrounded with "..." by default:
In such case, if necessary, we can add "..." or '...' manually.
Grouping
Grouping is a nested, expandable stack container.
Tables
Assertion
Error
Information
Warning
Extra JavaScript Source
Suppose we have a file named setup-canvas.js with the contents:
Add an attribute just as:
Then the running order would be:
- pc is initialized first
- canvas is created and appended to the DOM tree with the help of pc
- the canvas would be logged finally
In this way, we can move lots of same common source codes in a page into a specific file, saving spaces and times.
See also
For stand-alone usage of PageConsole, see Page Console Test。
For stand-alone usage of TrimPre, see TrimPre Test。
For high-lighted features of TrimPre, see Highlight with TrimPre Test。
