From 0a8b7c60b65c392e84aa6bb243e6d9a6477dade6 Mon Sep 17 00:00:00 2001 From: Jakub Jankiewicz Date: Fri, 11 Oct 2024 23:58:47 +0200 Subject: [PATCH] add REPL tracking --- docs/src/components/Interpreter/index.tsx | 26 +++++++++++++++++++-- docs/src/components/Interpreter/terminal.ts | 4 ++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/docs/src/components/Interpreter/index.tsx b/docs/src/components/Interpreter/index.tsx index 223f379c..9e027f14 100644 --- a/docs/src/components/Interpreter/index.tsx +++ b/docs/src/components/Interpreter/index.tsx @@ -15,6 +15,9 @@ export interface TerminalProps extends CSSProperties { '--size': string; } +type PiwikTrack = Array>; +export type JQueryTerminal = ReturnType; + const replReady = () => { return ( globalThis.jQuery && @@ -24,7 +27,22 @@ const replReady = () => { ); } +// monkey patch keymap added by LIPS terminal +function trackCommands(term: JQueryTerminal) { + const _paq = globalThis._paq as PiwikTrack + let command: string; + term.option('onCommandChange', (cmd: string) => { + command = cmd; + }); + const ENTER = term.cmd().keymap('ENTER'); + term.cmd().keymap('ENTER', function(e: KeyboardEvent, orig: () => any) { + _paq.push(['trackEvent', 'REPL', 'command', command]); + return ENTER(e, orig); + }); +} + export default function Interpreter(): JSX.Element { + const _paq = globalThis._paq as PiwikTrack const [activeSnippet, setActiveSnippet] = useState(0); const [size, setSize] = useState(1); const ref = useRef(); @@ -43,7 +61,8 @@ export default function Interpreter(): JSX.Element { useLayoutEffect(() => { (function loop() { if (replReady() && styleReady()) { - initTerminal(); + const term = initTerminal(); + trackCommands(term); } else { setTimeout(loop, 100); } @@ -53,10 +72,13 @@ export default function Interpreter(): JSX.Element { function execSnippet(selector = '.example:visible') { const $ = globalThis.jQuery; - const code = $(selector).text(); + const $snippet = $(selector); + const code = $snippet.text(); + const index = $snippet.closest('li').index(); const term = $('.term').terminal(); term.echo(term.get_prompt(), { formatters: false }); term.exec(code, true); + _paq.push(['trackEvent', 'REPL', 'snippet', index + 1]); if (typeof screen.orientation === 'undefined') { setTimeout(() => term.focus(), 0); } diff --git a/docs/src/components/Interpreter/terminal.ts b/docs/src/components/Interpreter/terminal.ts index c0d3ca8b..5b0ad3a2 100644 --- a/docs/src/components/Interpreter/terminal.ts +++ b/docs/src/components/Interpreter/terminal.ts @@ -2,6 +2,7 @@ let original: string; export function initTerminal() { const $ = globalThis.$; + $.terminal.syntax('scheme'); const $term = $('.term'); if (!original) { @@ -14,6 +15,7 @@ export function initTerminal() { name: 'demo', lips: globalThis.lips }); + term.tooltips($('.example'), ($node: any) => $node.text(), function($node: any) { const offset = $node.offset(); const top = offset.top; @@ -35,6 +37,8 @@ to display list of matched names in environment.` }), { formatters: false }); + + return term; }; export function destroyTerminal() {