Skip to content
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

Code editor #71

Merged
merged 32 commits into from
Nov 22, 2022
Merged

Code editor #71

merged 32 commits into from
Nov 22, 2022

Conversation

adamziel
Copy link
Collaborator

@adamziel adamziel commented Nov 17, 2022

Description

Adds a code editor with a live preview.

The architecture works as follows:

Code editor architecture

  • The CodeMirror editor is set up to edit files from a specific WordPress filesystem directory. Note that filesystem lives in the browser memory.
  • Every code change is saved to a disk directory, say, my-plugin/src.
  • Code changes trigger a bundling process using the browser versions of Rollup and Babel.
  • The resulting bundle consists of AMD (Asynchronous Modules). It was the most convenient format for our purposes here. This means each module declaration is wrapped in define('moduleName', ['dependency1', 'dependency2', ...], function moduleFactory(dep1, dep2) { });.
  • The bundle ships with a custom module loader that understands all the define() and require() calls even for JSON and CSS modules.
  • The resulting JavaScript bundle is:
    • stored in, say, my-plugin/build alongside non-JavaScript changes (like an updated index.php)
    • live eval()'d in the WordPress iframe to trigger live reloading when appropriate

The editor+bundler setup is heavyweight (~3.5MB JavaScript code minified) and thus is shipped as a separate chunk using esbuild's code splitting.

Usage

const { WordPressPluginIDE, createBlockPluginFixture } = await import(
	'../wordpress-plugin-ide/index.js'
);
// createBlockPluginFixture defines files to create on the disk to boostrap
// a create-block plugin
render(
	<WordPressPluginIDE
		plugin={createBlockPluginFixture}  // Create the plugin in the FileSystem and edit its files
		workerThread={workerThread}
		initialEditedFile="edit.js"
		onBundleReady={(bundleContents: string) => {
			if (doneFirstBoot) {
				(wpFrame.contentWindow as any).eval(bundleContents);
			} else {
				doneFirstBoot = true;
				wpFrame.src = workerThread.pathToInternalUrl(
					query.get('url') || '/'
				);
			}
		}}
	/>,
	document.getElementById('code-editor')!
);

Screenshots

CleanShot 2022-11-17 at 00 35 45@2x (1)

@adamziel adamziel self-assigned this Nov 17, 2022
So many console messages! Must. reduce. the. noise.
@adamziel adamziel marked this pull request as ready for review November 22, 2022 19:03
@adamziel adamziel merged commit 3c874b0 into trunk Nov 22, 2022
@adamziel adamziel deleted the add/editable-snippets branch November 22, 2022 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant