diff --git a/js-assembler/runtime-src/baselib-primitives.js b/js-assembler/runtime-src/baselib-primitives.js index bc2f22d..036649d 100644 --- a/js-assembler/runtime-src/baselib-primitives.js +++ b/js-assembler/runtime-src/baselib-primitives.js @@ -272,6 +272,23 @@ + installPrimitiveProcedure( + 'current-error-port', + makeList(0, 1), + function (MACHINE) { + if (MACHINE.argcount === 1) { + MACHINE.params['currentErrorPort'] = + checkOutputPort(MACHINE, 'current-output-port', 0); + return VOID; + } else { + return MACHINE.params['currentOutputPort']; + } + }); + + + + + installPrimitiveProcedure( diff --git a/scribblings/cs19.scrbl b/scribblings/cs19.scrbl index 99205fd..a921552 100644 --- a/scribblings/cs19.scrbl +++ b/scribblings/cs19.scrbl @@ -475,3 +475,41 @@ For example, } + + +@section{Tips and Tricks} +@subsection{Hiding standard output or directing it to an element} + +@declare-exporting/this-package[web-world] + +For a web-world program, output written by normal side effects such as +@racket[printf] or @racket[display] is still written to the current +output port, whose default behavior appends to the end of +@tt{document.body}. You may want to either disable such printing or +direct the output to a particular element on the page. For such +purposes, use a combination of @racket[current-output-port] and +@racket[open-output-element]. + +For example, in +@codeblock|{ +... +(current-output-port (open-output-element "stdout-div")) +... +(big-bang ... + (on-tick (lambda (world dom) + (printf "Tick!\n") + (add1 world))) + ...) +}| + +All subsequent I/O side effects after the call to +@racket[current-output-port] will be written out to the +@tt{stdout-div}, which can be easily styled with @tt{display: none} to +hide it from normal browser display. + + +@defproc[(open-output-element [id string]) output-port]{ +Opens an output port that will be directed to write to the DOM element +whose id is @racket[id]. Note: writing to this port shouldn't fail, +even if the id does not currently exist on the page. +} \ No newline at end of file diff --git a/web-world/impl.rkt b/web-world/impl.rkt index 9d5df74..5143935 100644 --- a/web-world/impl.rkt +++ b/web-world/impl.rkt @@ -28,9 +28,8 @@ ;; draw and update the view to-draw - with-output-to - open-output-element - + ;; helper: open an element as an output port. + open-output-element ;; coerse to view ->view diff --git a/web-world/js-impl.js b/web-world/js-impl.js index ffa8321..071c579 100644 --- a/web-world/js-impl.js +++ b/web-world/js-impl.js @@ -1109,12 +1109,12 @@ DomElementOutputPort.prototype.writeDomNode = function (MACHINE, v) { $("#" + this.id).append(v); + $(v).trigger({type : 'afterAttach'}); + $('*', v).trigger({type : 'afterAttach'}); }; - - ////////////////////////////////////////////////////////////////////// var checkReal = plt.baselib.check.checkReal; @@ -1455,13 +1455,6 @@ onChange); }); - EXPORTS['with-output-to'] = makePrimitiveProcedure( - 'with-output-to', - 1, - function(MACHINE) { - var outputPort = checkOutputPort(MACHINE, 'with-output-to', 0); - return new WithOutputToHandler(outputPort); - }); EXPORTS['open-output-element'] = makePrimitiveProcedure( 'open-output-element', diff --git a/web-world/racket-impl.rkt b/web-world/racket-impl.rkt index decca65..04d5dbd 100644 --- a/web-world/racket-impl.rkt +++ b/web-world/racket-impl.rkt @@ -21,7 +21,6 @@ view-hide view-append-child - with-output-to open-output-element) @@ -121,9 +120,5 @@ (error 'view-append "Please run in JavaScript context.")) -(define (with-output-to output-port) - (error 'with-output-to "Please run in JavaScript context.")) - - (define (open-output-element id) (error 'open-output-element "Please run in JavaScript context."))