Skip to content

Feat/mobile mvp #588

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

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions demo/stories/mobile-editor/MobileEditor.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type {StoryObj} from '@storybook/react';

import {MobileEditor as component} from './MobileEditor';

export const Story: StoryObj<typeof component> = {};
Story.storyName = 'Mobile editor';

export default {
title: 'Experiments / Mobile editor',
component,
};
34 changes: 34 additions & 0 deletions demo/stories/mobile-editor/MobileEditor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {memo} from 'react';

import {MarkdownEditorView, useMarkdownEditor} from '../../../src';
import {PlaygroundLayout} from '../../components/PlaygroundLayout';

import {toolbarPreset} from './preset';

export const MobileEditor = memo(() => {
const editor = useMarkdownEditor({
mobile: true,
wysiwygConfig: {
extensionOptions: {
commandMenu: undefined,
},
},
});

return (
<PlaygroundLayout
editor={editor}
view={({className}) => (
<MarkdownEditorView
stickyToolbar
settingsVisible={false}
editor={editor}
className={className}
toolbarsPreset={toolbarPreset}
/>
)}
/>
);
});

MobileEditor.displayName = 'MobileEditor';
46 changes: 46 additions & 0 deletions demo/stories/mobile-editor/preset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {ActionName as Action} from 'src/bundle/config/action-names';
import {ToolbarName as Toolbar} from 'src/modules/toolbars/constants';
import {
boldItemMarkup,
boldItemView,
boldItemWysiwyg,
italicItemMarkup,
italicItemView,
italicItemWysiwyg,
strikethroughItemMarkup,
strikethroughItemView,
strikethroughItemWysiwyg,
underlineItemMarkup,
underlineItemView,
underlineItemWysiwyg,
} from 'src/modules/toolbars/items';
import type {ToolbarsPreset} from 'src/modules/toolbars/types';

export const toolbarPreset: ToolbarsPreset = {
items: {
[Action.bold]: {
view: boldItemView,
wysiwyg: boldItemWysiwyg,
markup: boldItemMarkup,
},
[Action.italic]: {
view: italicItemView,
wysiwyg: italicItemWysiwyg,
markup: italicItemMarkup,
},
[Action.underline]: {
view: underlineItemView,
wysiwyg: underlineItemWysiwyg,
markup: underlineItemMarkup,
},
[Action.strike]: {
view: strikethroughItemView,
wysiwyg: strikethroughItemWysiwyg,
markup: strikethroughItemMarkup,
},
},
orders: {
[Toolbar.wysiwygMain]: [[Action.bold, Action.italic, Action.underline, Action.strike]],
[Toolbar.markupMain]: [[Action.bold, Action.italic, Action.underline, Action.strike]],
},
};
10 changes: 9 additions & 1 deletion src/bundle/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export interface EditorInt
readonly preset: EditorPreset;
readonly mdOptions: Readonly<MarkdownEditorMdOptions>;
readonly directiveSyntax: DirectiveSyntaxContext;
readonly mobile: boolean;

/** @internal used in demo for dev-tools */
readonly _wysiwygView?: PMEditorView;
Expand Down Expand Up @@ -126,7 +127,7 @@ export type ChangeEditorModeOptions = {

export type EditorOptions = Pick<
MarkdownEditorOptions,
'md' | 'initial' | 'handlers' | 'experimental' | 'markupConfig' | 'wysiwygConfig'
'md' | 'initial' | 'handlers' | 'experimental' | 'markupConfig' | 'wysiwygConfig' | 'mobile'
> & {
logger: Logger2.ILogger;
renderStorage: ReactRenderStorage;
Expand Down Expand Up @@ -165,6 +166,7 @@ export class EditorImpl extends SafeEventEmitter<EventMapInt> implements EditorI
#beforeEditorModeChange?: (
options: Pick<ChangeEditorModeOptions, 'mode' | 'reason'>,
) => boolean | undefined;
#mobile: boolean;

get _wysiwygView(): PMEditorView {
// @ts-expect-error internal typing
Expand Down Expand Up @@ -336,6 +338,10 @@ export class EditorImpl extends SafeEventEmitter<EventMapInt> implements EditorI
return this.#enableNewImageSizeCalculation;
}

get mobile(): boolean {
return this.#mobile;
}

constructor(opts: EditorOptions) {
const {logger} = opts;

Expand All @@ -353,6 +359,7 @@ export class EditorImpl extends SafeEventEmitter<EventMapInt> implements EditorI
experimental = {},
markupConfig = {},
wysiwygConfig = {},
mobile = false,
} = opts;

this.#logger = logger;
Expand Down Expand Up @@ -388,6 +395,7 @@ export class EditorImpl extends SafeEventEmitter<EventMapInt> implements EditorI
this.#prepareRawMarkup = experimental.prepareRawMarkup;
this.#escapeConfig = wysiwygConfig.escapeConfig;
this.#beforeEditorModeChange = experimental.beforeEditorModeChange;
this.#mobile = mobile;
}

