Skip to content

Commit 7b61a85

Browse files
committed
Merge branch 'gh-pages' of https://github.com/LivelyKernel/lively4-core into gh-pages
2 parents c932eeb + dae57df commit 7b61a85

File tree

14 files changed

+163
-24
lines changed

14 files changed

+163
-24
lines changed

demos/markdown/mle-workspaces.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Workspace Example
2+
3+
This is an Example of a workspace embedded in HTML/Markdown that will persist it's code in browser local storage.
4+
<lively-script><script>import focalStorage from 'src/external/focalStorage.js'; import {SocketSingleton} from 'src/components/mle/socket.js'; const idMap = new Map(); const enclosingDiv = <div />; var mle = {}; async function startUp(){ await SocketSingleton.get(); let i = 0; while(await focalStorage.getItem(`markdown_workspace_${i}`)){ await new Pane(i, await focalStorage.getItem(`markdown_workspace_${i}_kind`)).create(); i++; } } class Pane { constructor(id, kind){ if(id !== undefined){ this._id = id; this.kind = kind; } else { this._id = idMap.size; } idMap.set(this.textStorageId, this); } onDoIt() { this.saveText() this.workspace.tryBoundEval(this.workspace.value) } async onReset() { if(this.kind) this.socket = await SocketSingleton.reset(); this.logarea.value = "LOGS"; this.drawarea.innerHTML = ""; } get defaultText() { return "sql`SELECT * FROM students`" } get textStorageId() { return `markdown_workspace_${this._id}` } async loadText() { var loaded = await focalStorage.getItem(this.textStorageId); if (loaded) return loaded; return this.defaultText; } async saveText() { focalStorage.setItem(this.textStorageId, this.workspace.value); focalStorage.setItem(`${this.textStorageId}_kind`, this.kind); } log(s) { this.logarea.value += s + ""; } async create() { // #TODO #META style and pre tags are problematic in Markdown scripts this.kind = this.kind === undefined ? await lively.confirm("MLE Workspace? Ok for yes, Cancel for No.") : this.kind; var style = document.createElement("style"); style.textContent = ` lively-code-mirror { border: 1px solid gray; flex: 4; }`; var buttons = <div> <button click={() => {this.onDoIt()} }>DoIt</button> <button click={() => {this.onReset()} }>reset</button> </div>; this.workspace = await (<lively-code-mirror></lively-code-mirror>); this.workspace.value = await this.loadText(); this.workspace.doitContext = this; this.logarea = <textarea disabled style="flex: 2;"/>; this.logarea.value = "LOGS"; this.drawarea = <div></div>; if(this.kind){ this.workspace.boundEval = async function(s) { this.socket = this.socket || await SocketSingleton.get(); this.socket.emit("test", {id: this.textStorageId, func: "evaluate", parameters: [s]}); const value = await new Promise((res) => { this.socket.on("result", r => { if(!r) return; if(r.id !== this.textStorageId) return; console.log(r.data) res(JSON.parse(r.data)); }); }); this.outData = value; Object.defineProperty(window, `$${this._id}`, {configurable: true, value}); this.log(JSON.stringify(value)); this.saveText(); return {value}; } this.workspace.boundEval = this.workspace.boundEval.bind(this); } enclosingDiv.appendChild( <div style="padding: 10px; width:90%;"> {style} <h4>{this.kind ? "MLE" : "DOM"} Workspace ${this._id}</h4> {buttons} <div style="display: flex;"> {this.workspace} {this.logarea} </div> {this.drawarea} </div> ); } } startUp().then(t => <div> <button click={() => new Pane().create()}>New workspace</button> {enclosingDiv} </div>)</script> </lively-script>

src/client/lang/lang-ext.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ extend(Object.prototype, {
6767
traverseAsAST(visitor) {
6868
return babel.traverse(this, visitor);
6969
},
70-
inspect() {
70+
openInInspector() {
7171
lively.openInspector(this);
7272
}
7373

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
here we go....
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"source":"/src/client/reactive/babel-plugin-polymorphic-identifiers/playground.js","plugin":"https://lively-kernel.org/lively4/aexpr/src/client/reactive/babel-plugin-polymorphic-identifiers/polymorphic-identifiers.js","options":{"autoUpdateAST":true,"autoUpdateTransformation":false,"autoExecute":true,"systemJS":false,"autoRunTests":false,"autoSaveWorkspace":false},"pluginSelection":[{"url":"https://lively-kernel.org/lively4/aexpr/src/client/reactive/babel-plugin-polymorphic-identifiers/polymorphic-identifiers.js"}]}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
const SHARED_FLAG_GENERATED_IMPORT_IDENTIFIER = 'SHARED_FLAG_GENERATED_IMPORT_IDENTIFIER';
3+
4+
export default function ({ types: t, template }) {
5+
return {
6+
name: 'polymorphic-identifiers',
7+
visitor: {
8+
Program: {
9+
enter(path, state) {
10+
function addCustomTemplate(file, name) {
11+
const declar = file.declarations[name];
12+
if (declar) {
13+
return declar;
14+
}
15+
16+
const identifier = file.declarations[name] = file.addImport("polymorphic-identifiers", name, name);
17+
identifier[SHARED_FLAG_GENERATED_IMPORT_IDENTIFIER] = true;
18+
return identifier;
19+
}
20+
21+
function hasDirective(path, name) {
22+
let foundDirective = false;
23+
path.traverse({
24+
Directive(path) {
25+
if (path.get("value").node.value === name) {
26+
foundDirective = true;
27+
}
28+
}
29+
});
30+
return foundDirective;
31+
}
32+
33+
debugger
34+
const shouldTransform = state.opts.executedIn === 'workspace' || hasDirective(path, "pi");
35+
if (!shouldTransform) {
36+
return;
37+
}
38+
39+
path.traverse({
40+
TaggedTemplateExpression(path) {
41+
if (path.node.visitedByPI) {
42+
return;
43+
}
44+
path.node.visitedByPI = true;
45+
46+
const tagTemplate = template(`MAKE_REF(TAG_NODE, {
47+
thisReference: this,
48+
evalFunction: str => eval(str)
49+
})`);
50+
const tagPath = path.get('tag');
51+
tagPath.replaceWith(tagTemplate({
52+
MAKE_REF: addCustomTemplate(state.file, 'makeRef'),
53+
TAG_NODE: tagPath.node
54+
}));
55+
56+
path.replaceWith(t.memberExpression(path.node, t.identifier('access')));
57+
58+
const parentPath = path.parentPath;
59+
if (parentPath.isBinaryExpression() && path.parentKey === 'left' && parentPath.node.operator === "<<") {
60+
parentPath.replaceWith(parentPath.node.right);
61+
62+
// find something we can embed an assignment expression in
63+
const preStatementAncestor = parentPath.find(p => {
64+
const parent = p.parentPath;
65+
return parent && (parent.isStatement() || (parent.isArrowFunctionExpression() && p.parentKey === "body"))
66+
});
67+
preStatementAncestor.replaceWith(t.assignmentExpression('=', path.node, preStatementAncestor.node));
68+
}
69+
}
70+
});
71+
}
72+
}
73+
}
74+
};
75+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Polymorphic-Identifiers
2+
3+
```JavaScript
4+
import { PIReference } from 'polymorphic-identifiers';
5+
6+
// example scheme
7+
class qa extends PIReference {
8+
read() {
9+
const { elements } = this.parse();
10+
return elements;
11+
}
12+
write(v) {
13+
const { elements, type, prop } = this.parse();
14+
15+
if (type === 'style') {
16+
return elements.forEach(e => e.style[prop] = v)
17+
}
18+
elements.forEach(e => e.innerHTML = v)
19+
}
20+
21+
// helper
22+
parse() {
23+
const [selector, type, prop] = this.strings.first.split('/');
24+
const elements = this.query(selector);
25+
return { selector, type, prop, elements };
26+
}
27+
query(selector) {
28+
return Array.from(document.querySelectorAll(selector));
29+
}
30+
}
31+
32+
// usage
33+
// calls `read`
34+
qa`lively-window`.forEach(lw => lively.showElement(lw))
35+
36+
// assignments with <<
37+
// calls `write`
38+
qa`#rect` << 'html'
39+
qa`#rect/style/border-color` << 'green'
40+
```

src/client/reactive/polymorphic-identifiers/polymorphic-identifiers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
32
export class PIReference {
43
static get isPIReference() { return true; }
54

@@ -8,7 +7,8 @@ export class PIReference {
87
}
98

109
create(strings, ...expressions) {
11-
// debugger
10+
this.strings = strings;
11+
this.expressions = expressions;
1212
}
1313

1414
get access() {

src/client/reactive/test/polymorphic-identifiers/polymorphic-identifiers.spec.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,19 @@ describe("PI", function() {
117117
})``.access).to.equal('bar')
118118
});
119119

120-
// it("inserts default constructors", () => {
121-
// class A {
122-
// constructor() {
123-
// this.prop = 42;
124-
// }
125-
// }
126-
// class B extends A {}
120+
});
121+
describe("AEs", function() {
122+
123+
it("AExprs insert default constructors", () => {
124+
class A {
125+
constructor(prop) {
126+
this.prop = prop;
127+
}
128+
}
129+
class B extends A {}
127130

128-
// const b = new B();
129-
// expect(b.prop).to.equal(42);
130-
// });
131+
const b = new B(42);
132+
expect(b.prop).to.equal(42);
133+
});
131134

132135
});

src/client/vivide/scripts/scripts/vivide-script-future-week.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)