From f7a55a84f7ad524438dba84fce829c715aa5bc19 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Tue, 29 Aug 2023 20:54:40 -0700 Subject: [PATCH] feat: Create geogebra applet and implement some Joyce commands --- public/inscribed-modified.html | 2 +- src/adapptlet.civet | 89 +++++++++++++++++++++++++++++++++- 2 files changed, 88 insertions(+), 3 deletions(-) diff --git a/public/inscribed-modified.html b/public/inscribed-modified.html index c52e186..298f879 100644 --- a/public/inscribed-modified.html +++ b/public/inscribed-modified.html @@ -15,7 +15,7 @@
- + diff --git a/src/adapptlet.civet b/src/adapptlet.civet index a1f3082..354f06a 100644 --- a/src/adapptlet.civet +++ b/src/adapptlet.civet @@ -1,2 +1,87 @@ -import https://code.jquery.com/jquery-3.7.1.slim.js -$('body').append '
Hello World
' +import https://code.jquery.com/jquery-3.7.1.js + +joyceApplets := [] +$('applet[code="Geometry"]').before (i, html) -> + id := `joyceApplet${i}` + joyceApplets.push { html, this.children, id, + width: parseInt(this.getAttribute 'width'), + height: parseInt(this.getAttribute 'height') } + `
` + +jQuery.getScript 'https://www.geogebra.org/apps/deployggb.js', => + for each jApp of joyceApplets + params := { + appName: 'classic', + jApp.width, + jApp.height, + appletOnLoad: (api) => + for child of jApp.children + dispatchJcommand api, child + api.setCoordSystem(-10, 10 + jApp.width, -10, 10 + jApp.height) + } + geoApp := new GGBApplet params + geoApp.inject jApp.id + +type Cmdr + command: string + callbacks: ((api) => boolean)[] + +function dispatchJcommand(api, param) + val := param.getAttribute 'value' + switch param.getAttribute 'name' + 'background' + api.setGraphicsOptions 1, bgColor: `#${val}` + 'title' + api.evalCommand `TitlePoint = Corner(1,1) + Text("${val}", TitlePoint + (2,5))` + /e\[\d+\]/ + {command, callbacks} := jToG val + if command + if api.evalCommand command + for each cb of callbacks + cb(api) + else + console.log `Geogebra command '${command}' translated from`, + val, 'failed.' + else + console.log `Unknown command '${val}'` + else + console.log `Unkown param ${param}` + +function jToG(jCom: string): Cmdr + [name, klass, method, data, ...colors] := jCom.split(';') + if klass in classHandler + return classHandler[klass] name, method, data, colors + console.log `Unknown entity class ${klass}` + return '' + +classHandler := + point: (name: string, m: string, data: string, colors: string[]): Cmdr => + command .= '' + callbacks .= [] + switch m + /free|fixed/ + command += `${name} = (${data})` + if m is 'fixed' + callbacks.push (api) => api.setFixed(name, true) + 'perpendicular' + [center, direction] := data.split(',') + command += `${name} = Rotate(${direction}, 3*pi/2, ${center})` + return {command, callbacks} + + line: (name: string, m: string, data: string, colors: string[]): Cmdr => + command .= '' + callbacks .= [] + switch m + 'connect' + command += `${name} = Segment(${data})` + return {command, callbacks} + + circle: (name: string, m: string, data: string, colors: string[]): Cmdr => + command .= '' + callbacks .= [] + switch m + 'radius' + [center, point] := data.split(',') + command += `${name} = Circle(${center}, ${point})` + return {command, callbacks}