Skip to content

Add auto-complete to the P5 widget #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 48 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
bb1743a
slowly upgrade
citelao Dec 28, 2021
8f6144f
imports now compile
citelao Dec 28, 2021
971658e
messing with stuff
citelao Dec 28, 2021
bfb627d
autocomplete
citelao Dec 28, 2021
88f2113
start upgrading webpack etc
citelao Dec 28, 2021
096d7b5
switch to newer tests
citelao Dec 28, 2021
69ec1d6
begin consuming monaco
citelao Dec 28, 2021
8ca7bcf
move stuff and fix query params
citelao Dec 28, 2021
4ea0156
My friends, we have Monaco
citelao Dec 28, 2021
eaf3c0f
js autocomplete mfs
citelao Dec 28, 2021
c548132
import d.ts
citelao Dec 28, 2021
0a604ec
still works!
citelao Dec 28, 2021
97688fb
fix inline highlight
citelao Dec 28, 2021
9cf4159
remove some codemirror refs
citelao Dec 28, 2021
1fe749f
begin theming
citelao Dec 28, 2021
663ddb6
start highlighting string
citelao Dec 28, 2021
3a92f80
start colors
citelao Dec 29, 2021
d58cdf1
remove old dts
citelao Dec 29, 2021
845a435
better theme
citelao Dec 29, 2021
dfb8fa4
trying to do better layout
citelao Dec 29, 2021
ef0b191
gutter bg and remove guides
citelao Dec 29, 2021
a380321
lots more js code complete stuff
citelao Dec 29, 2021
25cdc1e
undo/redo buttons appear
citelao Dec 29, 2021
b30ca13
undo and redo buttons work!
citelao Dec 29, 2021
7cf81df
legit check undefined
citelao Dec 29, 2021
32982d4
ed
citelao Jan 16, 2022
7ad4c0f
fix svg logo
citelao Jan 16, 2022
9bcbbed
port some stuff
citelao Jan 16, 2022
cf33f3d
and that
citelao Jan 16, 2022
19216be
that's the best I can do I think
citelao Jan 16, 2022
4501bd4
note to self
citelao Jan 16, 2022
01d5093
remove codemirror dep
citelao Jan 16, 2022
040733b
fix overscrolling
citelao Jan 16, 2022
e3aca97
tentative changes to update script
citelao Jan 16, 2022
d95d9f3
add non-working full-screen
citelao Jan 16, 2022
037016d
working full-screen page
citelao Jan 16, 2022
7c95d3d
style closely to UI buttons
citelao Jan 16, 2022
24536ca
comment about not fullscreen
citelao Jan 16, 2022
ab22445
fix production builds
citelao Jan 16, 2022
4f6bc01
only ship typescript & js
citelao Jan 16, 2022
eb8d314
size note
citelao Jan 16, 2022
13eaab7
create a basic JS version of the update script
citelao Jan 16, 2022
0b5310f
rebuild
citelao Jan 16, 2022
040b73d
publish to gh-pages
citelao Jan 16, 2022
96b4fd8
add ghpages
citelao Jan 16, 2022
f16e979
make it a devdep
citelao Jan 16, 2022
3328ad4
npm run publish
citelao Jan 16, 2022
c1d1024
you can also specify a percentage
citelao Jan 16, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 51 additions & 18 deletions lib/editor.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import React = require("react");

import CodeMirror = require("codemirror");
// import CodeMirror = require("codemirror");
import * as Monaco from 'monaco-editor';

import "codemirror/mode/javascript/javascript.js";

import PureComponent from "./pure-component";

// TODO: versions
const p5dts = require("!!raw-loader!@types/p5/global.d.ts") as string;
const p5Uri = "ts:p5.d.ts";
// const p5domdts = require("raw-loader!@types/p5/lib/addons/p5.dom.d.ts") as string;
// const p5sounddts = require("raw-loader!@types/p5/lib/addons/p5.sound.d.ts") as string;

// It seems like CodeMirror behaves oddly with a flexbox layout, so
// we will manually size it. However, Chrome seems to have a bug
// whereby we need to wait a bit before resizing it after the
Expand All @@ -22,7 +29,7 @@ interface State {
}