// ---> implements CodeEditor
Expand Down
1 change: 1 addition & 0 deletions src/bundle/MarkdownEditorView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ const EditorWrapper = forwardRef<HTMLDivElement, EditorWrapperProps>(
splitModeEnabled: editor.splitModeEnabled,
stickyToolbar,
toolbarVisibility: editor.toolbarVisible && !showPreview,
mobile: editor.mobile,
};

const areSettingsVisible =
Expand Down
9 changes: 9 additions & 0 deletions src/bundle/PlatformPopup.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.g-md-platform-popup {
&__sheet {
--g-sheet-content-padding: 0px;

.g-md-settings-content {
width: auto;
}
}
}
43 changes: 43 additions & 0 deletions src/bundle/PlatformPopup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type {FC, ReactNode} from 'react';

import {Popup, type PopupProps, Sheet} from '@gravity-ui/uikit';

import {cn} from '../classname';

export type PlatformPopupProps = Pick<PopupProps, 'placement' | 'open' | 'anchorElement'> & {
children: ReactNode;
mobile?: boolean;
onClose?: () => void;
};

import './PlatformPopup.scss';

const b = cn('platform-popup');

export const PlatformPopup: FC<PlatformPopupProps> = ({
mobile,
children,
onClose,
anchorElement,
placement,
open = false,
}) => {
if (mobile) {
return (
<Sheet visible={open} onClose={onClose} className={b('sheet')}>
{children}
</Sheet>
);
}

return (
<Popup
anchorElement={anchorElement}
open={open}
onOpenChange={onClose}
placement={placement}
>
{children}
</Popup>
);
};
1 change: 1 addition & 0 deletions src/bundle/ToolbarView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export function ToolbarView<T>({
focus={toolbarFocus}
dotsTitle={i18n('more_action')}
onClick={(id, attrs) => editor.emit('toolbar-action', {id, attrs, editorMode})}
mobile={editor.mobile}
/>
{children}
</div>
Expand Down
2 changes: 2 additions & 0 deletions src/bundle/WysiwygEditorView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const WysiwygEditorView = memo<WysiwygEditorViewProps>((props) => {
children,
stickyToolbar = true,
} = props;

useRenderTime((time) => {
globalLogger.metrics({
component: 'wysiwyg-editor',
Expand All @@ -55,6 +56,7 @@ export const WysiwygEditorView = memo<WysiwygEditorViewProps>((props) => {
duration: time,
});
});

return (
<div className={b({toolbar: toolbarVisible}, [className])} data-qa={qa}>
{toolbarVisible ? (
Expand Down
53 changes: 32 additions & 21 deletions src/bundle/settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
HelpMark,
Icon,
Menu,
Popup,
type PopupPlacement,
type QAProps,
} from '@gravity-ui/uikit';
Expand All @@ -22,6 +21,7 @@ import {noop} from '../../lodash';
import {useBooleanState} from '../../react-utils/hooks';
import {ToolbarTooltipDelay} from '../../toolbar';
import {VERSION} from '../../version';
import {PlatformPopup} from '../PlatformPopup';
import type {MarkdownEditorMode, MarkdownEditorSplitMode} from '../types';

import {MarkdownHints} from './MarkdownHints';
Expand All @@ -39,8 +39,14 @@ export type EditorSettingsProps = Omit<SettingsContentProps, 'onClose'> & {
};

export const EditorSettings = memo<EditorSettingsProps>(function EditorSettings(props) {
const {className, onShowPreviewChange, showPreview, renderPreviewButton, settingsVisible} =
props;
const {
className,
onShowPreviewChange,
showPreview,
renderPreviewButton,
settingsVisible,
mobile,
} = props;
const [chevronElement, setChevronElement] = useState<HTMLButtonElement | null>(null);
const [popupShown, , hidePopup, togglePopup] = useBooleanState(false);

Expand Down Expand Up @@ -85,19 +91,20 @@ export const EditorSettings = memo<EditorSettingsProps>(function EditorSettings(
>
<Icon data={Gear} />
</Button>
<Popup
<PlatformPopup
mobile={mobile}
open={popupShown}
onClose={hidePopup}
anchorElement={chevronElement}
placement={placement}
onOpenChange={hidePopup}
>
<SettingsContent
{...props}
qa="g-md-settings-content"
onClose={hidePopup}
className={bSettings('content')}
/>
</Popup>
</PlatformPopup>
</>
)}
</div>
Expand All @@ -119,6 +126,7 @@ type SettingsContentProps = ClassNameProps &
splitMode?: MarkdownEditorSplitMode;
splitModeEnabled?: boolean;
onSplitModeChange?: (splitModeEnabled: boolean) => void;
mobile?: boolean;
};

const mdHelpPlacement: PopupPlacement = ['bottom', 'bottom-end', 'right-start', 'right', 'left'];
Expand All @@ -136,6 +144,7 @@ const SettingsContent: React.FC<SettingsContentProps> = function SettingsContent
showPreview,
settingsVisible,
qa,
mobile,
}) {
const isSettingsArray = Array.isArray(settingsVisible);
const showModeSetting = isSettingsArray ? settingsVisible?.includes('mode') : true;
Expand Down Expand Up @@ -167,23 +176,25 @@ const SettingsContent: React.FC<SettingsContentProps> = function SettingsContent
iconStart={<Icon data={LogoMarkdown} />}
>
{i18n('settings_markup')}
<HelpMark
popoverProps={{
placement: mdHelpPlacement,
modal: false,
}}
className={bContent('mode-help')}
>
<div
onClick={(e) => {
// stop clicks propagation
// because otherwise click in MarkdownHints handled as click on MenuItem
e.stopPropagation();
{!mobile && (
<HelpMark
popoverProps={{
placement: mdHelpPlacement,
modal: false,
}}
className={bContent('mode-help')}
>
<MarkdownHints />
</div>
</HelpMark>
<div
onClick={(e) => {
// stop clicks propagation
// because otherwise click in MarkdownHints handled as click on MenuItem
e.stopPropagation();
}}
>
<MarkdownHints />
</div>
</HelpMark>
)}
</Menu.Item>
</Menu>
)}
Expand Down
2 changes: 2 additions & 0 deletions src/bundle/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,6 @@ export type MarkdownEditorOptions = {
/** Options for wysiwyg mode */
wysiwygConfig?: MarkdownEditorWysiwygConfig;
logger?: Logger2.ILogger;
/** Mobile view */
mobile?: boolean;
};
11 changes: 10 additions & 1 deletion src/bundle/useMarkdownEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {Logger2, globalLogger} from '../logger';
import {DirectiveSyntaxContext} from '../utils/directive';

import {EditorImpl, type EditorInt} from './Editor';
import {wSelectionMenuConfigByPreset} from './config';
import type {
MarkdownEditorInstance,
MarkdownEditorMode,
Expand All @@ -30,6 +31,7 @@ export function useMarkdownEditor(
markupConfig = {},
wysiwygConfig = {},
logger = new Logger2(),
mobile,
} = props;

const preset: MarkdownEditorPreset = props.preset ?? 'full';
Expand All @@ -42,7 +44,13 @@ export function useMarkdownEditor(
const directiveSyntax = new DirectiveSyntaxContext(experimental.directiveSyntax);

const extensions: Extension = (builder) => {
const extensionOptions = wysiwygConfig.extensionOptions;
const extensionOptions = wysiwygConfig.extensionOptions ?? {};

if (mobile) {
extensionOptions.selectionContext = {
config: wSelectionMenuConfigByPreset.zero,
};
}

builder.use(BundlePreset, {
...extensionOptions,
Expand All @@ -65,6 +73,7 @@ export function useMarkdownEditor(
needToSetDimensionsForUploadedImages:
experimental.needToSetDimensionsForUploadedImages,
enableNewImageSizeCalculation: experimental.enableNewImageSizeCalculation,
mobile,
});
{
const extraExtensions = wysiwygConfig.extensions;
Expand Down
Loading
Loading