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

Custom Editor: Initial Experiment #39373

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ jobs:
- prepare
- run:
name: TypeScript strict typecheck of individual subprojects
command: npm run typecheck -- --project client/landing/gutenboarding
command: |
npm run typecheck -- --project client/landing/custom-editor
&& npm run typecheck -- --project client/landing/gutenboarding

lint-and-translate:
<<: *defaults
Expand Down
18 changes: 18 additions & 0 deletions client/landing/custom-editor/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* External dependencies
*/
import config from '../../config';
import { initialize } from '@wordpress/edit-site';

/**
* Internal dependencies
*/
import './utils';

window.AppBoot = () => {
if ( ! config.isEnabled( 'custom-editor' ) ) {
window.location.href = '/';
} else {
initialize( 'wpcom', {} );
}
};
8 changes: 8 additions & 0 deletions client/landing/custom-editor/section.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const CUSTOM_EDITOR_SECTION_DEFINITION = {
name: 'custom-editor',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case we decide to proceed with this, I think this should be replaced with site-editor or similar to avoid confusion.

paths: [ '/custom-editor' ],
module: 'custom-editor',
secondary: false,
group: 'custom-editor',
enableLoggedOut: true,
};
13 changes: 13 additions & 0 deletions client/landing/custom-editor/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "@automattic/calypso-build/tsconfig",
"compilerOptions": {
// Disallow features that require cross-file information for emit.
// Must be used with babel typescript
"isolatedModules": true,

"baseUrl": ".",
"paths": {
"*": [ "*", "../../*" ]
}
}
}
110 changes: 110 additions & 0 deletions client/landing/custom-editor/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* External dependencies
*/
import apiFetch from '@wordpress/api-fetch';
import wpcomRequest from 'wpcom-proxy-request';

/**
* A lot of the following is copied from D37093-code.
* Maybe we can move this code into Calypso `packages/`, parametrize it (e.g. apiFetch)
* and move into Calypso's `packages/wpcom-proxy-request`, or publish as a new npm for use on WP.com.
*/

// FIXME -- quick'n'dirty setting of a few params
const _currentSiteId = 89417913; // @ockham's test site, replace with your own

const wpApiSettings = {
root: 'https://public-api.wordpress.com/',
};

/**
* Normalizes the path for requests to the public API.
*/
function wpcomFetchNormalizePath( options ) {
if ( options.url && options.url.indexOf( wpApiSettings.root ) !== -1 ) {
options.path = options.url.replace( wpApiSettings.root, '' );
delete options.url;
}

if ( options.path ) {
// Ensures path starts with a slash.
if ( ! options.path.startsWith( '/' ) ) {
options.path = '/' + options.path;
}

// Removes namespace from path.
if ( options.apiNamespace ) {
options.path = options.path.replace( '/' + options.apiNamespace, '' );
}
}

return options;
}

/**
* Creates a fetch-like response.
*/
function wpcomFetchCreateFetchResponse( data, status, headers ) {
var fetchResponse;
var normalizedHeaders = {};
for ( var header in headers ) {
normalizedHeaders[ header.toLowerCase() ] = headers[ header ];
}
if ( Array.isArray( data ) ) {
fetchResponse = data.slice( 0 );
} else {
fetchResponse = Object.assign( {}, data );
}
fetchResponse.status = status;
fetchResponse.json = function() {
return Promise.resolve( data );
};
fetchResponse.headers = {
get: function( name ) {
return normalizedHeaders[ name && name.toLowerCase() ];
},
};
return fetchResponse;
}

/**
* Determines the namespace from the request path.
*/
function wpcomFetchSetNamespace( options, next ) {
if ( options.path && ! options.namespace ) {
var namespace = options.path.match( /^\/([a-z]+\/v?[0-9.]+)\// );
if ( namespace ) {
options.apiNamespace = namespace[ 1 ];
options.path = options.path.replace( '/' + options.apiNamespace, '' );
} else {
// Defaults all requests to the wp/v2 namespace.
options.apiNamespace = 'wp/v2';
}
}
console.log( 'post-norm options', options );
return next( options, next );
}

/**
* Prefixes any non-global request endpoint to the site specific endpoint.
*/
function wpcomFetchAddSitePrefix( options, next ) {
if ( options.path && ! options.global && options.path.indexOf( '/sites/' ) === -1 ) {
options.path = '/sites/' + _currentSiteId + options.path;
}
return next( options, next );
}

// Register middlewares (last-registered runs first).
[
wpcomFetchAddSitePrefix,
wpcomFetchSetNamespace,
function( options, next ) {
// Path needs to be normalized first.
return next( wpcomFetchNormalizePath( options ), next );
},
].forEach( function( middleware ) {
apiFetch.use( middleware );
} );

apiFetch.setFetchHandler( wpcomRequest );
1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@wordpress/data-controls": "1.8.0",
"@wordpress/dom": "2.8.0",
"@wordpress/edit-post": "3.13.1",
"@wordpress/edit-site": "1.3.1",
"@wordpress/editor": "9.12.1",
"@wordpress/element": "2.11.0",
"@wordpress/format-library": "1.14.1",
Expand Down
2 changes: 2 additions & 0 deletions client/server/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import { logSectionResponse } from './analytics';
import analytics from 'server/lib/analytics';
import { getLanguage, filterLanguageRevisions } from 'lib/i18n-utils';
import { isWooOAuth2Client } from 'lib/oauth2-clients';
import { CUSTOM_EDITOR_SECTION_DEFINITION } from 'landing/custom-editor/section';
import { GUTENBOARDING_SECTION_DEFINITION } from 'landing/gutenboarding/section';
import { JETPACK_CLOUD_SECTION_DEFINITION } from 'landing/jetpack-cloud/section';

Expand Down Expand Up @@ -860,6 +861,7 @@ module.exports = function() {
handleSectionPath( LOGIN_SECTION_DEFINITION, '/log-in', 'entry-login' );
loginRouter( serverRouter( app, setUpRoute, null ) );

handleSectionPath( CUSTOM_EDITOR_SECTION_DEFINITION, '/custom-editor', 'entry-custom-editor' );
handleSectionPath( GUTENBOARDING_SECTION_DEFINITION, '/gutenboarding', 'entry-gutenboarding' );

// This is used to log to tracks Content Security Policy violation reports sent by browsers
Expand Down
1 change: 1 addition & 0 deletions client/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ const webpackConfig = {
'entry-jetpack-cloud': [ path.join( __dirname, 'landing', 'jetpack-cloud' ) ],
'entry-login': [ path.join( __dirname, 'landing', 'login' ) ],
'entry-gutenboarding': [ path.join( __dirname, 'landing', 'gutenboarding' ) ],
'entry-custom-editor': [ path.join( __dirname, 'landing', 'custom-editor' ) ],
} ),
mode: isDevelopment ? 'development' : 'production',
devtool: process.env.SOURCEMAP || ( isDevelopment ? '#eval' : false ),
Expand Down
1 change: 1 addition & 0 deletions config/development.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"comments/management/threaded-view": false,
"composite-checkout-wpcom": false,
"coming-soon": true,
"custom-editor": true,
"desktop-promo": true,
"devdocs": true,
"devdocs/redirect-loggedout-homepage": true,
Expand Down
Loading