export default class Editor extends PureComponent<Props, State> {
_cm: CodeMirror.Editor
_cm: Monaco.editor.IStandaloneCodeEditor
_resizeTimeout: number
_errorLineHandle: any

Expand All @@ -33,30 +40,53 @@ export default class Editor extends PureComponent<Props, State> {
}
if (this.props.errorLine !== prevProps.errorLine) {
if (this._errorLineHandle) {
this._cm.removeLineClass(this._errorLineHandle, 'background',
'error-line');
this._cm.deltaDecorations(this._errorLineHandle, []);
// this._cm.removeLineClass(this._errorLineHandle, 'background',
// 'error-line');
this._errorLineHandle = null;
}
if (this.props.errorLine) {
this._errorLineHandle = this._cm.addLineClass(
this.props.errorLine - 1,
'background',
'error-line'
);
this._errorLineHandle = this._cm.deltaDecorations([], [
{
range: {
startColumn: 0,
startLineNumber: this.props.errorLine - 1,
endColumn: 0,
endLineNumber: this.props.errorLine,
},
options: {
linesDecorationsClassName: 'error-line'
}
}
]);
// this._errorLineHandle = this._cm.addLineClass(
// this.props.errorLine - 1,
// 'background',
// 'error-line'
// );
}
}
}

componentDidMount() {
this._cm = CodeMirror(this.refs.container, {
this._cm = Monaco.editor.create(this.refs.container, {
theme: 'p5-widget',
value: this.props.content,
lineNumbers: true,
mode: 'javascript'
lineNumbers: "on",
language: "javascript"
});
this._cm.on('change', () => {

Monaco.languages.typescript.javascriptDefaults.addExtraLib(p5dts, p5Uri);
// When resolving definitions and references, the editor will try to use created models.
// Creating a model for the library allows "peek definition/references" commands to work with the library.
Monaco.editor.createModel(p5dts, 'typescript', Monaco.Uri.parse(p5Uri));


this._cm.onDidChangeModelContent(() => {
if (this.props.onChange) {
let size = this._cm.getDoc().historySize();
// let size = this._cm.getDoc().historySize();
// TODO:
let size = { undo: 0, redo: 0 };
this.props.onChange(this._cm.getValue(),
size.undo > 0, size.redo > 0);
}
Expand All @@ -76,15 +106,17 @@ export default class Editor extends PureComponent<Props, State> {
}

undo() {
this._cm.getDoc().undo();
// TODO:
// this._cm.executeCommand();
}

redo() {
this._cm.getDoc().redo();
// TODO:
// this._cm.getDoc().redo();
}

resizeEditor = () => {
let wrapper = this._cm.getWrapperElement();
let wrapper = this._cm.getContainerDomNode();
let oldDisplay = wrapper.style.display;

// We need to get the size of our container when it's
Expand All @@ -96,7 +128,8 @@ export default class Editor extends PureComponent<Props, State> {

wrapper.style.display = oldDisplay;

this._cm.setSize(null, rectHeight);
// TODO: may be unnec.
// this._cm.resize(null, rectHeight);
}

// http://stackoverflow.com/a/33826399/2422398
Expand Down
20 changes: 9 additions & 11 deletions lib/main.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import React = require("react");
import ReactDOM = require("react-dom");

import url = require("url");

import * as defaults from "./defaults";
import { SessionStorageAutosaver } from "./autosaver";
import App from "./app";

let defaultSketchJS = require("raw!./default-sketch.js") as string;
let defaultSketchJS = require("raw-loader!./default-sketch.js") as string;

require("../node_modules/codemirror/lib/codemirror.css");
require("../css/style.css");
require("../css/p5-widget-codemirror-theme.css");

function start() {
let embeddingPageURL = document.referrer;
let qs = url.parse(window.location.search, true).query;
let id = embeddingPageURL + '_' + qs['id'];
let baseSketchURL = qs['baseSketchURL'] || embeddingPageURL;
let autoplay = (qs['autoplay'] === 'on');
let initialContent = qs['sketch'] || defaultSketchJS;
let p5version = qs['p5version'] || defaults.P5_VERSION;
let previewWidth = parseInt(qs['previewWidth']);
let maxRunTime = parseInt(qs['maxRunTime'])
let qs = new URLSearchParams(window.location.search);
let id = embeddingPageURL + '_' + qs.get('id');
let baseSketchURL = qs.get('baseSketchURL') || embeddingPageURL;
let autoplay = (qs.get('autoplay') === 'on');
let initialContent = qs.get('sketch') || defaultSketchJS;
let p5version = qs.get('p5version') || defaults.P5_VERSION;
let previewWidth = parseInt(qs.get('previewWidth'));
let maxRunTime = parseInt(qs.get('maxRunTime'))
if (isNaN(previewWidth)) {
previewWidth = defaults.PREVIEW_WIDTH;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/preview-frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface PreviewFrameWindow extends PreviewFrame.Runner {
p5: (sketch?: Function, node?: HTMLElement, sync?: boolean) => void;
}

let global = window as PreviewFrameWindow;
let global = window as unknown as PreviewFrameWindow;

function loadScript(url, cb?: () => void) {
let script = document.createElement('script');
Expand Down
Loading