Skip to content

Commit

Permalink
Add: Code editor to edit site.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Jan 20, 2022
1 parent 5f055de commit 01248e2
Show file tree
Hide file tree
Showing 23 changed files with 428 additions and 185 deletions.
4 changes: 3 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/base-styles/_z-index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ $z-layers: (
".block-editor-block-list__layout .reusable-block-indicator": 1,
".block-editor-block-list__block-selection-button": 22,
".components-form-toggle__input": 1,
".edit-post-text-editor__toolbar": 1,
".interface-code-editor-screen__toolbar": 1,
".edit-post-sidebar__panel-tab.is-active": 1,

// These next three share a stacking context
Expand Down
65 changes: 27 additions & 38 deletions packages/edit-post/src/components/text-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,43 @@
import {
PostTextEditor,
PostTitle,
TextEditorGlobalKeyboardShortcuts,
store as editorStore,
TextEditorGlobalKeyboardShortcuts,
} from '@wordpress/editor';
import { Button } from '@wordpress/components';
import { withDispatch, withSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { displayShortcut } from '@wordpress/keycodes';
import { compose } from '@wordpress/compose';
import { useDispatch, useSelect } from '@wordpress/data';
import { CodeEditorScreen } from '@wordpress/interface';
import { useCallback } from '@wordpress/element';
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';

/**
* Internal dependencies
*/
import { store as editPostStore } from '../../store';

function TextEditor( { onExit, isRichEditingEnabled } ) {
export default function TextEditor() {
const { isRichEditingEnabled, shortcut } = useSelect( ( select ) => {
const { getEditorSettings } = select( editorStore );
const { getShortcutRepresentation } = select( keyboardShortcutsStore );
return {
isRichEditingEnabled: getEditorSettings().richEditingEnabled,
shortcut: getShortcutRepresentation( 'core/edit-post/toggle-mode' ),
};
}, [] );
const { switchEditorMode } = useDispatch( editPostStore );
const onExit = useCallback( () => switchEditorMode( 'visual' ), [
switchEditorMode,
] );
return (
<div className="edit-post-text-editor">
{ isRichEditingEnabled && (
<div className="edit-post-text-editor__toolbar">
<h2>{ __( 'Editing code' ) }</h2>
<Button
variant="tertiary"
onClick={ onExit }
shortcut={ displayShortcut.secondary( 'm' ) }
>
{ __( 'Exit code editor' ) }
</Button>
<TextEditorGlobalKeyboardShortcuts />
</div>
) }
<div className="edit-post-text-editor__body">
<>
<TextEditorGlobalKeyboardShortcuts />
<CodeEditorScreen
className="edit-post-text-editor"
onExit={ isRichEditingEnabled ? onExit : undefined }
exitShortcut={ shortcut }
>
<PostTitle />
<PostTextEditor />
</div>
</div>
</CodeEditorScreen>
</>
);
}

export default compose(
withSelect( ( select ) => ( {
isRichEditingEnabled: select( editorStore ).getEditorSettings()
.richEditingEnabled,
} ) ),
withDispatch( ( dispatch ) => {
return {
onExit() {
dispatch( editPostStore ).switchEditorMode( 'visual' );
},
};
} )
)( TextEditor );
80 changes: 14 additions & 66 deletions packages/edit-post/src/components/text-editor/style.scss
Original file line number Diff line number Diff line change
@@ -1,75 +1,23 @@
.edit-post-text-editor {
position: relative;
width: 100%;
background-color: $white;
flex-grow: 1;
// Post title.
.edit-post-text-editor .editor-post-title {
max-width: none;
line-height: $default-line-height;

// Post title.
.editor-post-title {
max-width: none;
line-height: $default-line-height;
font-family: $editor-html-font;
font-size: 2.5em;
font-weight: normal;

font-family: $editor-html-font;
font-size: 2.5em;
font-weight: normal;
border: $border-width solid $gray-600;

border: $border-width solid $gray-600;

// Same padding as body.
padding: $grid-unit-20;

@include break-small() {
padding: $grid-unit-30;
}

&:focus {
border-color: var(--wp-admin-theme-color);
box-shadow: 0 0 0 var(--wp-admin-border-width-focus) var(--wp-admin-theme-color);
}
}
}

.edit-post-text-editor__body {
width: 100%;
padding: 0 $grid-unit-15 $grid-unit-15 $grid-unit-15;
max-width: $break-xlarge;
margin-left: auto;
margin-right: auto;

@include break-large() {
padding: $grid-unit-20 $grid-unit-30 #{ $grid-unit-60 * 2 } $grid-unit-30;
padding: 0 $grid-unit-30 $grid-unit-30 $grid-unit-30;
}

}

// Exit code editor toolbar.
.edit-post-text-editor__toolbar {
position: sticky;
z-index: z-index(".edit-post-text-editor__toolbar");
top: 0;
left: 0;
right: 0;
display: flex;
background: rgba($white, 0.8);
padding: $grid-unit-05 $grid-unit-15;
// Same padding as body.
padding: $grid-unit-20;

@include break-small() {
padding: $grid-unit-15;
}

@include break-large() {
padding: $grid-unit-15 $grid-unit-30;
}

h2 {
line-height: $button-size;
margin: 0 auto 0 0;
font-size: $default-font-size;
color: $gray-900;
padding: $grid-unit-30;
}

.components-button svg {
order: 1;
&:focus {
border-color: var(--wp-admin-theme-color);
box-shadow: 0 0 0 var(--wp-admin-border-width-focus) var(--wp-admin-theme-color);
}
}
55 changes: 55 additions & 0 deletions packages/edit-site/src/components/code-editor/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* WordPress dependencies
*/
import {
CodeEditorScreen,
CodeEditor as InterfaceCodeEditor,
} from '@wordpress/interface';
import { parse } from '@wordpress/blocks';
import { useEntityBlockEditor, useEntityProp } from '@wordpress/core-data';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';

/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../store';

export default function CodeEditor() {
const { templateType, shortcut } = useSelect( ( select ) => {
const { getEditedPostType } = select( editSiteStore );
const { getShortcutRepresentation } = select( keyboardShortcutsStore );
return {
templateType: getEditedPostType(),
shortcut: getShortcutRepresentation( 'core/edit-site/toggle-mode' ),
};
}, [] );
const [ contentStructure, setContent ] = useEntityProp(
'postType',
templateType,
'content'
);
const [ blocks, , onChange ] = useEntityBlockEditor(
'postType',
templateType
);
const content =
contentStructure instanceof Function
? contentStructure( { blocks } )
: contentStructure;
const { switchEditorMode } = useDispatch( editSiteStore );
return (
<CodeEditorScreen
onExit={ () => switchEditorMode( 'visual' ) }
exitShortcut={ shortcut }
>
<InterfaceCodeEditor
value={ content }
onChange={ ( newContent ) => {
onChange( parse( newContent ), { selection: undefined } );
} }
onInput={ setContent }
/>
</CodeEditorScreen>
);
}
23 changes: 16 additions & 7 deletions packages/edit-site/src/components/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import Header from '../header';
import { SidebarComplementaryAreaFills } from '../sidebar';
import NavigationSidebar from '../navigation-sidebar';
import BlockEditor from '../block-editor';
import CodeEditor from '../code-editor';
import KeyboardShortcuts from '../keyboard-shortcuts';
import URLQueryController from '../url-query-controller';
import InserterSidebar from '../secondary-sidebar/inserter-sidebar';
Expand Down Expand Up @@ -60,6 +61,7 @@ function Editor( { onError } ) {
isNavigationOpen,
previousShortcut,
nextShortcut,
editorMode,
} = useSelect( ( select ) => {
const {
isInserterOpened,
Expand All @@ -69,6 +71,7 @@ function Editor( { onError } ) {
getEditedPostId,
getPage,
isNavigationOpened,
getEditorMode,
} = select( editSiteStore );
const { hasFinishedResolution, getEntityRecord } = select( coreStore );
const postType = getEditedPostType();
Expand Down Expand Up @@ -102,6 +105,7 @@ function Editor( { onError } ) {
nextShortcut: select(
keyboardShortcutsStore
).getAllShortcutKeyCombinations( 'core/edit-site/next-region' ),
editorMode: getEditorMode(),
};
}, [] );
const { setPage, setIsInserterOpened } = useDispatch( editSiteStore );
Expand Down Expand Up @@ -220,13 +224,18 @@ function Editor( { onError } ) {
content={
<>
<EditorNotices />
{ template && (
<BlockEditor
setIsInserterOpen={
setIsInserterOpened
}
/>
) }
{ editorMode === 'visual' &&
template && (
<BlockEditor
setIsInserterOpen={
setIsInserterOpened
}
/>
) }
{ editorMode === 'text' &&
template && (
<CodeEditor />
) }
{ templateResolved &&
! template &&
settings?.siteUrl &&
Expand Down
67 changes: 67 additions & 0 deletions packages/edit-site/src/components/header/mode-switcher/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { MenuItemsChoice, MenuGroup } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';

/**
* Internal dependencies
*/
/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../../store';

/**
* Set of available mode options.
*
* @type {Array}
*/
const MODES = [
{
value: 'visual',
label: __( 'Visual editor' ),
},
{
value: 'text',
label: __( 'Code editor' ),
},
];

function ModeSwitcher() {
const { shortcut, mode } = useSelect(
( select ) => ( {
shortcut: select(
keyboardShortcutsStore
).getShortcutRepresentation( 'core/edit-site/toggle-mode' ),
isRichEditingEnabled: select( editSiteStore ).getSettings()
.richEditingEnabled,
isCodeEditingEnabled: select( editSiteStore ).getSettings()
.codeEditingEnabled,
mode: select( editSiteStore ).getEditorMode(),
} ),
[]
);
const { switchEditorMode } = useDispatch( editSiteStore );

const choices = MODES.map( ( choice ) => {
if ( choice.value !== mode ) {
return { ...choice, shortcut };
}
return choice;
} );

return (
<MenuGroup label={ __( 'Editor' ) }>
<MenuItemsChoice
choices={ choices }
value={ mode }
onSelect={ switchEditorMode }
/>
</MenuGroup>
);
}

export default ModeSwitcher;
2 changes: 2 additions & 0 deletions packages/edit-site/src/components/header/more-menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import ToolsMoreMenuGroup from '../tools-more-menu-group';
import SiteExport from './site-export';
import WelcomeGuideMenuItem from './welcome-guide-menu-item';
import CopyContentMenuItem from './copy-content-menu-item';
import ModeSwitcher from '../mode-switcher';

const POPOVER_PROPS = {
className: 'edit-site-more-menu__content',
Expand Down Expand Up @@ -76,6 +77,7 @@ export default function MoreMenu() {
'Spotlight mode deactivated'
) }
/>
<ModeSwitcher />
<ActionItem.Slot
name="core/edit-site/plugin-more-menu"
label={ __( 'Plugins' ) }
Expand Down
Loading

0 comments on commit 01248e2

Please sign in to comment.