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

Handle context menu format bold and italic #556

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion src/MarkdownTextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {MarkdownStyle} from './MarkdownTextInputDecoratorViewNativeComponen
import NativeLiveMarkdownModule from './NativeLiveMarkdownModule';
import {mergeMarkdownStyleWithDefault} from './styleUtils';
import type {PartialMarkdownStyle} from './styleUtils';
import type {InlineImagesInputProps, MarkdownRange} from './commonTypes';
import type {FormatType, InlineImagesInputProps, MarkdownRange} from './commonTypes';

declare global {
// eslint-disable-next-line no-var
Expand Down Expand Up @@ -53,6 +53,7 @@ function unregisterParser(parserId: number) {

interface MarkdownTextInputProps extends TextInputProps, InlineImagesInputProps {
markdownStyle?: PartialMarkdownStyle;
formatSelection?: (selectedText: string, formatType: FormatType) => string;
parser: (value: string) => MarkdownRange[];
}

Expand Down
59 changes: 57 additions & 2 deletions src/MarkdownTextInput.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ import type {MarkdownStyle} from './MarkdownTextInputDecoratorViewNativeComponen
import {getElementHeight, getPlaceholderValue, isEventComposing, normalizeValue, parseInnerHTMLToText} from './web/utils/inputUtils';
import {parseToReactDOMStyle, processMarkdownStyle} from './web/utils/webStyleUtils';
import {forceRefreshAllImages} from './web/inputElements/inlineImage';
import type {MarkdownRange, InlineImagesInputProps} from './commonTypes';
import type {MarkdownRange, InlineImagesInputProps, FormatType} from './commonTypes';

const useClientEffect = typeof window === 'undefined' ? useEffect : useLayoutEffect;

interface MarkdownTextInputProps extends TextInputProps, InlineImagesInputProps {
markdownStyle?: MarkdownStyle;
parser: (text: string) => MarkdownRange[];
formatSelection?: (selectedText: string, formatType: FormatType) => string;
tomekzaw marked this conversation as resolved.
Show resolved Hide resolved
onClick?: (e: MouseEvent<HTMLDivElement>) => void;
dir?: string;
disabled?: boolean;
Expand Down Expand Up @@ -85,6 +86,7 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP
multiline = false,
markdownStyle,
parser,
formatSelection,
onBlur,
onChange,
onChangeText,
Expand Down Expand Up @@ -236,6 +238,40 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP
[parser, parseText, processedMarkdownStyle],
);

const handleFormatSelection = useCallback(
(target: MarkdownTextInputElement, parsedText: string, cursorPosition: number, formatCommand: string): ParseTextResult => {
if (!contentSelection.current) {
return {
text: '',
cursorPosition: 0,
};
tomekzaw marked this conversation as resolved.
Show resolved Hide resolved
}
Skalakid marked this conversation as resolved.
Show resolved Hide resolved

let formatType: FormatType;
switch (formatCommand) {
case 'formatBold':
formatType = 'bold';
break;
case 'formatItalic':
formatType = 'italic';
break;
default:
formatType = 'underline';
break;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why underline is set as default?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually asked about this here. Since we only handle the bold, italic, and underline command, I default it to underline. Should I use the "throw" approach?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, I missed that question. Let's use the "throw" approach, we don't want the underline to be a default option

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since we don't do the conversion anymore, no throw is needed here.

Skalakid marked this conversation as resolved.
Show resolved Hide resolved

const beforeSelectedText = parsedText.slice(0, contentSelection.current.start);
Skalakid marked this conversation as resolved.
Show resolved Hide resolved
const selectedText = parsedText.slice(contentSelection.current.start, contentSelection.current.end);
const formattedText = formatSelection?.(selectedText, formatType) ?? selectedText;
Skalakid marked this conversation as resolved.
Show resolved Hide resolved
const formattedTextDiffLength = formattedText.length - selectedText.length;
Skalakid marked this conversation as resolved.
Show resolved Hide resolved
const afterSelectedText = parsedText.slice(contentSelection.current.end);
Skalakid marked this conversation as resolved.
Show resolved Hide resolved
const text = `${beforeSelectedText}${formattedText}${afterSelectedText}`;

return parseText(parser, target, text, processedMarkdownStyle, cursorPosition + formattedTextDiffLength, true);
},
[parser, parseText, formatSelection, processedMarkdownStyle],
);

// Placeholder text color logic
const updateTextColor = useCallback(
(node: HTMLDivElement, text: string) => {
Expand Down Expand Up @@ -361,6 +397,11 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP
case 'historyRedo':
newInputUpdate = redo(divRef.current);
break;
case 'formatBold':
case 'formatItalic':
case 'formatUnderline':
newInputUpdate = handleFormatSelection(divRef.current, parsedText, newCursorPosition, inputType);
break;
default:
newInputUpdate = parseText(parser, divRef.current, parsedText, processedMarkdownStyle, newCursorPosition, true, !inputType, inputType === 'pasteText');
}
Expand Down Expand Up @@ -414,7 +455,21 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP

handleContentSizeChange();
},
[parser, updateTextColor, updateSelection, onChange, onChangeText, handleContentSizeChange, undo, redo, parseText, processedMarkdownStyle, setEventProps, maxLength],
[
parser,
updateTextColor,
updateSelection,
onChange,
onChangeText,
handleContentSizeChange,
undo,
redo,
handleFormatSelection,
parseText,
processedMarkdownStyle,
setEventProps,
maxLength,
],
);

const insertText = useCallback(
Expand Down
4 changes: 3 additions & 1 deletion src/commonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type MarkdownType =
| 'syntax'
| 'inline-image';

type FormatType = 'bold' | 'italic' | 'underline';

interface MarkdownRange {
type: MarkdownType;
start: number;
Expand All @@ -26,4 +28,4 @@ type InlineImagesInputProps = {
imagePreviewAuthRequiredURLs?: string[];
};

export type {MarkdownType, MarkdownRange, InlineImagesInputProps};
export type {MarkdownType, FormatType, MarkdownRange, InlineImagesInputProps};
Loading