Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

fix: fixed editor bugs (#4552)

Kilu.He9746852b5f57
diff --git a/frontend/appflowy_tauri/package.json b/frontend/appflowy_tauri/package.json
index 120b822199..87249b6f2c 100644
--- a/frontend/appflowy_tauri/package.json
+++ b/frontend/appflowy_tauri/package.json
@@ -28,7 +28,6 @@
"@mui/x-date-pickers-pro": "^6.18.2",
"@reduxjs/toolkit": "2.0.0",
"@slate-yjs/core": "^1.0.2",
- "@tanstack/react-virtual": "3.0.0-beta.54",
"@tauri-apps/api": "^1.2.0",
"@types/react-swipeable-views": "^0.13.4",
"dayjs": "^1.11.9",
@@ -69,6 +68,7 @@
"react18-input-otp": "^1.1.2",
"redux": "^4.2.1",
"rxjs": "^7.8.0",
+ "sass": "^1.70.0",
"slate": "^0.101.4",
"slate-history": "^0.100.0",
"slate-react": "^0.101.3",
diff --git a/frontend/appflowy_tauri/pnpm-lock.yaml b/frontend/appflowy_tauri/pnpm-lock.yaml
index ba9fb26574..b2b708cb99 100644
--- a/frontend/appflowy_tauri/pnpm-lock.yaml
+++ b/frontend/appflowy_tauri/pnpm-lock.yaml
@@ -31,9 +31,6 @@ dependencies:
'@slate-yjs/core':
specifier: ^1.0.2
- '@tanstack/react-virtual':
- specifier: 3.0.0-beta.54
- version: 3.0.0-beta.54([email protected])
'@tauri-apps/api':
specifier: ^1.2.0
version: 1.3.0
@@ -154,6 +151,9 @@ dependencies:
rxjs:
specifier: ^7.8.0
version: 7.8.1
+ sass:
+ specifier: ^1.70.0
+ version: 1.70.0
slate:
specifier: ^0.101.4
version: 0.101.4
@@ -296,7 +296,7 @@ devDependencies:
version: 9.0.0
vite:
specifier: ^4.0.0
- version: 4.3.5(@types/[email protected])
+ version: 4.3.5(@types/[email protected])([email protected])
vite-plugin-svgr:
specifier: ^3.2.0
version: 3.2.0([email protected])
@@ -2061,19 +2061,6 @@ packages:
svgo: 3.0.2
dev: true
- resolution: {integrity: sha512-D1mDMf4UPbrtHRZZriCly5bXTBMhylslm4dhcHqTtDJ6brQcgGmk8YD9JdWBGWfGSWPKoh2x1H3e7eh+hgPXtQ==}
- peerDependencies:
- react: ^16.8.0 || ^17.0.0 || ^18.0.0
- dependencies:
- '@tanstack/virtual-core': 3.0.0-beta.54
- react: 18.2.0
- dev: false
-
- /@tanstack/[email protected]:
- resolution: {integrity: sha512-jtkwqdP2rY2iCCDVAFuaNBH3fiEi29aTn2RhtIoky8DTTiCdc48plpHHreLwmv1PICJ4AJUUESaq3xa8fZH8+g==}
- dev: false
-
/@tauri-apps/[email protected]:
resolution: {integrity: sha512-AH+3FonkKZNtfRtGrObY38PrzEj4d+1emCbwNGu0V2ENbXjlLHMZQlUh+Bhu/CRmjaIwZMGJ3yFvWaZZgTHoog==}
engines: {node: '>= 14.6.0', npm: '>= 6.6.0', yarn: '>= 1.19.1'}
@@ -2630,7 +2617,7 @@ packages:
'@babel/plugin-transform-react-jsx-source': 7.19.6(@babel/[email protected])
magic-string: 0.27.0
react-refresh: 0.14.0
- vite: 4.3.5(@types/[email protected])
+ vite: 4.3.5(@types/[email protected])([email protected])
transitivePeerDependencies:
- supports-color
dev: true
@@ -2887,7 +2874,6 @@ packages:
resolution: {integrity: sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==}
engines: {node: '>=8'}
- dev: true
resolution: {integrity: sha512-JZOSA7Mo9sNGB8+UjSgzdLtokWAky1zbztM3WRLCbZ70/3cTANmQmOdR7y2g+J0e2WXywy1yS468tY+IruqEww==}
@@ -3020,7 +3006,6 @@ packages:
readdirp: 3.6.0
optionalDependencies:
fsevents: 2.3.2
- dev: true
resolution: {integrity: sha512-eXTggHWSooYhq49F2opQhuHWgzucfF2YgODK4e1566GQs5BIfP30B0oenwBJHfWxAs2fyPB1s7Mg949zLf61Yw==}
@@ -3930,7 +3915,6 @@ packages:
engines: {node: '>= 6'}
dependencies:
is-glob: 4.0.3
- dev: true
resolution: {integrity: sha512-XxwI8EOhVQgWp6iDL+3b0r86f4d6AX6zSU55HfB4ydCEuXLXc5FcYeOu+nnGftS4TEju/11rt4KJPTMgbfmv4A==}
@@ -4144,6 +4128,9 @@ packages:
resolution: {integrity: sha512-pwupu3eWfouuaowscykeckFmVTpqbzW+rXFCX8rQLkZzM9ftBmU/++Ra+o+L27mz03zJTlyV4UUr+fdKNffo4A==}
dev: false
+ resolution: {integrity: sha512-fsXeu4J4i6WNWSikpI88v/PcVflZz+6kMhUfIwc5SY+poQRPnaf5V7qds6SUyUN3cVxEzuCab7QIoLOQ+DQ1wA==}
+
resolution: {integrity: sha512-veYYhQa+D1QBKznvhUHxb8faxlrwUnxseDAbAp457E0wLNio2bOSKnjYDhMj+YiAq61xrMGhQk9iXVk5FzgQMw==}
engines: {node: '>=6'}
@@ -4217,7 +4204,6 @@ packages:
engines: {node: '>=8'}
dependencies:
binary-extensions: 2.2.0
- dev: true
resolution: {integrity: sha512-gDYaKHJmnj4aWxyj6YHyXVpdQawtVLHU5cb+eztPGczf6cjuTdwve5ZIEfgXqH4e57An1D1AKf8CZ3kYrQRqYA==}
@@ -4246,7 +4232,6 @@ packages:
resolution: {integrity: sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==}
engines: {node: '>=0.10.0'}
- dev: true
resolution: {integrity: sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==}
@@ -4261,7 +4246,6 @@ packages:
engines: {node: '>=0.10.0'}
dependencies:
is-extglob: 2.1.1
- dev: true
resolution: {integrity: sha512-UknnZK4RakDmTgz4PI1wIph5yxSs/mvChWs9ifnlXsKuXgWmOkY/hAE0H/k2MIqH0RlRye0i1oC07MCRSD28Mw==}
@@ -6116,7 +6100,6 @@ packages:
engines: {node: '>=8.10.0'}
dependencies:
picomatch: 2.3.1
- dev: true
resolution: {integrity: sha512-NW2r5T6ksUKXCabzhL9z+h206HQw/NJkcLm1GPImRQ8IzfXwRGqjVhKJGauHirT0DAuyy6hjdnMZaRoAcy0Klw==}
@@ -6253,6 +6236,15 @@ packages:
resolution: {integrity: sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==}
dev: true
+ resolution: {integrity: sha512-uUxNQ3zAHeAx5nRFskBnrWzDUJrrvpCPD5FNAoRvTi0WwremlheES3tg+56PaVtCs5QDRX5CBLxxKMDJMEa1WQ==}
+ engines: {node: '>=14.0.0'}
+ hasBin: true
+ dependencies:
+ chokidar: 3.5.3
+ immutable: 4.3.4
+ source-map-js: 1.0.2
+
resolution: {integrity: sha512-xAg7SOnEhrm5zI3puOOKyy1OMcMlIJZYNJY7xLBwSze0UjhPLnWfj2GF2EpT0jmzaJKIWKHLsaSSajf35bcYnA==}
engines: {node: '>=v12.22.7'}
@@ -6378,7 +6370,6 @@ packages:
resolution: {integrity: sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==}
engines: {node: '>=0.10.0'}
- dev: true
resolution: {integrity: sha512-SHSKFHadjVA5oR4PPqhtAVdcBWwRYVd6g6cAXnIbRiIwc2EhPrTuKUBdSLvlEKyIP3GCf89fltvcZiP9MMFA1w==}
@@ -6965,13 +6956,13 @@ packages:
'@rollup/pluginutils': 5.0.2
'@svgr/core': 7.0.0
'@svgr/plugin-jsx': 7.0.0
- vite: 4.3.5(@types/[email protected])
+ vite: 4.3.5(@types/[email protected])([email protected])
transitivePeerDependencies:
- rollup
- supports-color
dev: true
resolution: {integrity: sha512-0gEnL9wiRFxgz40o/i/eTBwm+NEbpUeTWhzKrZDSdKm6nplj+z4lKz8ANDgildxHm47Vg8EUia0aicKbawUVVA==}
engines: {node: ^14.18.0 || >=16.0.0}
hasBin: true
@@ -7000,6 +6991,7 @@ packages:
esbuild: 0.17.19
postcss: 8.4.23
rollup: 3.21.7
+ sass: 1.70.0
optionalDependencies:
fsevents: 2.3.2
dev: true
diff --git a/frontend/appflowy_tauri/src/appflowy_app/application/document/document.service.ts b/frontend/appflowy_tauri/src/appflowy_app/application/document/document.service.ts
index 5a025a2f41..3b85063604 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/application/document/document.service.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/application/document/document.service.ts
@@ -21,8 +21,10 @@ import get from 'lodash-es/get';
import { EditorData, EditorNodeType } from '$app/application/document/document.types';
import { Log } from '$app/utils/log';
import { Op } from 'quill-delta';
-import { Element } from 'slate';
-import { getInlinesWithDelta } from '$app/components/editor/provider/utils/convert';
+import { Element, Text } from 'slate';
+import { generateId, getInlinesWithDelta } from '$app/components/editor/provider/utils/convert';
+import { CustomEditor } from '$app/components/editor/command';
+import { LIST_TYPES } from '$app/components/editor/command/tab';
export function blockPB2Node(block: BlockPB) {
let data = {};
@@ -237,24 +239,38 @@ function flattenBlockJson(block: BlockJSON) {
type: block.type,
data: data,
children: [],
+ blockId: generateId(),
};
+ const isEmbed = CustomEditor.isEmbedNode(slateNode);
- const textNode: Element | null = delta
+ const textNode: {
+ type: EditorNodeType.Text;
+ children: (Text | Element)[];
+ textId: string;
+ } | null = !isEmbed
? {
- type: 'text',
- children: [],
+ type: EditorNodeType.Text,
+ children: [{ text: '' }],
+ textId: generateId(),
}
: null;
- const inlinesNodes = getInlinesWithDelta(delta);
-
- textNode?.children.push(...inlinesNodes);
+ if (delta && textNode) {
+ textNode.children = getInlinesWithDelta(delta);
+ }
- const children = block.children;
+ slateNode.children = block.children.map((child) => traverse(child));
- slateNode.children = children.map((child) => traverse(child));
if (textNode) {
- slateNode.children.unshift(textNode);
+ if (!LIST_TYPES.includes(block.type as EditorNodeType) && slateNode.type !== EditorNodeType.Page) {
+ slateNode.children.unshift(textNode);
+ } else {
+ slateNode.children.unshift({
+ type: EditorNodeType.Paragraph,
+ children: [textNode],
+ blockId: generateId(),
+ });
+ }
}
return slateNode;
diff --git a/frontend/appflowy_tauri/src/appflowy_app/application/document/document.types.ts b/frontend/appflowy_tauri/src/appflowy_app/application/document/document.types.ts
index a60f242381..4497dfff80 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/application/document/document.types.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/application/document/document.types.ts
@@ -34,6 +34,7 @@ export type BlockData = {
};
export interface HeadingNode extends Element {
+ blockId: string;
type: EditorNodeType.HeadingBlock;
data: {
level: number;
@@ -41,6 +42,7 @@ export interface HeadingNode extends Element {
}
export interface GridNode extends Element {
+ blockId: string;
type: EditorNodeType.GridBlock;
data: {
viewId?: string;
@@ -48,6 +50,7 @@ export interface GridNode extends Element {
}
export interface TodoListNode extends Element {
+ blockId: string;
type: EditorNodeType.TodoListBlock;
data: {
checked: boolean;
@@ -55,6 +58,7 @@ export interface TodoListNode extends Element {
}
export interface CodeNode extends Element {
+ blockId: string;
type: EditorNodeType.CodeBlock;
data: {
language: string;
@@ -62,19 +66,23 @@ export interface CodeNode extends Element {
}
export interface QuoteNode extends Element {
+ blockId: string;
type: EditorNodeType.QuoteBlock;
}
export interface NumberedListNode extends Element {
type: EditorNodeType.NumberedListBlock;
+ blockId: string;
}
export interface BulletedListNode extends Element {
type: EditorNodeType.BulletedListBlock;
+ blockId: string;
}
export interface ToggleListNode extends Element {
type: EditorNodeType.ToggleListBlock;
+ blockId: string;
data: {
collapsed: boolean;
} & BlockData;
@@ -82,10 +90,12 @@ export interface ToggleListNode extends Element {
export interface DividerNode extends Element {
type: EditorNodeType.DividerBlock;
+ blockId: string;
}
export interface CalloutNode extends Element {
type: EditorNodeType.CalloutBlock;
+ blockId: string;
data: {
icon: string;
} & BlockData;
@@ -93,6 +103,7 @@ export interface CalloutNode extends Element {
export interface MathEquationNode extends Element {
type: EditorNodeType.EquationBlock;
+ blockId: string;
data: {
formula?: string;
} & BlockData;
@@ -182,6 +193,7 @@ export enum EditorMarkFormat {
Href = 'href',
FontColor = 'font_color',
BgColor = 'bg_color',
+ Align = 'align',
}
export enum MentionType {
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/_shared/emoji_picker/EmojiPicker.hooks.ts b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/emoji_picker/EmojiPicker.hooks.ts
index ed6f07c648..af82c82df5 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/_shared/emoji_picker/EmojiPicker.hooks.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/emoji_picker/EmojiPicker.hooks.ts
@@ -1,10 +1,9 @@
-import React, { useCallback, useEffect, useRef, useState } from 'react';
+import React, { useCallback, useEffect, useState } from 'react';
import emojiData, { EmojiMartData } from '@emoji-mart/data';
import { init, FrequentlyUsed, getEmojiDataFromNative, Store } from 'emoji-mart';
import { PopoverProps } from '@mui/material/Popover';
import { PopoverOrigin } from '@mui/material/Popover/Popover';
-import { useVirtualizer } from '@tanstack/react-virtual';
import chunk from 'lodash-es/chunk';
export const EMOJI_SIZE = 32;
@@ -164,16 +163,3 @@ export function getRowsWithCategories(emojiCategories: EmojiCategory[], rowSize:
});
return rows;
}
-
-export function useVirtualizedCategories({ count }: { count: number }) {
- const ref = useRef<HTMLDivElement>(null);
- const virtualize = useVirtualizer({
- count,
- getScrollElement: () => ref.current,
- estimateSize: () => {
- return EMOJI_SIZE;
- },
- });
-
- return { virtualize, ref };
-}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/_shared/emoji_picker/EmojiPicker.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/emoji_picker/EmojiPicker.tsx
index b52a2eff61..0d7c1858a3 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/_shared/emoji_picker/EmojiPicker.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/emoji_picker/EmojiPicker.tsx
@@ -6,13 +6,14 @@ import EmojiPickerCategories from './EmojiPickerCategories';
interface Props {
onEmojiSelect: (emoji: string) => void;
+ onEscape?: () => void;
}
-function EmojiPicker(props: Props) {
+function EmojiPicker({ onEscape, ...props }: Props) {
const { skin, onSkinChange, emojiCategories, setSearchValue, searchValue, onSelect } = useLoadEmojiData(props);
return (
- <div className={'emoji-picker flex h-[360px] max-h-[70vh] flex-col p-4 pt-2'}>
+ <div tabIndex={0} className={'emoji-picker flex h-[360px] max-h-[70vh] flex-col p-4 pt-2'}>
<EmojiPickerHeader
onEmojiSelect={onSelect}
skin={skin}
@@ -20,7 +21,7 @@ function EmojiPicker(props: Props) {
searchValue={searchValue}
onSearchChange={setSearchValue}
/>
- <EmojiPickerCategories onEmojiSelect={onSelect} emojiCategories={emojiCategories} />
+ <EmojiPickerCategories onEscape={onEscape} onEmojiSelect={onSelect} emojiCategories={emojiCategories} />
</div>
);
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/_shared/emoji_picker/EmojiPickerCategories.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/emoji_picker/EmojiPickerCategories.tsx
index fff09cf7aa..d410b6109e 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/_shared/emoji_picker/EmojiPickerCategories.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/emoji_picker/EmojiPickerCategories.tsx
@@ -1,30 +1,35 @@
-import React, { useCallback, useMemo } from 'react';
+import React, { useCallback, useEffect, useMemo } from 'react';
import {
EMOJI_SIZE,
EmojiCategory,
getRowsWithCategories,
PER_ROW_EMOJI_COUNT,
- useVirtualizedCategories,
} from '$app/components/_shared/emoji_picker/EmojiPicker.hooks';
+import { FixedSizeList } from 'react-window';
import { useTranslation } from 'react-i18next';
-import { IconButton } from '@mui/material';
+import AutoSizer from 'react-virtualized-auto-sizer';
+import { getDistanceEdge, inView } from '$app/components/_shared/keyboard_navigation/utils';
function EmojiPickerCategories({
emojiCategories,
onEmojiSelect,
+ onEscape,
}: {
emojiCategories: EmojiCategory[];
onEmojiSelect: (emoji: string) => void;
+ onEscape?: () => void;
}) {
+ const scrollRef = React.useRef<HTMLDivElement>(null);
const { t } = useTranslation();
+ const [selectCell, setSelectCell] = React.useState({
+ row: 1,
+ column: 0,
+ });
const rows = useMemo(() => {
return getRowsWithCategories(emojiCategories, PER_ROW_EMOJI_COUNT);
}, [emojiCategories]);
- const { ref, virtualize } = useVirtualizedCategories({
- count: rows.length,
- });
- const virtualItems = virtualize.getVirtualItems();
+ const ref = React.useRef<HTMLDivElement>(null);
const getCategoryName = useCallback(
(id: string) => {
@@ -45,67 +50,282 @@ function EmojiPickerCategories({
[t]
);
+ useEffect(() => {
+ scrollRef.current?.scrollTo({
+ top: 0,
+ });
+
+ setSelectCell({
+ row: 1,
+ column: 0,
+ });
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [rows]);
+
+ const renderRow = useCallback(
+ ({ index, style }: { index: number; style: React.CSSProperties }) => {
+ const item = rows[index];
+
+ return (
+ <div style={style} data-index={index}>
+ {item.type === 'category' ? (
+ <div className={'pt-2 text-xs font-medium text-text-caption'}>{getCategoryName(item.id)}</div>
+ ) : null}
+ <div className={'flex'}>
+ {item.emojis?.map((emoji, columnIndex) => {
+ const isSelected = selectCell.row === index && selectCell.column === columnIndex;
+
+ return (
+ <div
+ key={emoji.id}
+ data-key={emoji.id}
+ style={{
+ width: EMOJI_SIZE,
+ height: EMOJI_SIZE,
+ }}
+ onClick={() => {
+ onEmojiSelect(emoji.native);
+ }}
+ className={`flex cursor-pointer items-center justify-center rounded hover:bg-fill-list-active ${
+ isSelected ? 'bg-fill-list-hover' : ''
+ }`}
+ >
+ {emoji.native}
+ </div>
+ );
+ })}
+ </div>
+ </div>
+ );
+ },
+ [getCategoryName, onEmojiSelect, rows, selectCell.column, selectCell.row]
+ );
+
+ const getNewColumnIndex = useCallback(
+ (rowIndex: number, columnIndex: number): number => {
+ const row = rows[rowIndex];
+ const length = row.emojis?.length;
+ let newColumnIndex = columnIndex;
+
+ if (length && length <= columnIndex) {
+ newColumnIndex = length - 1 || 0;
+ }
+
+ return newColumnIndex;
+ },
+ [rows]
+ );
+
+ const findNextRow = useCallback(
+ (rowIndex: number, columnIndex: number): { row: number; column: number } => {
+ const rowLength = rows.length;
+ let nextRowIndex = rowIndex + 1;
+
+ if (nextRowIndex >= rowLength - 1) {
+ nextRowIndex = rowLength - 1;
+ } else if (rows[nextRowIndex].type === 'category') {
+ nextRowIndex = findNextRow(nextRowIndex, columnIndex).row;
+ }
+
+ const newColumnIndex = getNewColumnIndex(nextRowIndex, columnIndex);
+
+ return {
+ row: nextRowIndex,
+ column: newColumnIndex,
+ };
+ },
+ [getNewColumnIndex, rows]
+ );
+
+ const findPrevRow = useCallback(
+ (rowIndex: number, columnIndex: number): { row: number; column: number } => {
+ let prevRowIndex = rowIndex - 1;
+
+ if (prevRowIndex < 1) {
+ prevRowIndex = 1;
+ } else if (rows[prevRowIndex].type === 'category') {
+ prevRowIndex = findPrevRow(prevRowIndex, columnIndex).row;
+ }
+
+ const newColumnIndex = getNewColumnIndex(prevRowIndex, columnIndex);
+
+ return {
+ row: prevRowIndex,
+ column: newColumnIndex,
+ };
+ },
+ [getNewColumnIndex, rows]
+ );
+
+ const findPrevCell = useCallback(
+ (row: number, column: number): { row: number; column: number } => {
+ const prevColumn = column - 1;
+
+ if (prevColumn < 0) {
+ const prevRow = findPrevRow(row, column).row;
+
+ if (prevRow === row) return { row, column };
+ const length = rows[prevRow].emojis?.length || 0;
+
+ return {
+ row: prevRow,
+ column: length > 0 ? length - 1 : 0,
+ };
+ }
+
+ return {
+ row,
+ column: prevColumn,
+ };
+ },
+ [findPrevRow, rows]
+ );
+
+ const findNextCell = useCallback(
+ (row: number, column: number): { row: number; column: number } => {
+ const nextColumn = column + 1;
+
+ const rowLength = rows[row].emojis?.length || 0;
+
+ if (nextColumn >= rowLength) {
+ const nextRow = findNextRow(row, column).row;
+
+ if (nextRow === row) return { row, column };
+ return {
+ row: nextRow,
+ column: 0,
+ };
+ }
+
+ return {
+ row,
+ column: nextColumn,
+ };
+ },
+ [findNextRow, rows]
+ );
+
+ useEffect(() => {
+ if (!selectCell || !scrollRef.current) return;
+ const emojiKey = rows[selectCell.row]?.emojis?.[selectCell.column]?.id;
+ const emojiDom = document.querySelector(`[data-key="${emojiKey}"]`);
+
+ if (emojiDom && !inView(emojiDom as HTMLElement, scrollRef.current as HTMLElement)) {
+ const distance = getDistanceEdge(emojiDom as HTMLElement, scrollRef.current as HTMLElement);
+
+ scrollRef.current?.scrollTo({
+ top: scrollRef.current?.scrollTop + distance,
+ });
+ }
+ }, [selectCell, rows]);
+
+ const handleKeyDown = useCallback(
+ (e: KeyboardEvent) => {
+ e.stopPropagation();
+
+ switch (e.key) {
+ case 'Escape':
+ e.preventDefault();
+ onEscape?.();
+ break;
+ case 'ArrowUp': {
+ e.preventDefault();
+
+ setSelectCell(findPrevRow(selectCell.row, selectCell.column));
+
+ break;
+ }
+
+ case 'ArrowDown': {
+ e.preventDefault();
+
+ setSelectCell(findNextRow(selectCell.row, selectCell.column));
+
+ break;
+ }
+
+ case 'ArrowLeft': {
+ e.preventDefault();
+
+ const prevCell = findPrevCell(selectCell.row, selectCell.column);
+
+ setSelectCell(prevCell);
+ break;
+ }
+
+ case 'ArrowRight': {
+ e.preventDefault();
+
+ const nextCell = findNextCell(selectCell.row, selectCell.column);
+
+ setSelectCell(nextCell);
+ break;
+ }
+
+ case 'Enter': {
+ e.preventDefault();
+ const currentRow = rows[selectCell.row];
+ const emoji = currentRow.emojis?.[selectCell.column];
+
+ if (emoji) {
+ onEmojiSelect(emoji.native);
+ }
+
+ break;
+ }
+
+ default:
+ break;
+ }
+ },
+ [
+ findNextCell,
+ findPrevCell,
+ findPrevRow,
+ findNextRow,
+ onEmojiSelect,
+ onEscape,
+ rows,
+ selectCell.column,
+ selectCell.row,
+ ]
+ );
+
+ useEffect(() => {
+ const focusElement = document.querySelector('.emoji-picker .search-emoji-input') as HTMLInputElement;
+
+ const parentElement = ref.current?.parentElement;
+
+ focusElement?.addEventListener('keydown', handleKeyDown);
+ parentElement?.addEventListener('keydown', handleKeyDown);
+ return () => {
+ focusElement?.removeEventListener('keydown', handleKeyDown);
+ parentElement?.removeEventListener('keydown', handleKeyDown);
+ };
+ }, [handleKeyDown]);
+
return (
<div
ref={ref}
className={`mt-2 w-[${
EMOJI_SIZE * PER_ROW_EMOJI_COUNT
- }px] flex-1 items-center justify-center overflow-y-auto overflow-x-hidden`}
+ }px] flex-1 transform items-center justify-center overflow-y-auto overflow-x-hidden`}
>
- <div
- style={{
- height: virtualize.getTotalSize(),
- position: 'relative',
- }}
- className={'mx-1'}
- >
- {virtualItems.length ? (
- <div
- style={{
- position: 'absolute',
- top: 0,
- left: 0,
- width: '100%',
- transform: `translateY(${virtualItems[0].start || 0}px)`,
- }}
+ <AutoSizer>
+ {({ height, width }: { height: number; width: number }) => (
+ <FixedSizeList
+ overscanCount={10}
+ height={height}
+ width={width}
+ outerRef={scrollRef}
+ itemCount={rows.length}
+ itemSize={EMOJI_SIZE}
+ itemData={rows}
>
- {virtualItems.map(({ index }) => {
- const item = rows[index];
-
- return (
- <div data-index={index} ref={virtualize.measureElement} key={item.id} className={'flex flex-col'}>
- {item.type === 'category' ? (
- <div className={'p-2 text-sm font-medium text-text-caption'}>{getCategoryName(item.id)}</div>
- ) : null}
- <div className={'flex'}>
- {item.emojis?.map((emoji) => {
- return (
- <div
- key={emoji.id}
- style={{
- width: EMOJI_SIZE,
- height: EMOJI_SIZE,
- }}
- className={`flex items-center justify-center`}
- >
- <IconButton
- size={'small'}
- onClick={() => {
- onEmojiSelect(emoji.native);
- }}
- >
- {emoji.native}
- </IconButton>
- </div>
- );
- })}
- </div>
- </div>
- );
- })}
- </div>
- ) : null}
- </div>
+ {renderRow}
+ </FixedSizeList>
+ )}
+ </AutoSizer>
</div>
);
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/_shared/emoji_picker/EmojiPickerHeader.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/emoji_picker/EmojiPickerHeader.tsx
index d4867fb517..177ac2e7a0 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/_shared/emoji_picker/EmojiPickerHeader.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/emoji_picker/EmojiPickerHeader.tsx
@@ -49,7 +49,7 @@ function EmojiPickerHeader({ onEmojiSelect, onSkinSelect, searchValue, onSearchC
const { t } = useTranslation();
return (
- <div className={'h-[45px] px-0.5'}>
+ <div className={'px-0.5 py-2'}>
<div className={'search-input flex items-end'}>
<Box sx={{ display: 'flex', alignItems: 'flex-end', marginRight: 2 }}>
<SearchOutlined sx={{ color: 'action.active', mr: 1, my: 0.5 }} />
@@ -59,7 +59,11 @@ function EmojiPickerHeader({ onEmojiSelect, onSkinSelect, searchValue, onSearchC
onSearchChange(e.target.value);
}}
autoFocus={true}
- label={t('search.label')}
+ autoCorrect={'off'}
+ autoComplete={'off'}
+ spellCheck={false}
+ className={'search-emoji-input'}
+ placeholder={t('search.label')}
variant='standard'
/>
</Box>
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/_shared/error_boundary/withError.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/error_boundary/withError.tsx
new file mode 100644
index 0000000000..9b9ba159fb
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/error_boundary/withError.tsx
@@ -0,0 +1,16 @@
+import { ErrorBoundary } from 'react-error-boundary';
+import { Log } from '$app/utils/log';
+import { Alert } from '@mui/material';
+
+export default function withErrorBoundary<T extends object>(WrappedComponent: React.ComponentType<T>) {
+ return (props: T) => (
+ <ErrorBoundary
+ onError={(e) => {
+ Log.error(e);
+ }}
+ fallback={<Alert color={'error'}>Something went wrong</Alert>}
+ >
+ <WrappedComponent {...props} />
+ </ErrorBoundary>
+ );
+}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/_shared/katex_math/KatexMath.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/katex_math/KatexMath.tsx
index 443a268061..e6c7cac5ed 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/_shared/katex_math/KatexMath.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/katex_math/KatexMath.tsx
@@ -4,7 +4,21 @@ import { BlockMath, InlineMath } from 'react-katex';
import './index.css';
function KatexMath({ latex, isInline = false }: { latex: string; isInline?: boolean }) {
- return isInline ? <InlineMath math={latex} /> : <BlockMath math={latex} />;
+ return isInline ? (
+ <InlineMath math={latex} />
+ ) : (
+ <BlockMath
+ renderError={(error) => {
+ return (
+ <div className='flex h-[51px] items-center justify-center'>
+ {error.name}: {error.message}
+ </div>
+ );
+ }}
+ >
+ {latex}
+ </BlockMath>
+ );
}
export default KatexMath;
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/_shared/keyboard_navigation/KeyboardNavigation.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/keyboard_navigation/KeyboardNavigation.tsx
new file mode 100644
index 0000000000..df6485ab3b
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/keyboard_navigation/KeyboardNavigation.tsx
@@ -0,0 +1,303 @@
+import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
+import { MenuItem, Typography } from '@mui/material';
+import { scrollIntoView } from '$app/components/_shared/keyboard_navigation/utils';
+import { ReactEditor, useSlateStatic } from 'slate-react';
+import { useTranslation } from 'react-i18next';
+
+/**
+ * The option of the keyboard navigation
+ * the options will be flattened
+ * - key: the key of the option
+ * - content: the content of the option
+ * - children: the children of the option
+ */
+export interface KeyboardNavigationOption<T = string> {
+ key: T;
+ content?: React.ReactNode;
+ children?: KeyboardNavigationOption<T>[];
+ disabled?: boolean;
+}
+
+/**
+ * - scrollRef: the scrollable element
+ * - focusRef: the element to focus when the keyboard navigation is disabled
+ * - options: the options to navigate
+ * - onSelected: called when an option is selected(hovered)
+ * - onConfirm: called when an option is confirmed
+ * - onEscape: called when the escape key is pressed
+ * - onPressRight: called when the right arrow is pressed
+ * - onPressLeft: called when the left arrow key is pressed
+ * - disableFocus: disable the focus on the keyboard navigation
+ * - disableSelect: disable selecting an option when the options are initialized
+ * - onKeyDown: called when a key is pressed
+ * - defaultFocusedKey: the default focused key
+ * - onFocus: called when the keyboard navigation is focused
+ * - onBlur: called when the keyboard navigation is blurred
+ */
+export interface KeyboardNavigationProps<T> {
+ scrollRef: React.RefObject<HTMLDivElement>;
+ focusRef?: React.RefObject<HTMLElement>;
+ options: KeyboardNavigationOption<T>[];
+ onSelected?: (optionKey: T) => void;
+ onConfirm?: (optionKey: T) => void;
+ onEscape?: () => void;
+ onPressRight?: (optionKey: T) => void;
+ onPressLeft?: (optionKey: T) => void;
+ disableFocus?: boolean;
+ disableSelect?: boolean;
+ onKeyDown?: (e: KeyboardEvent) => void;
+ defaultFocusedKey?: T;
+ onFocus?: () => void;
+ onBlur?: () => void;
+}
+
+function KeyboardNavigation<T>({
+ defaultFocusedKey,
+ onPressRight,
+ onPressLeft,
+ onEscape,
+ onConfirm,
+ scrollRef,
+ options,
+ onSelected,
+ focusRef,
+ disableFocus = false,
+ onKeyDown: onPropsKeyDown,
+ disableSelect = false,
+ onBlur,
+ onFocus,
+}: KeyboardNavigationProps<T>) {
+ const { t } = useTranslation();
+ const editor = useSlateStatic();
+ const ref = useRef<HTMLDivElement>(null);
+ const mouseY = useRef<number | null>(null);
+ const defaultKeyRef = useRef<T | undefined>(defaultFocusedKey);
+ // flatten the options
+ const flattenOptions = useMemo(() => {
+ return options.flatMap((group) => {
+ if (group.children) {
+ return group.children;
+ }
+
+ return [group];
+ });
+ }, [options]);
+
+ const [focusedKey, setFocusedKey] = useState<T>();
+
+ const firstOptionKey = useMemo(() => {
+ if (disableSelect) return;
+ const firstOption = flattenOptions.find((option) => !option.disabled);
+
+ return firstOption?.key;
+ }, [flattenOptions, disableSelect]);
+
+ // set the default focused key when the options are initialized
+ useEffect(() => {
+ if (defaultKeyRef.current) {
+ setFocusedKey(defaultKeyRef.current);
+ defaultKeyRef.current = undefined;
+ return;
+ }
+
+ setFocusedKey(firstOptionKey);
+ }, [firstOptionKey]);
+
+ // call the onSelected callback when the focused key is changed
+ useEffect(() => {
+ if (focusedKey === undefined) return;
+ onSelected?.(focusedKey);
+
+ const scrollElement = scrollRef.current;
+
+ if (!scrollElement) return;
+
+ const dom = ref.current?.querySelector(`[data-key="${focusedKey}"]`);
+
+ if (!dom) return;
+ // scroll the focused option into view
+ requestAnimationFrame(() => {
+ scrollIntoView(dom as HTMLDivElement, scrollElement);
+ });
+ }, [focusedKey, onSelected, scrollRef]);
+
+ const onKeyDown = useCallback(
+ (e: KeyboardEvent) => {
+ onPropsKeyDown?.(e);
+ e.stopPropagation();
+ const key = e.key;
+
+ if (key === 'Tab') {
+ e.preventDefault();
+ return;
+ }
+
+ if (key === 'Escape') {
+ e.preventDefault();
+ onEscape?.();
+ return;
+ }
+
+ if (focusedKey === undefined) return;
+ const focusedIndex = flattenOptions.findIndex((option) => option?.key === focusedKey);
+ const nextIndex = (focusedIndex + 1) % flattenOptions.length;
+ const prevIndex = (focusedIndex - 1 + flattenOptions.length) % flattenOptions.length;
+
+ switch (key) {
+ // move the focus to the previous option
+ case 'ArrowUp': {
+ e.preventDefault();
+
+ const prevKey = flattenOptions[prevIndex]?.key;
+
+ setFocusedKey(prevKey);
+
+ break;
+ }
+
+ // move the focus to the next option
+ case 'ArrowDown': {
+ e.preventDefault();
+ const nextKey = flattenOptions[nextIndex]?.key;
+
+ setFocusedKey(nextKey);
+ break;
+ }
+
+ case 'ArrowRight':
+ if (onPressRight) {
+ e.preventDefault();
+ onPressRight(focusedKey);
+ }
+
+ break;
+ case 'ArrowLeft':
+ if (onPressLeft) {
+ e.preventDefault();
+ onPressLeft(focusedKey);
+ }
+
+ break;
+ // confirm the focused option
+ case 'Enter': {
+ e.preventDefault();
+ const disabled = flattenOptions[focusedIndex]?.disabled;
+
+ if (!disabled) {
+ onConfirm?.(focusedKey);
+ }
+
+ break;
+ }
+
+ default:
+ break;
+ }
+ },
+ [flattenOptions, focusedKey, onConfirm, onEscape, onPressLeft, onPressRight, onPropsKeyDown]
+ );
+
+ const renderOption = useCallback(
+ (option: KeyboardNavigationOption<T>, index: number) => {
+ const hasChildren = option.children && option.children.length > 0;
+
+ const isFocused = focusedKey === option.key;
+
+ return (
+ <div className={'flex flex-col gap-1'} key={option.key as string}>
+ {hasChildren ? (
+ // render the group name
+ option.content && <div className={'text-text-caption'}>{option.content}</div>
+ ) : (
+ // render the option
+ <MenuItem
+ disabled={option.disabled}
+ data-key={option.key}
+ // prevent the focused option is changed when the mouse is not moved but the mouse is entered when scrolling into view
+ onMouseMove={(e) => {
+ mouseY.current = e.clientY;
+ }}
+ onMouseEnter={(e) => {
+ if (mouseY.current === null || mouseY.current !== e.clientY) {
+ setFocusedKey(option.key);
+ }
+
+ mouseY.current = e.clientY;
+ }}
+ onClick={() => {
+ setFocusedKey(option.key);
+ if (!option.disabled) {
+ onConfirm?.(option.key);
+ }
+ }}
+ selected={isFocused}
+ className={`ml-0 flex w-full items-center justify-start rounded-none px-2 py-1 text-xs ${
+ !isFocused ? 'hover:bg-transparent' : ''
+ }`}
+ >
+ {option.content}
+ </MenuItem>
+ )}
+
+ {option.children?.map((child, childIndex) => {
+ return renderOption(child, index + childIndex);
+ })}
+ </div>
+ );
+ },
+ [focusedKey, onConfirm]
+ );
+
+ useEffect(() => {
+ const element = ref.current;
+
+ if (!disableFocus && element) {
+ element.focus();
+ element.addEventListener('keydown', onKeyDown);
+
+ return () => {
+ element.removeEventListener('keydown', onKeyDown);
+ };
+ } else {
+ let element: HTMLElement | null | undefined = focusRef?.current;
+
+ if (!element) {
+ element = ReactEditor.toDOMNode(editor, editor);
+ }
+
+ element.addEventListener('keydown', onKeyDown);
+ return () => {
+ element?.removeEventListener('keydown', onKeyDown);
+ };
+ }
+ }, [disableFocus, editor, onKeyDown, focusRef]);
+
+ return (
+ <div
+ tabIndex={0}
+ onFocus={(e) => {
+ e.stopPropagation();
+
+ onFocus?.();
+ }}
+ onBlur={(e) => {
+ e.stopPropagation();
+
+ onBlur?.();
+ }}
+ autoFocus={!disableFocus}
+ className={'flex w-full flex-col gap-1 outline-none'}
+ ref={ref}
+ >
+ {options.length > 0 ? (
+ options.map(renderOption)
+ ) : (
+ <Typography variant='body1' className={'p-3 text-text-caption'}>
+ {t('findAndReplace.noResult')}
+ </Typography>
+ )}
+ </div>
+ );
+}
+
+export default KeyboardNavigation;
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/_shared/keyboard_navigation/utils.ts b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/keyboard_navigation/utils.ts
new file mode 100644
index 0000000000..621143869b
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/keyboard_navigation/utils.ts
@@ -0,0 +1,32 @@
+export function inView(dom: HTMLElement, container: HTMLElement) {
+ const domRect = dom.getBoundingClientRect();
+ const containerRect = container.getBoundingClientRect();
+
+ if (!domRect || !containerRect) return true;
+
+ return domRect?.bottom <= containerRect?.bottom && domRect?.top >= containerRect?.top;
+}
+
+export function getDistanceEdge(dom: HTMLElement, container: HTMLElement) {
+ const domRect = dom.getBoundingClientRect();
+ const containerRect = container.getBoundingClientRect();
+
+ if (!domRect || !containerRect) return 0;
+
+ const distanceTop = domRect?.top - containerRect?.top;
+ const distanceBottom = domRect?.bottom - containerRect?.bottom;
+
+ return Math.abs(distanceTop) < Math.abs(distanceBottom) ? distanceTop : distanceBottom;
+}
+
+export function scrollIntoView(dom: HTMLElement, container: HTMLElement) {
+ const isDomInView = inView(dom, container);
+
+ if (isDomInView) return;
+
+ dom.scrollIntoView({
+ block: 'nearest',
+ inline: 'nearest',
+ behavior: 'smooth',
+ });
+}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/_shared/popover/Popover.hooks.ts b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/popover/Popover.hooks.ts
new file mode 100644
index 0000000000..44e2df4099
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/popover/Popover.hooks.ts
@@ -0,0 +1,206 @@
+import { useState, useEffect, useCallback } from 'react';
+import { PopoverOrigin } from '@mui/material/Popover/Popover';
+
+interface PopoverPosition {
+ anchorOrigin: PopoverOrigin;
+ transformOrigin: PopoverOrigin;
+ paperWidth: number;
+ paperHeight: number;
+ isEntered: boolean;
+ anchorPosition?: { left: number; top: number };
+}
+
+interface UsePopoverAutoPositionProps {
+ anchorEl?: HTMLElement | null;
+ anchorPosition?: { left: number; top: number; height: number };
+ initialAnchorOrigin?: PopoverOrigin;
+ initialTransformOrigin?: PopoverOrigin;
+ initialPaperWidth: number;
+ initialPaperHeight: number;
+ marginThreshold?: number;
+ open: boolean;
+ anchorSize?: { width: number; height: number };
+}
+
+const minPaperWidth = 80;
+const minPaperHeight = 120;
+
+function getOffsetLeft(
+ rect: {
+ height: number;
+ width: number;
+ },
+ horizontal: number | 'center' | 'left' | 'right'
+) {
+ let offset = 0;
+
+ if (typeof horizontal === 'number') {
+ offset = horizontal;
+ } else if (horizontal === 'center') {
+ offset = rect.width / 2;
+ } else if (horizontal === 'right') {
+ offset = rect.width;
+ }
+
+ return offset;
+}
+
+function getOffsetTop(
+ rect: {
+ height: number;
+ width: number;
+ },
+ vertical: number | 'center' | 'bottom' | 'top'
+) {
+ let offset = 0;
+
+ if (typeof vertical === 'number') {
+ offset = vertical;
+ } else if (vertical === 'center') {
+ offset = rect.height / 2;
+ } else if (vertical === 'bottom') {
+ offset = rect.height;
+ }
+
+ return offset;
+}
+
+const defaultAnchorOrigin: PopoverOrigin = {
+ vertical: 'top',
+ horizontal: 'left',
+};
+
+const defaultTransformOrigin: PopoverOrigin = {
+ vertical: 'top',
+ horizontal: 'left',
+};
+
+const usePopoverAutoPosition = ({
+ anchorEl,
+ anchorPosition,
+ initialAnchorOrigin = defaultAnchorOrigin,
+ initialTransformOrigin = defaultTransformOrigin,
+ initialPaperWidth,
+ initialPaperHeight,
+ marginThreshold = 16,
+ open,
+}: UsePopoverAutoPositionProps): PopoverPosition => {
+ const [position, setPosition] = useState<PopoverPosition>({
+ anchorOrigin: initialAnchorOrigin,
+ transformOrigin: initialTransformOrigin,
+ paperWidth: initialPaperWidth,
+ paperHeight: initialPaperHeight,
+ anchorPosition,
+ isEntered: false,
+ });
+
+ const getAnchorOffset = useCallback(() => {
+ if (anchorPosition) {
+ return {
+ ...anchorPosition,
+ width: 0,
+ };
+ }
+
+ return anchorEl ? anchorEl.getBoundingClientRect() : undefined;
+ }, [anchorEl, anchorPosition]);
+
+ useEffect(() => {
+ if (!open) {
+ return;
+ }
+
+ const viewportWidth = window.innerWidth;
+ const viewportHeight = window.innerHeight;
+ const anchorRect = getAnchorOffset();
+
+ if (!anchorRect) return;
+ let newPaperWidth = initialPaperWidth;
+ let newPaperHeight = initialPaperHeight;
+ const newAnchorPosition = {
+ top: anchorRect.top,
+ left: anchorRect.left,
+ };
+
+ // calculate new paper width
+ const newLeft = anchorRect.left + getOffsetLeft(anchorRect, initialAnchorOrigin.horizontal);
+ const newTop = anchorRect.top + getOffsetTop(anchorRect, initialAnchorOrigin.vertical);
+
+ let isExceedViewportRight = false;
+ let isExceedViewportBottom = false;
+ let isExceedViewportLeft = false;
+ let isExceedViewportTop = false;
+
+ // Check if exceed viewport right
+ if (newLeft + newPaperWidth > viewportWidth - marginThreshold) {
+ isExceedViewportRight = true;
+ // Check if exceed viewport left
+ if (newLeft - newPaperWidth < marginThreshold) {
+ isExceedViewportLeft = true;
+ newPaperWidth = Math.max(minPaperWidth, Math.min(newPaperWidth, viewportWidth - newLeft - marginThreshold));
+ }
+ }
+
+ // Check if exceed viewport bottom
+ if (newTop + newPaperHeight > viewportHeight - marginThreshold) {
+ isExceedViewportBottom = true;
+ // Check if exceed viewport top
+ if (newTop - newPaperHeight < marginThreshold) {
+ isExceedViewportTop = true;
+ newPaperHeight = Math.max(minPaperHeight, Math.min(newPaperHeight, viewportHeight - newTop - marginThreshold));
+ }
+ }
+
+ const newPosition = {
+ anchorOrigin: { ...initialAnchorOrigin },
+ transformOrigin: { ...initialTransformOrigin },
+ paperWidth: newPaperWidth,
+ paperHeight: newPaperHeight,
+ anchorPosition: newAnchorPosition,
+ };
+
+ // If exceed viewport, adjust anchor origin and transform origin
+ if (!isExceedViewportRight && !isExceedViewportLeft) {
+ if (isExceedViewportBottom && !isExceedViewportTop) {
+ newPosition.anchorOrigin.vertical = 'top';
+ newPosition.transformOrigin.vertical = 'bottom';
+ } else if (!isExceedViewportBottom && isExceedViewportTop) {
+ newPosition.anchorOrigin.vertical = 'bottom';
+ newPosition.transformOrigin.vertical = 'top';
+ }
+ } else if (!isExceedViewportBottom && !isExceedViewportTop) {
+ if (isExceedViewportRight && !isExceedViewportLeft) {
+ newPosition.anchorOrigin.horizontal = 'left';
+ newPosition.transformOrigin.horizontal = 'right';
+ } else if (!isExceedViewportRight && isExceedViewportLeft) {
+ newPosition.anchorOrigin.horizontal = 'right';
+ newPosition.transformOrigin.horizontal = 'left';
+ }
+ }
+
+ // anchorPosition is top-left of the anchor element, so we need to adjust it to avoid overlap with the anchor element
+ if (newPosition.anchorOrigin.vertical === 'bottom' && newPosition.transformOrigin.vertical === 'top') {
+ newPosition.anchorPosition.top += anchorRect.height;
+ }
+
+ if (newPosition.anchorOrigin.vertical === 'top' && newPosition.transformOrigin.vertical === 'bottom') {
+ newPosition.paperHeight = newPaperHeight - anchorRect.height;
+ }
+
+ // Set new position and set isEntered to true
+ setPosition({ ...newPosition, isEntered: true });
+ }, [
+ anchorPosition,
+ open,
+ initialAnchorOrigin,
+ initialTransformOrigin,
+ initialPaperWidth,
+ initialPaperHeight,
+ marginThreshold,
+ getAnchorOffset,
+ ]);
+
+ return position;
+};
+
+export default usePopoverAutoPosition;
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/_shared/popover/utils.ts b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/popover/utils.ts
new file mode 100644
index 0000000000..dccaf2f4d4
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/popover/utils.ts
@@ -0,0 +1,45 @@
+import { PopoverOrigin } from '@mui/material/Popover/Popover';
+
+export function getOffsetTop(rect: DOMRect, vertical: number | 'center' | 'bottom' | 'top') {
+ let offset = 0;
+
+ if (typeof vertical === 'number') {
+ offset = vertical;
+ } else if (vertical === 'center') {
+ offset = rect.height / 2;
+ } else if (vertical === 'bottom') {
+ offset = rect.height;
+ }
+
+ return offset;
+}
+
+export function getOffsetLeft(rect: DOMRect, horizontal: number | 'center' | 'left' | 'right') {
+ let offset = 0;
+
+ if (typeof horizontal === 'number') {
+ offset = horizontal;
+ } else if (horizontal === 'center') {
+ offset = rect.width / 2;
+ } else if (horizontal === 'right') {
+ offset = rect.width;
+ }
+
+ return offset;
+}
+
+export function getAnchorOffset(anchorElement: HTMLElement, anchorOrigin: PopoverOrigin) {
+ const anchorRect = anchorElement.getBoundingClientRect();
+
+ return {
+ top: anchorRect.top + getOffsetTop(anchorRect, anchorOrigin.vertical),
+ left: anchorRect.left + getOffsetLeft(anchorRect, anchorOrigin.horizontal),
+ };
+}
+
+export function getTransformOrigin(elemRect: DOMRect, transformOrigin: PopoverOrigin) {
+ return {
+ vertical: getOffsetTop(elemRect, transformOrigin.vertical),
+ horizontal: getOffsetLeft(elemRect, transformOrigin.horizontal),
+ };
+}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/_shared/view_title/ViewIcon.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/view_title/ViewIcon.tsx
index 147bd1cbb0..706827ba9c 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/_shared/view_title/ViewIcon.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/_shared/view_title/ViewIcon.tsx
@@ -22,7 +22,6 @@ function ViewIcon({ icon, onUpdateIcon }: { icon?: PageIcon; onUpdateIcon: (icon
const onEmojiSelect = useCallback(
(emoji: string) => {
onUpdateIcon(emoji);
- setAnchorPosition(undefined);
},
[onUpdateIcon]
);
@@ -38,13 +37,18 @@ function ViewIcon({ icon, onUpdateIcon }: { icon?: PageIcon; onUpdateIcon: (icon
{open && (
<Popover
open={open}
+ autoFocus={true}
+ disableRestoreFocus={false}
anchorReference='anchorPosition'
anchorPosition={anchorPosition}
- disableAutoFocus
- disableRestoreFocus
onClose={() => setAnchorPosition(undefined)}
>
- <EmojiPicker onEmojiSelect={onEmojiSelect} />
+ <EmojiPicker
+ onEscape={() => {
+ setAnchorPosition(undefined);
+ }}
+ onEmojiSelect={onEmojiSelect}
+ />
</Popover>
)}
</>
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/database/Database.hooks.ts b/frontend/appflowy_tauri/src/appflowy_app/components/database/Database.hooks.ts
index 1e83853530..10db88ba43 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/database/Database.hooks.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/database/Database.hooks.ts
@@ -183,3 +183,11 @@ export const useConnectDatabase = (viewId: string) => {
return database;
};
+
+const DatabaseRenderedContext = createContext<() => void>(() => {
+ // do nothing
+});
+
+export const DatabaseRenderedProvider = DatabaseRenderedContext.Provider;
+
+export const useDatabaseRendered = () => useContext(DatabaseRenderedContext);
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/database/Database.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/database/Database.tsx
index 4e59b16b2f..c9b0440d84 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/database/Database.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/database/Database.tsx
@@ -1,4 +1,4 @@
-import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
+import React, { forwardRef, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useViewId } from '$app/hooks/ViewId.hooks';
import { databaseViewService } from '$app/application/database';
import { DatabaseTabBar } from './components';
@@ -19,8 +19,10 @@ interface Props {
setSelectedViewId?: (viewId: string) => void;
}
-export const Database = ({ selectedViewId, setSelectedViewId }: Props) => {
- const ref = useRef<HTMLDivElement>(null);
+export const Database = forwardRef<HTMLDivElement, Props>(({ selectedViewId, setSelectedViewId }, ref) => {
+ const innerRef = useRef<HTMLDivElement>();
+ const databaseRef = (ref ?? innerRef) as React.MutableRefObject<HTMLDivElement>;
+
const viewId = useViewId();
const { t } = useTranslation();
const [notFound, setNotFound] = useState(false);
@@ -28,26 +30,29 @@ export const Database = ({ selectedViewId, setSelectedViewId }: Props) => {
const [editRecordRowId, setEditRecordRowId] = useState<string | null>(null);
const [openCollections, setOpenCollections] = useState<string[]>([]);
- useEffect(() => {
- const onPageChanged = () => {
- void databaseViewService
- .getDatabaseViews(viewId)
- .then((value) => {
- setChildViewIds(value.map((view) => view.id));
- })
- .catch((err) => {
- if (err.code === ErrorCode.RecordNotFound) {
- setNotFound(true);
- }
- });
- };
-
- onPageChanged();
+ const handleResetDatabaseViews = useCallback(async (viewId: string) => {
+ await databaseViewService
+ .getDatabaseViews(viewId)
+ .then((value) => {
+ setChildViewIds(value.map((view) => view.id));
+ })
+ .catch((err) => {
+ if (err.code === ErrorCode.RecordNotFound) {
+ setNotFound(true);
+ }
+ });
+ }, []);
+ useEffect(() => {
+ void handleResetDatabaseViews(viewId);
const unsubscribePromise = subscribeNotifications(
{
- [FolderNotification.DidUpdateView]: () => {
- onPageChanged();
+ [FolderNotification.DidUpdateChildViews]: (changeset) => {
+ if (changeset.create_child_views.length === 0 && changeset.delete_child_views.length === 0) {
+ return;
+ }
+
+ void handleResetDatabaseViews(viewId);
},
},
{
@@ -56,7 +61,7 @@ export const Database = ({ selectedViewId, setSelectedViewId }: Props) => {
);
return () => void unsubscribePromise.then((unsubscribe) => unsubscribe());
- }, [viewId]);
+ }, [handleResetDatabaseViews, viewId]);
const value = useMemo(() => {
return Math.max(0, childViewIds.indexOf(selectedViewId ?? viewId));
@@ -100,7 +105,7 @@ export const Database = ({ selectedViewId, setSelectedViewId }: Props) => {
}
return (
- <div ref={ref} className='appflowy-database relative flex flex-1 flex-col overflow-y-hidden'>
+ <div ref={databaseRef} className='appflowy-database relative flex flex-1 flex-col overflow-y-hidden'>
<DatabaseTabBar
pageId={viewId}
setSelectedViewId={setSelectedViewId}
@@ -120,7 +125,7 @@ export const Database = ({ selectedViewId, setSelectedViewId }: Props) => {
<DatabaseLoader viewId={id}>
{selectedViewId === id && (
<>
- <Portal container={ref.current}>
+ <Portal container={databaseRef.current}>
<div className={'absolute right-16 top-0 py-1'}>
<DatabaseSettings
onToggleCollection={(forceOpen?: boolean) => onToggleCollection(id, forceOpen)}
@@ -147,4 +152,4 @@ export const Database = ({ selectedViewId, setSelectedViewId }: Props) => {
</SwipeableViews>
</div>
);
-};
+});
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/database/components/tab_bar/ViewActions.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/database/components/tab_bar/ViewActions.tsx
index 0d61e62544..5adbf332ed 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/database/components/tab_bar/ViewActions.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/database/components/tab_bar/ViewActions.tsx
@@ -2,10 +2,12 @@ import React, { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { ReactComponent as DeleteSvg } from '$app/assets/delete.svg';
import { ReactComponent as EditSvg } from '$app/assets/edit.svg';
-import { deleteView, updateView } from '$app/application/database/database_view/database_view_service';
+import { deleteView } from '$app/application/database/database_view/database_view_service';
import { MenuItem, MenuProps, Menu } from '@mui/material';
import RenameDialog from '$app/components/layout/nested_page/RenameDialog';
import { Page } from '$app_reducers/pages/slice';
+import { useAppDispatch } from '$app/stores/store';
+import { updatePageName } from '$app_reducers/pages/async_actions';
enum ViewAction {
Rename,
@@ -15,6 +17,7 @@ enum ViewAction {
function ViewActions({ view, ...props }: { view: Page } & MenuProps) {
const { t } = useTranslation();
const viewId = view.id;
+ const dispatch = useAppDispatch();
const [openRenameDialog, setOpenRenameDialog] = useState(false);
const options = [
{
@@ -56,9 +59,12 @@ function ViewActions({ view, ...props }: { view: Page } & MenuProps) {
onClose={() => setOpenRenameDialog(false)}
onOk={async (val) => {
try {
- await updateView(viewId, {
- name: val,
- });
+ await dispatch(
+ updatePageName({
+ id: viewId,
+ name: val,
+ })
+ );
setOpenRenameDialog(false);
props.onClose?.({}, 'backdropClick');
} catch (e) {
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/database/grid/grid_table/GridTable.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/database/grid/grid_table/GridTable.tsx
index 3dee0941a9..ffaa0f8458 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/database/grid/grid_table/GridTable.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/database/grid/grid_table/GridTable.tsx
@@ -1,6 +1,6 @@
import React, { FC, useCallback, useMemo, useRef } from 'react';
import { RowMeta } from '$app/application/database';
-import { useDatabaseVisibilityFields, useDatabaseVisibilityRows } from '../../Database.hooks';
+import { useDatabaseRendered, useDatabaseVisibilityFields, useDatabaseVisibilityRows } from '../../Database.hooks';
import { fieldsToColumns, GridColumn, RenderRow, RenderRowType, rowMetasToRenderRow } from '../constants';
import { CircularProgress } from '@mui/material';
import { GridChildComponentProps, GridOnScrollProps, VariableSizeGrid as Grid } from 'react-window';
@@ -23,6 +23,7 @@ export const GridTable: FC<GridTableProps> = React.memo(({ onEditRecord }) => {
const ref = useRef<Grid<HTMLDivElement>>(null);
const { columnWidth } = useGridColumn(columns, ref);
const { rowHeight } = useGridRow();
+ const onRendered = useDatabaseRendered();
const getItemKey = useCallback(
({ columnIndex, rowIndex }: { columnIndex: number; rowIndex: number }) => {
@@ -114,7 +115,11 @@ export const GridTable: FC<GridTableProps> = React.memo(({ onEditRecord }) => {
style={{
overscrollBehavior: 'none',
}}
- outerRef={scrollElementRef}
+ className={'grid-scroll-container'}
+ outerRef={(el) => {
+ scrollElementRef.current = el;
+ onRendered();
+ }}
innerRef={containerRef}
>
{Cell}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/document/document_header/DocumentHeader.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/document/document_header/DocumentHeader.tsx
index 611aecc84a..209e5fd694 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/document/document_header/DocumentHeader.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/document/document_header/DocumentHeader.tsx
@@ -9,6 +9,7 @@ interface DocumentHeaderProps {
export function DocumentHeader({ page }: DocumentHeaderProps) {
const pageId = page.id;
+
const onUpdateIcon = useCallback(
async (icon: PageIcon) => {
await updatePageIcon(pageId, icon.value ? icon : undefined);
@@ -18,7 +19,7 @@ export function DocumentHeader({ page }: DocumentHeaderProps) {
if (!page) return null;
return (
- <div className={'document-header px-16 py-4'}>
+ <div className={'document-header px-16 pt-4'}>
<ViewTitle showTitle={false} onUpdateIcon={onUpdateIcon} view={page} />
</div>
);
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/Editor.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/Editor.tsx
index 1d6be5c293..6ba63da503 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/Editor.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/Editor.tsx
@@ -4,6 +4,8 @@ import { EditorProps } from '../../application/document/document.types';
import { Toaster } from 'react-hot-toast';
import { CollaborativeEditor } from '$app/components/editor/components/editor';
import { EditorIdProvider } from '$app/components/editor/Editor.hooks';
+import './editor.scss';
+import withErrorBoundary from '$app/components/_shared/error_boundary/withError';
export function Editor(props: EditorProps) {
return (
@@ -16,4 +18,4 @@ export function Editor(props: EditorProps) {
);
}
-export default memo(Editor);
+export default withErrorBoundary(memo(Editor));
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/command/formula.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/command/formula.ts
index cf8a5c348f..c4c13187b2 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/command/formula.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/command/formula.ts
@@ -2,9 +2,9 @@ import { ReactEditor } from 'slate-react';
import { Editor, Element as SlateElement, NodeEntry, Range, Transforms } from 'slate';
import { EditorInlineNodeType, FormulaNode } from '$app/application/document/document.types';
-export function insertFormula(editor: ReactEditor) {
+export function insertFormula(editor: ReactEditor, formula?: string) {
if (editor.selection) {
- wrapFormula(editor);
+ wrapFormula(editor, formula);
}
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/command/index.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/command/index.ts
index 7e0dd174bf..14fcc8b63f 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/command/index.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/command/index.ts
@@ -1,5 +1,17 @@
import { ReactEditor } from 'slate-react';
-import { Editor, Element, Node, NodeEntry, Point, Range, Transforms, Location } from 'slate';
+import {
+ Editor,
+ Element,
+ Node,
+ NodeEntry,
+ Point,
+ Range,
+ Transforms,
+ Location,
+ Path,
+ EditorBeforeOptions,
+ Text,
+} from 'slate';
import { LIST_TYPES, tabBackward, tabForward } from '$app/components/editor/command/tab';
import { isMarkActive, removeMarks, toggleMark } from '$app/components/editor/command/mark';
import {
@@ -16,11 +28,19 @@ import {
Mention,
TodoListNode,
ToggleListNode,
+ inlineNodeTypes,
+ FormulaNode,
} from '$app/application/document/document.types';
import cloneDeep from 'lodash-es/cloneDeep';
import { generateId } from '$app/components/editor/provider/utils/convert';
import { YjsEditor } from '@slate-yjs/core';
+export const EmbedTypes: string[] = [
+ EditorNodeType.DividerBlock,
+ EditorNodeType.EquationBlock,
+ EditorNodeType.GridBlock,
+];
+
export const CustomEditor = {
getBlock: (editor: ReactEditor, at?: Location): NodeEntry<Element> | undefined => {
return Editor.above(editor, {
@@ -29,12 +49,48 @@ export const CustomEditor = {
});
},
+ isInlineNode: (editor: ReactEditor, point: Point): boolean => {
+ return Boolean(
+ editor.above({
+ at: point,
+ match: (n) => {
+ return !Editor.isEditor(n) && Element.isElement(n) && inlineNodeTypes.includes(n.type as EditorInlineNodeType);
+ },
+ })
+ );
+ },
+
+ beforeIsInlineNode: (editor: ReactEditor, at: Location, opts?: EditorBeforeOptions): boolean => {
+ const beforePoint = Editor.before(editor, at, opts);
+
+ if (!beforePoint) return false;
+ return CustomEditor.isInlineNode(editor, beforePoint);
+ },
+
+ afterIsInlineNode: (editor: ReactEditor, at: Location, opts?: EditorBeforeOptions): boolean => {
+ const afterPoint = Editor.after(editor, at, opts);
+
+ if (!afterPoint) return false;
+ return CustomEditor.isInlineNode(editor, afterPoint);
+ },
+ blockEqual: (editor: ReactEditor, point: Point, anotherPoint: Point) => {
+ const match = CustomEditor.getBlock(editor, point);
+ const anotherMatch = CustomEditor.getBlock(editor, anotherPoint);
+
+ if (!match || !anotherMatch) return false;
+
+ const [node] = match;
+ const [anotherNode] = anotherMatch;
+
+ return node === anotherNode;
+ },
+
/**
* turn the current block to a new block
* 1. clone the current block to a new block
- * 2. remove the current block
- * 3. insert the new block
- * 4. lift the children of the new block if the new block doesn't allow has children
+ * 2. lift the children of the current block if the current block doesn't allow has children
+ * 3. remove the old block
+ * 4. insert the new block
* @param editor
* @param newProperties
*/
@@ -50,20 +106,29 @@ export const CustomEditor = {
const cloneNode = CustomEditor.cloneBlock(editor, node);
- Transforms.removeNodes(editor, {
- at: path,
- });
-
Object.assign(cloneNode, newProperties);
- const [, ...children] = cloneNode.children;
+ const isEmbed = editor.isEmbed(cloneNode);
- Transforms.insertNodes(editor, cloneNode, { at: path });
+ if (isEmbed) {
+ editor.splitNodes({
+ always: true,
+ });
+ cloneNode.children = [];
+
+ Transforms.removeNodes(editor, {
+ at: path,
+ });
+ Transforms.insertNodes(editor, cloneNode, { at: path });
+ return;
+ }
const isListType = LIST_TYPES.includes(cloneNode.type as EditorNodeType);
- // if node doesn't allow has children, the children should be lifted
+ // if node doesn't allow has children, lift the children before insert the new node and remove the old node
if (!isListType) {
+ const [textNode, ...children] = cloneNode.children;
+
const length = children.length;
for (let i = 0; i < length; i++) {
@@ -71,14 +136,17 @@ export const CustomEditor = {
at: [...path, length - i],
});
}
+
+ cloneNode.children = [textNode];
}
- const isSelectable = editor.isSelectable(cloneNode);
+ Transforms.removeNodes(editor, {
+ at: path,
+ });
- if (isSelectable) {
- Transforms.select(editor, selection);
- } else {
- Transforms.select(editor, path);
+ Transforms.insertNodes(editor, cloneNode, { at: path });
+ if (selection) {
+ editor.select(selection);
}
},
tabForward,
@@ -87,6 +155,7 @@ export const CustomEditor = {
removeMarks,
isMarkActive,
isFormulaActive,
+ insertFormula,
updateFormula,
deleteFormula,
toggleFormula: (editor: ReactEditor) => {
@@ -110,14 +179,16 @@ export const CustomEditor = {
toggleAlign(editor: ReactEditor, format: string) {
const matchNodes = Array.from(
Editor.nodes(editor, {
- match: (n) => Element.isElement(n) && n.blockId !== undefined,
+ // Note: we need to select the text node instead of the element node, otherwise the parent node will be selected
+ match: (n) => Element.isElement(n) && n.type === EditorNodeType.Text,
})
);
if (!matchNodes) return;
matchNodes.forEach((match) => {
- const [node] = match as NodeEntry<
+ const [, textPath] = match as NodeEntry<Element>;
+ const [node] = editor.parent(textPath) as NodeEntry<
Element & {
data: {
align?: string;
@@ -148,6 +219,26 @@ export const CustomEditor = {
return (node.data as { align?: string })?.align;
},
+ isInlineActive(editor: ReactEditor) {
+ const [match] = editor.nodes({
+ match: (n) => {
+ return !Editor.isEditor(n) && Element.isElement(n) && inlineNodeTypes.includes(n.type as EditorInlineNodeType);
+ },
+ });
+
+ return !!match;
+ },
+
+ isMentionActive(editor: ReactEditor) {
+ const [match] = editor.nodes({
+ match: (n) => {
+ return !Editor.isEditor(n) && Element.isElement(n) && n.type === EditorInlineNodeType.Mention;
+ },
+ });
+
+ return Boolean(match);
+ },
+
insertMention(editor: ReactEditor, mention: Mention) {
const mentionElement = {
type: EditorInlineNodeType.Mention,
@@ -165,8 +256,10 @@ export const CustomEditor = {
toggleTodo(editor: ReactEditor, node: TodoListNode) {
const checked = node.data.checked;
const path = ReactEditor.findPath(editor, node);
+ const data = node.data || {};
const newProperties = {
data: {
+ ...data,
checked: !checked,
},
} as Partial<Element>;
@@ -177,23 +270,39 @@ export const CustomEditor = {
toggleToggleList(editor: ReactEditor, node: ToggleListNode) {
const collapsed = node.data.collapsed;
const path = ReactEditor.findPath(editor, node);
+ const data = node.data || {};
const newProperties = {
data: {
+ ...data,
collapsed: !collapsed,
},
} as Partial<Element>;
- Transforms.setNodes(editor, newProperties, { at: path });
- editor.select(path);
- editor.collapse({
- edge: 'start',
+ const selectMatch = Editor.above(editor, {
+ match: (n) => Element.isElement(n) && n.blockId !== undefined,
});
+
+ Transforms.setNodes(editor, newProperties, { at: path });
+
+ if (selectMatch) {
+ const [selectNode] = selectMatch;
+ const selectNodePath = ReactEditor.findPath(editor, selectNode);
+
+ if (Path.isAncestor(path, selectNodePath)) {
+ editor.select(path);
+ editor.collapse({
+ edge: 'start',
+ });
+ }
+ }
},
setCalloutIcon(editor: ReactEditor, node: CalloutNode, newIcon: string) {
const path = ReactEditor.findPath(editor, node);
+ const data = node.data || {};
const newProperties = {
data: {
+ ...data,
icon: newIcon,
},
} as Partial<Element>;
@@ -203,8 +312,10 @@ export const CustomEditor = {
setMathEquationBlockFormula(editor: ReactEditor, node: Element, newFormula: string) {
const path = ReactEditor.findPath(editor, node);
+ const data = node.data || {};
const newProperties = {
data: {
+ ...data,
formula: newFormula,
},
} as Partial<Element>;
@@ -214,8 +325,10 @@ export const CustomEditor = {
setGridBlockViewId(editor: ReactEditor, node: Element, newViewId: string) {
const path = ReactEditor.findPath(editor, node);
+ const data = node.data || {};
const newProperties = {
data: {
+ ...data,
viewId: newViewId,
},
} as Partial<Element>;
@@ -227,13 +340,19 @@ export const CustomEditor = {
const cloneNode: Element = {
...cloneDeep(block),
blockId: generateId(),
+ type: block.type === EditorNodeType.Page ? EditorNodeType.Paragraph : block.type,
children: [],
};
+ const isEmbed = editor.isEmbed(cloneNode);
+
+ if (isEmbed) {
+ return cloneNode;
+ }
+
const [firstTextNode, ...children] = block.children as Element[];
- const isSelectable = editor.isSelectable(cloneNode);
const textNode =
- firstTextNode && firstTextNode.type === EditorNodeType.Text && isSelectable
+ firstTextNode && firstTextNode.type === EditorNodeType.Text
? {
textId: generateId(),
type: EditorNodeType.Text,
@@ -259,7 +378,10 @@ export const CustomEditor = {
const path = ReactEditor.findPath(editor, node);
- Transforms.insertNodes(editor, cloneNode, { at: path });
+ const nextPath = Path.next(path);
+
+ Transforms.insertNodes(editor, cloneNode, { at: nextPath });
+ return cloneNode;
},
deleteNode(editor: ReactEditor, node: Node) {
@@ -268,6 +390,9 @@ export const CustomEditor = {
Transforms.removeNodes(editor, {
at: path,
});
+ editor.collapse({
+ edge: 'start',
+ });
},
getBlockType: (editor: ReactEditor) => {
@@ -292,7 +417,7 @@ export const CustomEditor = {
return CustomEditor.getBlockType(editor) === EditorNodeType.CodeBlock;
},
- insertEmptyLineAtEnd: (editor: ReactEditor & YjsEditor) => {
+ insertEmptyLine: (editor: ReactEditor & YjsEditor, path: Path) => {
editor.insertNode(
{
type: EditorNodeType.Paragraph,
@@ -312,13 +437,17 @@ export const CustomEditor = {
},
{
select: true,
- at: [editor.children.length],
+ at: path,
}
);
ReactEditor.focus(editor);
Transforms.move(editor);
},
+ insertEmptyLineAtEnd: (editor: ReactEditor & YjsEditor) => {
+ CustomEditor.insertEmptyLine(editor, [editor.children.length]);
+ },
+
focusAtStartOfBlock(editor: ReactEditor) {
const { selection } = editor;
@@ -342,11 +471,17 @@ export const CustomEditor = {
}
) {
const path = ReactEditor.findPath(editor, node);
+
+ const nodeData = node.data || {};
const newProperties = {
- data,
+ data: {
+ ...nodeData,
+ ...data,
+ },
} as Partial<Element>;
Transforms.setNodes(editor, newProperties, { at: path });
+ editor.select(path);
},
deleteAllText(editor: ReactEditor, node: Element) {
@@ -383,4 +518,20 @@ export const CustomEditor = {
return editor.isEmpty(textNode);
},
+
+ getNodeTextContent(node: Node): string {
+ if (Element.isElement(node) && node.type === EditorInlineNodeType.Formula) {
+ return (node as FormulaNode).data || '';
+ }
+
+ if (Text.isText(node)) {
+ return node.text || '';
+ }
+
+ return node.children.map((n) => CustomEditor.getNodeTextContent(n)).join('');
+ },
+
+ isEmbedNode(node: Element): boolean {
+ return EmbedTypes.includes(node.type);
+ },
};
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/command/mark.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/command/mark.ts
index 2f11c01da9..9da3ca40aa 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/command/mark.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/command/mark.ts
@@ -13,13 +13,18 @@ export function toggleMark(
const isActive = isMarkActive(editor, key);
- if (isActive) {
+ if (isActive || !value) {
Editor.removeMark(editor, key as string);
- } else {
+ } else if (value) {
Editor.addMark(editor, key as string, value);
}
}
+/**
+ * Check if the every text in the selection has the mark.
+ * @param editor
+ * @param format
+ */
export function isMarkActive(editor: ReactEditor, format: EditorMarkFormat) {
const selection = editor.selection;
@@ -27,6 +32,34 @@ export function isMarkActive(editor: ReactEditor, format: EditorMarkFormat) {
const isExpanded = Range.isExpanded(selection);
+ if (isExpanded) {
+ const matches = Array.from(getSelectionNodeEntry(editor) || []);
+
+ return matches.every((match) => {
+ const [node] = match;
+
+ const { text, ...attributes } = node;
+
+ if (!text) {
+ return true;
+ }
+
+ return !!(attributes as Record<string, boolean | string>)[format];
+ });
+ }
+
+ const marks = Editor.marks(editor) as Record<string, string | boolean> | null;
+
+ return marks ? !!marks[format] : false;
+}
+
+function getSelectionNodeEntry(editor: ReactEditor) {
+ const selection = editor.selection;
+
+ if (!selection) return null;
+
+ const isExpanded = Range.isExpanded(selection);
+
if (isExpanded) {
let anchor = Range.start(selection);
const focus = Range.end(selection);
@@ -40,36 +73,52 @@ export function isMarkActive(editor: ReactEditor, format: EditorMarkFormat) {
}
}
- const matches = Array.from(
- Editor.nodes(editor, {
- match: Text.isText,
- at: {
- anchor,
- focus,
- },
- })
- );
+ return Editor.nodes(editor, {
+ match: Text.isText,
+ at: {
+ anchor,
+ focus,
+ },
+ });
+ }
- return matches.every((match) => {
- const [node] = match;
+ return null;
+}
- const { text, ...attributes } = node;
+/**
+ * Get all marks in the current selection.
+ * @param editor
+ */
+export function getAllMarks(editor: ReactEditor) {
+ const selection = editor.selection;
- if (!text) {
- return true;
- }
+ if (!selection) return null;
- return !!attributes[format];
+ const isExpanded = Range.isExpanded(selection);
+
+ if (isExpanded) {
+ const matches = Array.from(getSelectionNodeEntry(editor) || []);
+
+ const marks: Record<string, string | boolean> = {};
+
+ matches.forEach((match) => {
+ const [node] = match;
+
+ Object.entries(node).forEach(([key, value]) => {
+ if (key !== 'text') {
+ marks[key] = value;
+ }
+ });
});
- }
- const marks = Editor.marks(editor) as Record<string, string | boolean> | null;
+ return marks;
+ }
- return marks ? !!marks[format] : false;
+ return Editor.marks(editor) as Record<string, string | boolean> | null;
}
export function removeMarks(editor: ReactEditor) {
- const marks = Editor.marks(editor);
+ const marks = getAllMarks(editor);
if (!marks) return;
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/command/tab.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/command/tab.ts
index 463f46321e..74ebe80be4 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/command/tab.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/command/tab.ts
@@ -12,16 +12,6 @@ export const LIST_TYPES = [
EditorNodeType.Paragraph,
];
-const LIST_ITEM_TYPES = [
- EditorNodeType.NumberedListBlock,
- EditorNodeType.BulletedListBlock,
- EditorNodeType.TodoListBlock,
- EditorNodeType.ToggleListBlock,
- EditorNodeType.QuoteBlock,
- EditorNodeType.Paragraph,
- EditorNodeType.HeadingBlock,
-];
-
/**
* Indent the current list item
* Conditions:
@@ -41,11 +31,6 @@ export function tabForward(editor: ReactEditor) {
const [node, path] = match as NodeEntry<Element>;
- // the node is not a list item
- if (!LIST_ITEM_TYPES.includes(node.type as EditorNodeType)) {
- return;
- }
-
const previousPath = Path.previous(path);
const previous = editor.node(previousPath);
@@ -84,12 +69,6 @@ export function tabBackward(editor: ReactEditor) {
const depth = path.length;
if (node.type === EditorNodeType.Page) return;
- if (node.type !== EditorNodeType.Paragraph) {
- CustomEditor.turnToBlock(editor, {
- type: EditorNodeType.Paragraph,
- });
- return;
- }
if (depth === 1) return;
editor.liftNodes({
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/_shared/PlaceholderContent.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/_shared/PlaceholderContent.tsx
index 9c8e163b45..fd12121a09 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/_shared/PlaceholderContent.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/_shared/PlaceholderContent.tsx
@@ -1,18 +1,20 @@
import React, { CSSProperties, useEffect, useMemo, useState } from 'react';
-import { ReactEditor, useSelected, useSlateStatic } from 'slate-react';
+import { ReactEditor, useSelected, useSlate } from 'slate-react';
import { Editor, Element, Range } from 'slate';
import { EditorNodeType, HeadingNode } from '$app/application/document/document.types';
import { useTranslation } from 'react-i18next';
function PlaceholderContent({ node, ...attributes }: { node: Element; className?: string; style?: CSSProperties }) {
const { t } = useTranslation();
- const editor = useSlateStatic();
+ const editor = useSlate();
const selected = useSelected() && !!editor.selection && Range.isCollapsed(editor.selection);
const [isComposing, setIsComposing] = useState(false);
const block = useMemo(() => {
const path = ReactEditor.findPath(editor, node);
const match = Editor.above(editor, {
- match: (n) => !Editor.isEditor(n) && Element.isElement(n) && n.blockId !== undefined,
+ match: (n) => {
+ return !Editor.isEditor(n) && Element.isElement(n) && n.blockId !== undefined && n.type !== undefined;
+ },
at: path,
});
@@ -22,9 +24,7 @@ function PlaceholderContent({ node, ...attributes }: { node: Element; className?
}, [editor, node]);
const className = useMemo(() => {
- return `pointer-events-none select-none mx-1 absolute left-0.5 min-h-[26px] top-0 whitespace-nowrap text-text-placeholder ${
- attributes.className ?? ''
- }`;
+ return `text-placeholder ${attributes.className ?? ''}`;
}, [attributes.className]);
const unSelectedPlaceholder = useMemo(() => {
@@ -81,6 +81,7 @@ function PlaceholderContent({ node, ...attributes }: { node: Element; className?
case EditorNodeType.GridBlock:
case EditorNodeType.EquationBlock:
case EditorNodeType.CodeBlock:
+ case EditorNodeType.DividerBlock:
return '';
default:
@@ -114,9 +115,12 @@ function PlaceholderContent({ node, ...attributes }: { node: Element; className?
}
return (
- <span contentEditable={false} {...attributes} className={className}>
- {selected ? selectedPlaceholder : unSelectedPlaceholder}
- </span>
+ <span
+ placeholder={selected ? selectedPlaceholder : unSelectedPlaceholder}
+ contentEditable={false}
+ {...attributes}
+ className={className}
+ />
);
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/bulleted_list/BulletedList.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/bulleted_list/BulletedList.tsx
index 247d39c890..41fce1c9dc 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/bulleted_list/BulletedList.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/bulleted_list/BulletedList.tsx
@@ -5,14 +5,9 @@ export const BulletedList = memo(
forwardRef<HTMLDivElement, EditorElementProps<BulletedListNode>>(
({ node: _, children, className, ...attributes }, ref) => {
return (
- <>
- <span contentEditable={false} className={'absolute flex w-6 select-none justify-center pt-[3px] font-medium'}>
- •
- </span>
- <div ref={ref} {...attributes} className={`${className} pl-6`}>
- {children}
- </div>
- </>
+ <div ref={ref} {...attributes} className={`${className}`}>
+ {children}
+ </div>
);
}
)
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/bulleted_list/BulletedListIcon.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/bulleted_list/BulletedListIcon.tsx
new file mode 100644
index 0000000000..c94353d45c
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/bulleted_list/BulletedListIcon.tsx
@@ -0,0 +1,16 @@
+import React from 'react';
+import { BulletedListNode } from '$app/application/document/document.types';
+
+function BulletedListIcon({ block: _, className }: { block: BulletedListNode; className: string }) {
+ return (
+ <span
+ onMouseDown={(e) => {
+ e.preventDefault();
+ }}
+ contentEditable={false}
+ className={`${className} bulleted-icon flex w-[23px] justify-center pr-1 font-medium`}
+ />
+ );
+}
+
+export default BulletedListIcon;
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/callout/Callout.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/callout/Callout.tsx
index 44e1f17bf1..0966edf384 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/callout/Callout.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/callout/Callout.tsx
@@ -6,17 +6,15 @@ export const Callout = memo(
forwardRef<HTMLDivElement, EditorElementProps<CalloutNode>>(({ node, children, ...attributes }, ref) => {
return (
<>
- <div contentEditable={false} className={'absolute w-full select-none px-2 pt-3'}>
+ <div contentEditable={false} className={'absolute w-full select-none px-2 pt-[15px]'}>
<CalloutIcon node={node} />
</div>
- <div
- {...attributes}
- ref={ref}
- className={`${
- attributes.className ?? ''
- } my-2 flex w-full flex-col rounded border border-solid border-line-divider bg-content-blue-50 py-2 pl-10`}
- >
- {children}
+ <div {...attributes} ref={ref} className={`${attributes.className ?? ''} w-full bg-bg-body py-2`}>
+ <div
+ className={`flex w-full flex-col rounded border border-solid border-line-divider bg-content-blue-50 py-2 pl-10`}
+ >
+ {children}
+ </div>
</div>
</>
);
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/callout/CalloutIcon.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/callout/CalloutIcon.tsx
index 5a57e57888..e9bba448a7 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/callout/CalloutIcon.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/callout/CalloutIcon.tsx
@@ -4,19 +4,30 @@ import { CalloutNode } from '$app/application/document/document.types';
import EmojiPicker from '$app/components/_shared/emoji_picker/EmojiPicker';
import Popover from '@mui/material/Popover';
import { PopoverCommonProps } from '$app/components/editor/components/tools/popover';
-import { useSlateStatic } from 'slate-react';
+import { ReactEditor, useSlateStatic } from 'slate-react';
import { CustomEditor } from '$app/components/editor/command';
function CalloutIcon({ node }: { node: CalloutNode }) {
const ref = useRef<HTMLButtonElement>(null);
const [open, setOpen] = useState(false);
-
const editor = useSlateStatic();
+
+ const handleClose = useCallback(() => {
+ setOpen(false);
+ const path = ReactEditor.findPath(editor, node);
+
+ ReactEditor.focus(editor);
+ editor.select(path);
+ editor.collapse({
+ edge: 'start',
+ });
+ }, [editor, node]);
const handleEmojiSelect = useCallback(
(emoji: string) => {
CustomEditor.setCalloutIcon(editor, node, emoji);
+ handleClose();
},
- [editor, node]
+ [editor, node, handleClose]
);
return (
@@ -38,11 +49,9 @@ function CalloutIcon({ node }: { node: CalloutNode }) {
anchorEl={ref.current}
disableAutoFocus={true}
open={open}
- onClose={() => {
- setOpen(false);
- }}
+ onClose={handleClose}
anchorOrigin={{
- vertical: 'top',
+ vertical: 'bottom',
horizontal: 'right',
}}
transformOrigin={{
@@ -50,7 +59,7 @@ function CalloutIcon({ node }: { node: CalloutNode }) {
horizontal: 'left',
}}
>
- <EmojiPicker onEmojiSelect={handleEmojiSelect} />
+ <EmojiPicker onEscape={handleClose} onEmojiSelect={handleEmojiSelect} />
</Popover>
)}
</>
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/code/Code.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/code/Code.tsx
index 0b408bdc08..7fe7b205f4 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/code/Code.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/code/Code.tsx
@@ -1,26 +1,35 @@
-import { forwardRef, memo } from 'react';
+import { forwardRef, memo, useCallback } from 'react';
import { EditorElementProps, CodeNode } from '$app/application/document/document.types';
import LanguageSelect from './SelectLanguage';
import { useCodeBlock } from '$app/components/editor/components/blocks/code/Code.hooks';
+import { ReactEditor, useSlateStatic } from 'slate-react';
export const Code = memo(
forwardRef<HTMLDivElement, EditorElementProps<CodeNode>>(({ node, children, ...attributes }, ref) => {
const { language, handleChangeLanguage } = useCodeBlock(node);
+ const editor = useSlateStatic();
+ const onBlur = useCallback(() => {
+ const path = ReactEditor.findPath(editor, node);
+
+ ReactEditor.focus(editor);
+ editor.select(path);
+ editor.collapse({
+ edge: 'start',
+ });
+ }, [editor, node]);
+
return (
<>
- <div contentEditable={false} className={'absolute w-full select-none px-7 py-6'}>
- <LanguageSelect language={language} onChangeLanguage={handleChangeLanguage} />
+ <div contentEditable={false} className={'absolute mt-2 flex h-20 w-full select-none items-center px-6'}>
+ <LanguageSelect onBlur={onBlur} language={language} onChangeLanguage={handleChangeLanguage} />
</div>
- <div
- {...attributes}
- ref={ref}
- className={`${
- attributes.className ?? ''
- } my-2 flex w-full rounded border border-solid border-line-divider bg-content-blue-50 p-6 pt-14`}
- >
- <pre>
+ <div {...attributes} ref={ref} className={`${attributes.className ?? ''} flex w-full bg-bg-body py-2`}>
+ <pre
+ spellCheck={false}
+ className={`flex w-full rounded border border-solid border-line-divider bg-content-blue-50 p-5 pt-20`}
+ >
<code>{children}</code>
</pre>
</div>
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/code/SelectLanguage.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/code/SelectLanguage.tsx
index 501304d287..0fc8601f73 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/code/SelectLanguage.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/code/SelectLanguage.tsx
@@ -1,36 +1,161 @@
-import React from 'react';
-import MenuItem from '@mui/material/MenuItem';
-import FormControl from '@mui/material/FormControl';
-import Select, { SelectChangeEvent } from '@mui/material/Select';
+import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
+import { TextField, Popover } from '@mui/material';
import { useTranslation } from 'react-i18next';
import { supportLanguage } from './constants';
+import KeyboardNavigation, {
+ KeyboardNavigationOption,
+} from '$app/components/_shared/keyboard_navigation/KeyboardNavigation';
+import usePopoverAutoPosition from '$app/components/_shared/popover/Popover.hooks';
+import { PopoverOrigin } from '@mui/material/Popover/Popover';
+
+const initialOrigin: {
+ transformOrigin: PopoverOrigin;
+ anchorOrigin: PopoverOrigin;
+} = {
+ transformOrigin: {
+ vertical: 'top',
+ horizontal: 'left',
+ },
+ anchorOrigin: {
+ vertical: 'bottom',
+ horizontal: 'left',
+ },
+};
function SelectLanguage({
- language,
+ language = 'json',
onChangeLanguage,
+ onBlur,
}: {
language: string;
onChangeLanguage: (language: string) => void;
+ onBlur?: () => void;
}) {
const { t } = useTranslation();
+ const ref = useRef<HTMLDivElement>(null);
+ const [open, setOpen] = useState(false);
+ const [search, setSearch] = useState('');
+
+ const searchRef = useRef<HTMLDivElement>(null);
+ const scrollRef = useRef<HTMLDivElement>(null);
+ const options: KeyboardNavigationOption[] = useMemo(() => {
+ return supportLanguage
+ .map((item) => ({
+ key: item.id,
+ content: item.title,
+ }))
+ .filter((item) => {
+ return item.content?.toLowerCase().includes(search.toLowerCase());
+ });
+ }, [search]);
+
+ const handleClose = useCallback(() => {
+ setOpen(false);
+ setSearch('');
+ }, []);
+
+ const handleConfirm = useCallback(
+ (key: string) => {
+ onChangeLanguage(key);
+ handleClose();
+ },
+ [onChangeLanguage, handleClose]
+ );
+
+ useEffect(() => {
+ const element = ref.current;
+
+ if (!element) return;
+ const handleKeyDown = (e: KeyboardEvent) => {
+ e.stopPropagation();
+ e.preventDefault();
+
+ if (e.key === 'Enter') {
+ setOpen(true);
+ return;
+ }
+
+ onBlur?.();
+ };
+
+ element.addEventListener('keydown', handleKeyDown);
+
+ return () => {
+ element.removeEventListener('keydown', handleKeyDown);
+ };
+ }, [onBlur]);
+
+ const { paperHeight, transformOrigin, anchorOrigin, isEntered } = usePopoverAutoPosition({
+ initialPaperWidth: 200,
+ initialPaperHeight: 220,
+ anchorEl: ref.current,
+ initialAnchorOrigin: initialOrigin.anchorOrigin,
+ initialTransformOrigin: initialOrigin.transformOrigin,
+ open,
+ });
return (
- <FormControl variant='standard'>
- <Select
+ <>
+ <TextField
+ ref={ref}
size={'small'}
- className={'h-[28px] w-[150px]'}
- value={language || 'javascript'}
- onChange={(event: SelectChangeEvent) => onChangeLanguage(event.target.value)}
+ variant={'standard'}
+ className={'w-[150px]'}
+ value={language}
+ onClick={() => {
+ setOpen(true);
+ }}
+ InputProps={{
+ readOnly: true,
+ }}
placeholder={t('document.codeBlock.language.placeholder')}
label={t('document.codeBlock.language.label')}
- >
- {supportLanguage.map((item) => (
- <MenuItem key={item.id} value={item.id}>
- {item.title}
- </MenuItem>
- ))}
- </Select>
- </FormControl>
+ />
+
+ {open && (
+ <Popover
+ disableAutoFocus={true}
+ anchorOrigin={anchorOrigin}
+ transformOrigin={transformOrigin}
+ anchorEl={ref.current}
+ keepMounted={false}
+ open={open && isEntered}
+ onClose={handleClose}
+ >
+ <div
+ style={{
+ height: paperHeight,
+ }}
+ className={'flex max-h-[220px] w-[200px] flex-col overflow-hidden py-2'}
+ >
+ <TextField
+ ref={searchRef}
+ value={search}
+ autoComplete={'off'}
+ spellCheck={false}
+ autoCorrect={'off'}
+ onChange={(e) => setSearch(e.target.value)}
+ size={'small'}
+ autoFocus={true}
+ variant={'standard'}
+ className={'px-2 text-xs'}
+ placeholder={t('search.label')}
+ />
+ <div ref={scrollRef} className={'flex-1 overflow-y-auto overflow-x-hidden'}>
+ <KeyboardNavigation
+ disableFocus={true}
+ focusRef={searchRef}
+ onConfirm={handleConfirm}
+ defaultFocusedKey={language}
+ scrollRef={scrollRef}
+ options={options}
+ onEscape={handleClose}
+ />
+ </div>
+ </div>
+ </Popover>
+ )}
+ </>
);
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/code/constants.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/code/constants.ts
index d2f4ba0033..dee71624db 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/code/constants.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/code/constants.ts
@@ -1,4 +1,68 @@
export const supportLanguage = [
+ {
+ id: 'bash',
+ title: 'Bash',
+ },
+ {
+ id: 'basic',
+ title: 'Basic',
+ },
+ {
+ id: 'c',
+ title: 'C',
+ },
+ {
+ id: 'clojure',
+ title: 'Clojure',
+ },
+ {
+ id: 'cpp',
+ title: 'C++',
+ },
+ {
+ id: 'cs',
+ title: 'CS',
+ },
+ {
+ id: 'css',
+ title: 'CSS',
+ },
+ {
+ id: 'dart',
+ title: 'Dart',
+ },
+ {
+ id: 'elixir',
+ title: 'Elixir',
+ },
+ {
+ id: 'elm',
+ title: 'Elm',
+ },
+ {
+ id: 'erlang',
+ title: 'Erlang',
+ },
+ {
+ id: 'fortran',
+ title: 'Fortran',
+ },
+ {
+ id: 'go',
+ title: 'Go',
+ },
+ {
+ id: 'graphql',
+ title: 'GraphQL',
+ },
+ {
+ id: 'haskell',
+ title: 'Haskell',
+ },
+ {
+ id: 'java',
+ title: 'Java',
+ },
{
id: 'javascript',
title: 'JavaScript',
@@ -8,12 +72,83 @@ export const supportLanguage = [
title: 'JSON',
},
{
- id: 'typescript',
- title: 'TypeScript',
+ id: 'kotlin',
+ title: 'Kotlin',
+ },
+ {
+ id: 'lisp',
+ title: 'Lisp',
+ },
+ {
+ id: 'lua',
+ title: 'Lua',
+ },
+ {
+ id: 'markdown',
+ title: 'Markdown',
+ },
+ {
+ id: 'matlab',
+ title: 'Matlab',
+ },
+ {
+ id: 'ocaml',
+ title: 'OCaml',
+ },
+ {
+ id: 'perl',
+ title: 'Perl',
+ },
+ {
+ id: 'php',
+ title: 'PHP',
+ },
+ {
+ id: 'powershell',
+ title: 'Powershell',
+ },
+ {
+ id: 'python',
+ title: 'Python',
+ },
+ {
+ id: 'r',
+ title: 'R',
+ },
+ {
+ id: 'ruby',
+ title: 'Ruby',
},
-
{
id: 'rust',
title: 'Rust',
},
+ {
+ id: 'scala',
+ title: 'Scala',
+ },
+ {
+ id: 'shell',
+ title: 'Shell',
+ },
+ {
+ id: 'sql',
+ title: 'SQL',
+ },
+ {
+ id: 'swift',
+ title: 'Swift',
+ },
+ {
+ id: 'typescript',
+ title: 'TypeScript',
+ },
+ {
+ id: 'xml',
+ title: 'XML',
+ },
+ {
+ id: 'yaml',
+ title: 'YAML',
+ },
];
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/code/utils.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/code/utils.ts
index 563115c88a..52eeebc8c4 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/code/utils.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/code/utils.ts
@@ -1,9 +1,43 @@
import Prism from 'prismjs';
+import 'prismjs/components/prism-bash';
+import 'prismjs/components/prism-basic';
+import 'prismjs/components/prism-c';
+import 'prismjs/components/prism-clojure';
+import 'prismjs/components/prism-cpp';
+import 'prismjs/components/prism-csp';
+import 'prismjs/components/prism-css';
+import 'prismjs/components/prism-dart';
+import 'prismjs/components/prism-elixir';
+import 'prismjs/components/prism-elm';
+import 'prismjs/components/prism-erlang';
+import 'prismjs/components/prism-fortran';
+import 'prismjs/components/prism-go';
+import 'prismjs/components/prism-graphql';
+import 'prismjs/components/prism-haskell';
+import 'prismjs/components/prism-java';
import 'prismjs/components/prism-javascript';
import 'prismjs/components/prism-json';
-import 'prismjs/components/prism-typescript';
+import 'prismjs/components/prism-kotlin';
+import 'prismjs/components/prism-lisp';
+import 'prismjs/components/prism-lua';
+import 'prismjs/components/prism-markdown';
+import 'prismjs/components/prism-matlab';
+import 'prismjs/components/prism-ocaml';
+import 'prismjs/components/prism-perl';
+import 'prismjs/components/prism-php';
+import 'prismjs/components/prism-powershell';
+import 'prismjs/components/prism-python';
+import 'prismjs/components/prism-r';
+import 'prismjs/components/prism-ruby';
import 'prismjs/components/prism-rust';
+import 'prismjs/components/prism-scala';
+import 'prismjs/components/prism-shell-session';
+import 'prismjs/components/prism-sql';
+import 'prismjs/components/prism-swift';
+import 'prismjs/components/prism-typescript';
+import 'prismjs/components/prism-xml-doc';
+import 'prismjs/components/prism-yaml';
import { BaseRange, NodeEntry, Text, Path } from 'slate';
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/database/DatabaseEmpty.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/database/DatabaseEmpty.tsx
index 172e3784ad..41d6165958 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/database/DatabaseEmpty.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/database/DatabaseEmpty.tsx
@@ -1,4 +1,4 @@
-import React, { useRef } from 'react';
+import React, { useCallback, useRef } from 'react';
import CreateNewFolderIcon from '@mui/icons-material/CreateNewFolder';
import { GridNode } from '$app/application/document/document.types';
@@ -12,21 +12,12 @@ function DatabaseEmpty({ node }: { node: GridNode }) {
const [open, setOpen] = React.useState(false);
- const toggleDrawer = (open: boolean) => (event: React.KeyboardEvent | React.MouseEvent) => {
- if (
- event &&
- event.type === 'keydown' &&
- ((event as React.KeyboardEvent).key === 'Tab' || (event as React.KeyboardEvent).key === 'Shift')
- ) {
- return;
- }
-
- if (event?.type === 'click') {
- event.stopPropagation();
- }
-
- setOpen(open);
- };
+ const toggleDrawer = useCallback((open: boolean) => {
+ return (e: React.MouseEvent | KeyboardEvent) => {
+ e.stopPropagation();
+ setOpen(open);
+ };
+ }, []);
return (
<div
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/database/DatabaseList.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/database/DatabaseList.tsx
index d248fc851e..5a342a08aa 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/database/DatabaseList.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/database/DatabaseList.tsx
@@ -1,23 +1,56 @@
-import React, { useCallback, useEffect } from 'react';
+import React, { useCallback, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
-import { List, MenuItem, TextField } from '@mui/material';
+import { TextField } from '@mui/material';
import { useLoadDatabaseList } from '$app/components/editor/components/blocks/database/DatabaseList.hooks';
import { ViewLayoutPB } from '@/services/backend';
import { ReactComponent as GridSvg } from '$app/assets/grid.svg';
import { useSlateStatic } from 'slate-react';
import { CustomEditor } from '$app/components/editor/command';
import { GridNode } from '$app/application/document/document.types';
+import KeyboardNavigation, {
+ KeyboardNavigationOption,
+} from '$app/components/_shared/keyboard_navigation/KeyboardNavigation';
+import { Page } from '$app_reducers/pages/slice';
-function DatabaseList({ node }: { node: GridNode }) {
+function DatabaseList({
+ node,
+ toggleDrawer,
+}: {
+ node: GridNode;
+ toggleDrawer: (open: boolean) => (e: React.MouseEvent | KeyboardEvent) => void;
+}) {
+ const scrollRef = React.useRef<HTMLDivElement>(null);
+
+ const inputRef = React.useRef<HTMLElement>(null);
const editor = useSlateStatic();
const { t } = useTranslation();
const [searchText, setSearchText] = React.useState<string>('');
- const [hovered, setHovered] = React.useState<string | null>(null);
const { list } = useLoadDatabaseList({
searchText: searchText || '',
layout: ViewLayoutPB.Grid,
});
+ const renderItem = useCallback(
+ (item: Page) => {
+ return (
+ <div className={'flex items-center text-text-title'}>
+ <GridSvg className={'mr-2 h-4 w-4'} />
+ <div className={'truncate'}>{item.name || t('document.title.placeholder')}</div>
+ </div>
+ );
+ },
+ [t]
+ );
+
+ const options: KeyboardNavigationOption[] = useMemo(() => {
+ return list.map((item) => {
+ return {
+ key: item.id,
+ content: renderItem(item),
+ };
+ });
+ }, [list, renderItem]);
+
const handleSelected = useCallback(
(id: string) => {
CustomEditor.setGridBlockViewId(editor, node, id);
@@ -25,53 +58,23 @@ function DatabaseList({ node }: { node: GridNode }) {
[editor, node]
);
- useEffect(() => {
- if (list.length > 0) {
- setHovered(list[0].id);
- }
- }, [list]);
-
const handleKeyDown = useCallback(
- (e: React.KeyboardEvent<HTMLDivElement>) => {
- const index = list.findIndex((item) => item.id === hovered);
- const prevIndex = index - 1;
- const nextIndex = index + 1;
-
- switch (e.key) {
- case 'ArrowDown':
- e.stopPropagation();
- e.preventDefault();
- if (nextIndex < list.length) {
- setHovered(list[nextIndex].id);
- }
-
- break;
- case 'ArrowUp':
- e.stopPropagation();
- e.preventDefault();
- if (prevIndex >= 0) {
- setHovered(list[prevIndex].id);
- }
-
- break;
- case 'Enter':
- e.stopPropagation();
- if (hovered) {
- handleSelected(hovered);
- }
-
- break;
+ (e: KeyboardEvent) => {
+ if (e.key === 'Escape') {
+ e.stopPropagation();
+ e.preventDefault();
+ toggleDrawer(false)(e);
}
},
- [handleSelected, hovered, list]
+ [toggleDrawer]
);
return (
- <div className={'relative overflow-y-auto overflow-x-hidden p-2'}>
+ <div className={'relative flex flex-col gap-1.5 p-2'}>
<TextField
- onKeyDown={handleKeyDown}
variant={'standard'}
autoFocus={true}
+ inputRef={inputRef}
className={'sticky top-0 z-10 px-2'}
value={searchText}
onChange={(e) => {
@@ -82,28 +85,16 @@ function DatabaseList({ node }: { node: GridNode }) {
}}
placeholder={t('document.plugins.database.linkToDatabase')}
/>
- <List>
- {list.map((item) => {
- return (
- <MenuItem
- onMouseEnter={() => {
- setHovered(item.id);
- }}
- selected={hovered === item.id}
- key={item.id}
- className={'flex items-center justify-between'}
- onClick={() => {
- handleSelected(item.id);
- }}
- >
- <div className={'flex items-center text-text-title'}>
- <GridSvg className={'mr-2 h-4 w-4'} />
- <div className={'truncate'}>{item.name || t('document.title.placeholder')}</div>
- </div>
- </MenuItem>
- );
- })}
- </List>
+ <div ref={scrollRef} className={'flex-1 overflow-y-auto overflow-x-hidden'}>
+ <KeyboardNavigation
+ disableFocus={true}
+ onKeyDown={handleKeyDown}
+ focusRef={inputRef}
+ scrollRef={scrollRef}
+ options={options}
+ onConfirm={handleSelected}
+ />
+ </div>
</div>
);
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/database/Drawer.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/database/Drawer.tsx
index e5377b9830..d5767bb79e 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/database/Drawer.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/database/Drawer.tsx
@@ -16,7 +16,7 @@ function Drawer({
node,
}: {
open: boolean;
- toggleDrawer: (open: boolean) => (event: React.KeyboardEvent | React.MouseEvent) => void;
+ toggleDrawer: (open: boolean) => (e: React.MouseEvent | KeyboardEvent) => void;
node: GridNode;
}) {
const editor = useSlateStatic();
@@ -46,7 +46,7 @@ function Drawer({
<CloseSvg />
</IconButton>
</div>
- <div className={'flex-1'}>{open && <DatabaseList node={node} />}</div>
+ <div className={'flex-1'}>{open && <DatabaseList toggleDrawer={toggleDrawer} node={node} />}</div>
<div
onClick={handleCreateGrid}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/database/GridBlock.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/database/GridBlock.tsx
index a215e59130..10ea5a605a 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/database/GridBlock.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/database/GridBlock.tsx
@@ -1,30 +1,25 @@
-import React, { forwardRef, memo, useCallback, useContext } from 'react';
+import React, { forwardRef, memo } from 'react';
import { EditorElementProps, GridNode } from '$app/application/document/document.types';
import GridView from '$app/components/editor/components/blocks/database/GridView';
import DatabaseEmpty from '$app/components/editor/components/blocks/database/DatabaseEmpty';
-import { EditorSelectedBlockContext } from '$app/components/editor/components/editor/Editor.hooks';
+import { useSelected } from 'slate-react';
export const GridBlock = memo(
forwardRef<HTMLDivElement, EditorElementProps<GridNode>>(({ node, children, className = '', ...attributes }, ref) => {
const viewId = node.data.viewId;
-
- const blockId = node.blockId;
- const selectedBlockContext = useContext(EditorSelectedBlockContext);
- const onClick = useCallback(() => {
- if (!blockId) return;
- selectedBlockContext.clear();
- selectedBlockContext.add(blockId);
- }, [blockId, selectedBlockContext]);
+ const selected = useSelected();
return (
- <div {...attributes} onClick={onClick} className={`${className} relative my-2 w-full`}>
+ <div {...attributes} className={`${className} grid-block relative w-full bg-bg-body py-2`}>
<div ref={ref} className={'absolute left-0 top-0 h-full w-full caret-transparent'}>
{children}
</div>
<div
contentEditable={false}
- className='flex h-[400px] w-full overflow-hidden border-b border-t border-line-divider bg-bg-body py-3 caret-text-title'
+ className={`flex h-[400px] w-full overflow-hidden border-b border-t ${
+ selected ? 'border-fill-list-hover' : 'border-line-divider'
+ } py-3 caret-text-title`}
>
{viewId ? <GridView viewId={viewId} /> : <DatabaseEmpty node={node} />}
</div>
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/database/GridView.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/database/GridView.tsx
index 5b2303c126..9873beaeae 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/database/GridView.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/database/GridView.tsx
@@ -1,13 +1,64 @@
-import React, { useState } from 'react';
-import { Database } from '$app/components/database';
+import React, { useCallback, useEffect, useRef, useState } from 'react';
+import { Database, DatabaseRenderedProvider } from '$app/components/database';
import { ViewIdProvider } from '$app/hooks';
function GridView({ viewId }: { viewId: string }) {
const [selectedViewId, onChangeSelectedViewId] = useState(viewId);
+ const ref = useRef<HTMLDivElement>(null);
+
+ const [rendered, setRendered] = useState(false);
+
+ // delegate wheel event to layout when grid is scrolled to top or bottom
+ useEffect(() => {
+ const element = ref.current;
+
+ if (!element) {
+ return;
+ }
+
+ const gridScroller = element.querySelector('.grid-scroll-container') as HTMLDivElement;
+
+ const scrollLayout = gridScroller?.closest('.appflowy-layout') as HTMLDivElement;
+
+ if (!gridScroller || !scrollLayout) {
+ return;
+ }
+
+ const onWheel = (event: WheelEvent) => {
+ const deltaY = event.deltaY;
+ const deltaX = event.deltaX;
+
+ if (deltaX > 10) {
+ return;
+ }
+
+ const { scrollTop, scrollHeight, clientHeight } = gridScroller;
+
+ const atTop = deltaY < 0 && scrollTop === 0;
+ const atBottom = deltaY > 0 && scrollTop + clientHeight >= scrollHeight;
+
+ // if at top or bottom, prevent default to allow layout to scroll
+ if (atTop || atBottom) {
+ scrollLayout.scrollTop += deltaY;
+ }
+ };
+
+ gridScroller.addEventListener('wheel', onWheel, { passive: false });
+ return () => {
+ gridScroller.removeEventListener('wheel', onWheel);
+ };
+ }, [rendered]);
+
+ const onRendered = useCallback(() => {
+ setRendered(true);
+ }, []);
+
return (
<ViewIdProvider value={viewId}>
- <Database selectedViewId={selectedViewId} setSelectedViewId={onChangeSelectedViewId} />
+ <DatabaseRenderedProvider value={onRendered}>
+ <Database ref={ref} selectedViewId={selectedViewId} setSelectedViewId={onChangeSelectedViewId} />
+ </DatabaseRenderedProvider>
</ViewIdProvider>
);
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/divider/DividerNode.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/divider/DividerNode.tsx
index 8ef09a5b83..763a0983fa 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/divider/DividerNode.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/divider/DividerNode.tsx
@@ -1,12 +1,21 @@
-import React, { forwardRef, memo } from 'react';
+import React, { forwardRef, memo, useMemo } from 'react';
import { EditorElementProps, DividerNode as DividerNodeType } from '$app/application/document/document.types';
+import { useSelected } from 'slate-react';
export const DividerNode = memo(
forwardRef<HTMLDivElement, EditorElementProps<DividerNodeType>>(
- ({ node: _node, children: children, className, ...attributes }, ref) => {
+ ({ node: _node, children: children, ...attributes }, ref) => {
+ const selected = useSelected();
+
+ const className = useMemo(() => {
+ return `${attributes.className ?? ''} divider-node relative w-full rounded ${
+ selected ? 'bg-content-blue-100' : ''
+ }`;
+ }, [attributes.className, selected]);
+
return (
- <div {...attributes} className={`${className} relative w-full`}>
- <div contentEditable={false} className={'w-full py-3 text-line-divider'}>
+ <div {...attributes} className={className}>
+ <div contentEditable={false} className={'w-full py-2 text-line-divider'}>
<hr />
</div>
<div ref={ref} className={`absolute h-full w-full caret-transparent`}>
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/math_equation/EditPopover.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/math_equation/EditPopover.tsx
index e4f6a7ef95..933766f214 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/math_equation/EditPopover.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/math_equation/EditPopover.tsx
@@ -1,13 +1,29 @@
-import React, { useCallback, useState } from 'react';
+import React, { useCallback, useEffect, useState } from 'react';
import Popover from '@mui/material/Popover';
import { PopoverCommonProps } from '$app/components/editor/components/tools/popover';
import { TextareaAutosize } from '@mui/material';
import Button from '@mui/material/Button';
import { useTranslation } from 'react-i18next';
import { CustomEditor } from '$app/components/editor/command';
-import { Element } from 'slate';
-import { KeyboardReturnOutlined } from '@mui/icons-material';
import { ReactEditor, useSlateStatic } from 'slate-react';
+import { MathEquationNode } from '$app/application/document/document.types';
+import katex from 'katex';
+import { PopoverOrigin } from '@mui/material/Popover/Popover';
+import usePopoverAutoPosition from '$app/components/_shared/popover/Popover.hooks';
+
+const initialOrigin: {
+ transformOrigin: PopoverOrigin;
+ anchorOrigin: PopoverOrigin;
+} = {
+ transformOrigin: {
+ vertical: 'top',
+ horizontal: 'center',
+ },
+ anchorOrigin: {
+ vertical: 'bottom',
+ horizontal: 'center',
+ },
+};
function EditPopover({
open,
@@ -16,14 +32,18 @@ function EditPopover({
node,
}: {
open: boolean;
- node: Element | null;
+ node: MathEquationNode;
anchorEl: HTMLDivElement | null;
onClose: () => void;
}) {
const editor = useSlateStatic();
+ const [error, setError] = useState<{
+ name: string;
+ message: string;
+ } | null>(null);
const { t } = useTranslation();
- const [value, setValue] = useState<string>('');
+ const [value, setValue] = useState<string>(node.data.formula || '');
const onInput = (event: React.FormEvent<HTMLTextAreaElement>) => {
setValue(event.currentTarget.value);
};
@@ -39,11 +59,15 @@ function EditPopover({
const handleDone = () => {
if (!node) return;
- CustomEditor.setMathEquationBlockFormula(editor, node, value);
+ if (value !== node.data.formula) {
+ CustomEditor.setMathEquationBlockFormula(editor, node, value);
+ }
+
handleClose();
};
- const handleEnter = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
+ const onKeyDown = (e: React.KeyboardEvent) => {
+ e.stopPropagation();
const shift = e.shiftKey;
// If shift is pressed, allow the user to enter a new line, otherwise close the popover
@@ -52,40 +76,87 @@ function EditPopover({
e.stopPropagation();
handleDone();
}
+
+ if (e.key === 'Escape') {
+ e.preventDefault();
+ e.stopPropagation();
+ handleClose();
+ }
};
+ useEffect(() => {
+ try {
+ katex.render(value, document.createElement('div'));
+ setError(null);
+ } catch (e) {
+ setError(
+ e as {
+ name: string;
+ message: string;
+ }
+ );
+ }
+ }, [value]);
+
+ const { transformOrigin, anchorOrigin, isEntered } = usePopoverAutoPosition({
+ initialPaperWidth: 300,
+ initialPaperHeight: 200,
+ anchorEl,
+ initialAnchorOrigin: initialOrigin.anchorOrigin,
+ initialTransformOrigin: initialOrigin.transformOrigin,
+ open,
+ });
+
return (
<Popover
{...PopoverCommonProps}
- open={open}
+ open={open && isEntered}
anchorEl={anchorEl}
- transformOrigin={{
- vertical: 'top',
- horizontal: 'center',
- }}
- anchorOrigin={{
- vertical: 'bottom',
- horizontal: 'center',
- }}
+ transformOrigin={transformOrigin}
+ anchorOrigin={anchorOrigin}
onClose={handleClose}
+ onMouseDown={(e) => {
+ e.stopPropagation();
+ }}
+ onKeyDown={onKeyDown}
>
<div className={'flex flex-col gap-3 p-4'}>
<TextareaAutosize
className='w-full resize-none whitespace-break-spaces break-all rounded border p-2 text-sm'
autoFocus
autoCorrect='off'
+ autoComplete={'off'}
+ spellCheck={false}
value={value}
minRows={3}
onInput={onInput}
- onKeyDown={handleEnter}
+ onKeyDown={onKeyDown}
placeholder={`|x| = \\begin{cases}
x, &\\quad x \\geq 0 \\\\
-x, &\\quad x < 0
\\end{cases}`}
/>
- <Button endIcon={<KeyboardReturnOutlined />} variant={'outlined'} onClick={handleDone}>
- {t('button.done')}
- </Button>
+
+ {error && (
+ <div className={'max-w-[270px] text-sm text-red-500'}>
+ {error.name}: {error.message}
+ </div>
+ )}
+
+ <div className={'flex justify-between gap-2'}>
+ <Button
+ size={'small'}
+ color={'inherit'}
+ variant={'outlined'}
+ onClick={handleClose}
+ className={'flex-grow text-text-caption'}
+ >
+ {t('button.cancel')}
+ </Button>
+ <Button disabled={!!error} size={'small'} variant={'contained'} onClick={handleDone} className={'flex-grow'}>
+ {t('button.done')}
+ </Button>
+ </div>
</div>
</Popover>
);
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/math_equation/MathEquation.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/math_equation/MathEquation.tsx
index db13ed49ed..71b5d0f706 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/math_equation/MathEquation.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/math_equation/MathEquation.tsx
@@ -1,30 +1,59 @@
-import { forwardRef, memo, useState } from 'react';
+import { forwardRef, memo, useEffect, useRef, useState } from 'react';
import { EditorElementProps, MathEquationNode } from '$app/application/document/document.types';
import KatexMath from '$app/components/_shared/katex_math/KatexMath';
import { useTranslation } from 'react-i18next';
import { FunctionsOutlined } from '@mui/icons-material';
import EditPopover from '$app/components/editor/components/blocks/math_equation/EditPopover';
+import { ReactEditor, useSelected, useSlateStatic } from 'slate-react';
export const MathEquation = memo(
forwardRef<HTMLDivElement, EditorElementProps<MathEquationNode>>(
({ node, children, className, ...attributes }, ref) => {
const formula = node.data.formula;
const { t } = useTranslation();
- const [anchorEl, setAnchorEl] = useState<HTMLDivElement | null>(null);
- const open = Boolean(anchorEl);
+ const containerRef = useRef<HTMLDivElement>(null);
+ const [open, setOpen] = useState(false);
+
+ const selected = useSelected();
+
+ const editor = useSlateStatic();
+
+ useEffect(() => {
+ const slateDom = ReactEditor.toDOMNode(editor, editor);
+
+ if (!slateDom) return;
+ const handleKeyDown = (e: KeyboardEvent) => {
+ if (e.key === 'Enter') {
+ e.preventDefault();
+ e.stopPropagation();
+ setOpen(true);
+ }
+ };
+
+ if (selected) {
+ slateDom.addEventListener('keydown', handleKeyDown);
+ }
+
+ return () => {
+ slateDom.removeEventListener('keydown', handleKeyDown);
+ };
+ }, [editor, selected]);
return (
<>
<div
{...attributes}
- onClick={(e) => {
- setAnchorEl(e.currentTarget);
+ ref={containerRef}
+ onClick={() => {
+ setOpen(true);
}}
- className={`${className} relative my-2 w-full cursor-pointer`}
+ className={`${className} relative w-full cursor-pointer py-2`}
>
<div
contentEditable={false}
- className={`w-full select-none rounded border border-line-divider bg-content-blue-50 px-3`}
+ className={`w-full select-none rounded border border-line-divider ${
+ selected ? 'border-fill-hover' : ''
+ } bg-content-blue-50 px-3`}
>
{formula ? (
<KatexMath latex={formula} />
@@ -42,11 +71,11 @@ export const MathEquation = memo(
{open && (
<EditPopover
onClose={() => {
- setAnchorEl(null);
+ setOpen(false);
}}
node={node}
open={open}
- anchorEl={anchorEl}
+ anchorEl={containerRef.current}
/>
)}
</>
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/numbered_list/NumberListIcon.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/numbered_list/NumberListIcon.tsx
new file mode 100644
index 0000000000..9891365b36
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/numbered_list/NumberListIcon.tsx
@@ -0,0 +1,44 @@
+import React, { useMemo } from 'react';
+import { ReactEditor, useSlate } from 'slate-react';
+import { Element, Path } from 'slate';
+import { NumberedListNode } from '$app/application/document/document.types';
+
+function NumberListIcon({ block, className }: { block: NumberedListNode; className: string }) {
+ const editor = useSlate();
+
+ const path = ReactEditor.findPath(editor, block);
+ const index = useMemo(() => {
+ let index = 1;
+
+ let prevPath = Path.previous(path);
+
+ while (prevPath) {
+ const prev = editor.node(prevPath);
+
+ const prevNode = prev[0] as Element;
+
+ if (prevNode.type === block.type) {
+ index += 1;
+ } else {
+ break;
+ }
+
+ prevPath = Path.previous(prevPath);
+ }
+
+ return index;
+ }, [editor, block, path]);
+
+ return (
+ <span
+ onMouseDown={(e) => {
+ e.preventDefault();
+ }}
+ contentEditable={false}
+ data-number={index}
+ className={`${className} numbered-icon flex w-[23px] justify-center pr-1 font-medium`}
+ />
+ );
+}
+
+export default NumberListIcon;
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/numbered_list/NumberedList.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/numbered_list/NumberedList.tsx
index a48b1ef1a0..f3e34e1571 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/numbered_list/NumberedList.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/numbered_list/NumberedList.tsx
@@ -1,46 +1,13 @@
-import React, { forwardRef, memo, useMemo } from 'react';
+import React, { forwardRef, memo } from 'react';
import { EditorElementProps, NumberedListNode } from '$app/application/document/document.types';
-import { ReactEditor, useSlate } from 'slate-react';
-import { Element, Path } from 'slate';
-
export const NumberedList = memo(
forwardRef<HTMLDivElement, EditorElementProps<NumberedListNode>>(
- ({ node, children, className, ...attributes }, ref) => {
- const editor = useSlate();
-
- const path = ReactEditor.findPath(editor, node);
- const index = useMemo(() => {
- let index = 1;
-
- let prevPath = Path.previous(path);
-
- while (prevPath) {
- const prev = editor.node(prevPath);
-
- const prevNode = prev[0] as Element;
-
- if (prevNode.type === node.type) {
- index += 1;
- } else {
- break;
- }
-
- prevPath = Path.previous(prevPath);
- }
-
- return index;
- }, [editor, node, path]);
-
+ ({ node: _, children, className, ...attributes }, ref) => {
return (
- <>
- <span contentEditable={false} className={'absolute flex w-6 select-none justify-center pt-[3px] font-medium'}>
- {index}.
- </span>
- <div ref={ref} {...attributes} className={`${className} pl-6`}>
- {children}
- </div>
- </>
+ <div ref={ref} {...attributes} className={`${className}`}>
+ {children}
+ </div>
);
}
)
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/page/Page.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/page/Page.tsx
index cae34bd697..f376f9cd75 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/page/Page.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/page/Page.tsx
@@ -4,7 +4,7 @@ import { EditorElementProps, PageNode } from '$app/application/document/document
export const Page = memo(
forwardRef<HTMLDivElement, EditorElementProps<PageNode>>(({ node: _, children, ...attributes }, ref) => {
const className = useMemo(() => {
- return `${attributes.className ?? ''} pb-3 min-h-[52px] text-4xl font-bold`;
+ return `${attributes.className ?? ''} pb-3 text-4xl font-bold`;
}, [attributes.className]);
return (
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/quote/Quote.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/quote/Quote.tsx
index 91ede54012..5afc35289b 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/quote/Quote.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/quote/Quote.tsx
@@ -4,7 +4,7 @@ import { EditorElementProps, QuoteNode } from '$app/application/document/documen
export const QuoteList = memo(
forwardRef<HTMLDivElement, EditorElementProps<QuoteNode>>(({ node: _, children, ...attributes }, ref) => {
const className = useMemo(() => {
- return `flex w-full flex-col ml-2.5 border-l-[4px] border-fill-default pl-2.5 ${attributes.className ?? ''}`;
+ return `flex w-full flex-col ml-3 border-l-[4px] border-fill-default pl-2 ${attributes.className ?? ''}`;
}, [attributes.className]);
return (
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/text/StartIcon.hooks.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/text/StartIcon.hooks.tsx
new file mode 100644
index 0000000000..b745530acc
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/text/StartIcon.hooks.tsx
@@ -0,0 +1,46 @@
+import React, { FC, useCallback, useMemo } from 'react';
+import { EditorNodeType, TextNode } from '$app/application/document/document.types';
+import { ReactEditor, useSlate } from 'slate-react';
+import { Editor, Element } from 'slate';
+import CheckboxIcon from '$app/components/editor/components/blocks/todo_list/CheckboxIcon';
+import ToggleIcon from '$app/components/editor/components/blocks/toggle_list/ToggleIcon';
+import NumberListIcon from '$app/components/editor/components/blocks/numbered_list/NumberListIcon';
+import BulletedListIcon from '$app/components/editor/components/blocks/bulleted_list/BulletedListIcon';
+
+export function useStartIcon(node: TextNode) {
+ const editor = useSlate();
+ const path = ReactEditor.findPath(editor, node);
+ const block = Editor.parent(editor, path)?.[0] as Element | null;
+
+ const Component = useMemo(() => {
+ if (!Element.isElement(block)) {
+ return null;
+ }
+
+ switch (block.type) {
+ case EditorNodeType.TodoListBlock:
+ return CheckboxIcon;
+ case EditorNodeType.ToggleListBlock:
+ return ToggleIcon;
+ case EditorNodeType.NumberedListBlock:
+ return NumberListIcon;
+ case EditorNodeType.BulletedListBlock:
+ return BulletedListIcon;
+ default:
+ return null;
+ }
+ }, [block]) as FC<{ block: Element; className: string }> | null;
+
+ const renderIcon = useCallback(() => {
+ if (!Component || !block) {
+ return null;
+ }
+
+ return <Component className={`text-block-icon relative select-all`} block={block} />;
+ }, [Component, block]);
+
+ return {
+ hasStartIcon: !!Component,
+ renderIcon,
+ };
+}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/text/Text.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/text/Text.tsx
index dd309e333d..4e1642a8da 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/text/Text.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/text/Text.tsx
@@ -2,23 +2,27 @@ import React, { forwardRef, memo } from 'react';
import Placeholder from '$app/components/editor/components/blocks/_shared/Placeholder';
import { EditorElementProps, TextNode } from '$app/application/document/document.types';
import { useSlateStatic } from 'slate-react';
+import { useStartIcon } from '$app/components/editor/components/blocks/text/StartIcon.hooks';
export const Text = memo(
forwardRef<HTMLDivElement, EditorElementProps<TextNode>>(({ node, children, className, ...attributes }, ref) => {
const editor = useSlateStatic();
+ const { hasStartIcon, renderIcon } = useStartIcon(node);
const isEmpty = editor.isEmpty(node);
return (
- <div
+ <span
ref={ref}
{...attributes}
- className={`text-element min-h-[26px] px-1 ${!isEmpty ? 'flex items-center' : 'select-none leading-[26px]'} ${
- className ?? ''
- } relative h-full`}
+ className={`text-element relative my-1 flex w-full px-1 ${isEmpty ? 'select-none' : ''} ${className ?? ''} ${
+ hasStartIcon ? 'has-start-icon' : ''
+ }`}
>
+ {renderIcon()}
<Placeholder isEmpty={isEmpty} node={node} />
- <span>{children}</span>
- </div>
+
+ <span className={'min-w-[4px]'}>{children}</span>
+ </span>
);
})
);
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/todo_list/CheckboxIcon.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/todo_list/CheckboxIcon.tsx
new file mode 100644
index 0000000000..630aa93fb7
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/todo_list/CheckboxIcon.tsx
@@ -0,0 +1,32 @@
+import React, { useCallback } from 'react';
+import { TodoListNode } from '$app/application/document/document.types';
+import { CustomEditor } from '$app/components/editor/command';
+import { useSlateStatic } from 'slate-react';
+import { ReactComponent as CheckboxCheckSvg } from '$app/assets/database/checkbox-check.svg';
+import { ReactComponent as CheckboxUncheckSvg } from '$app/assets/database/checkbox-uncheck.svg';
+
+function CheckboxIcon({ block, className }: { block: TodoListNode; className: string }) {
+ const editor = useSlateStatic();
+ const { checked } = block.data;
+
+ const toggleTodo = useCallback(() => {
+ CustomEditor.toggleTodo(editor, block);
+ }, [editor, block]);
+
+ return (
+ <span
+ data-playwright-selected={false}
+ contentEditable={false}
+ onClick={toggleTodo}
+ draggable={false}
+ onMouseDown={(e) => {
+ e.preventDefault();
+ }}
+ className={`${className} cursor-pointer pr-1 text-xl text-fill-default`}
+ >
+ {checked ? <CheckboxCheckSvg /> : <CheckboxUncheckSvg />}
+ </span>
+ );
+}
+
+export default CheckboxIcon;
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/todo_list/TodoList.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/todo_list/TodoList.tsx
index b3e9f0704a..a6a4759e8a 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/todo_list/TodoList.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/todo_list/TodoList.tsx
@@ -1,31 +1,15 @@
-import React, { forwardRef, memo, useCallback, useMemo } from 'react';
+import React, { forwardRef, memo, useMemo } from 'react';
import { EditorElementProps, TodoListNode } from '$app/application/document/document.types';
-import { ReactComponent as CheckboxCheckSvg } from '$app/assets/database/checkbox-check.svg';
-import { ReactComponent as CheckboxUncheckSvg } from '$app/assets/database/checkbox-uncheck.svg';
-import { useSlateStatic } from 'slate-react';
-import { CustomEditor } from '$app/components/editor/command';
export const TodoList = memo(
forwardRef<HTMLDivElement, EditorElementProps<TodoListNode>>(({ node, children, ...attributes }, ref) => {
const { checked } = node.data;
- const editor = useSlateStatic();
const className = useMemo(() => {
- return `flex w-full flex-col pl-6 ${checked ? 'checked' : ''} ${attributes.className ?? ''}`;
+ return `flex w-full flex-col ${checked ? 'checked' : ''} ${attributes.className ?? ''}`;
}, [attributes.className, checked]);
- const toggleTodo = useCallback(() => {
- CustomEditor.toggleTodo(editor, node);
- }, [editor, node]);
return (
<>
- <span
- data-playwright-selected={false}
- contentEditable={false}
- onClick={toggleTodo}
- className='absolute cursor-pointer select-none pt-[3px] text-xl text-fill-default'
- >
- {checked ? <CheckboxCheckSvg /> : <CheckboxUncheckSvg />}
- </span>
<div {...attributes} ref={ref} className={className}>
{children}
</div>
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/toggle_list/ToggleIcon.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/toggle_list/ToggleIcon.tsx
new file mode 100644
index 0000000000..ad27822cb5
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/toggle_list/ToggleIcon.tsx
@@ -0,0 +1,30 @@
+import React, { useCallback } from 'react';
+import { CustomEditor } from '$app/components/editor/command';
+import { useSlateStatic } from 'slate-react';
+import { ToggleListNode } from '$app/application/document/document.types';
+import { ReactComponent as RightSvg } from '$app/assets/more.svg';
+
+function ToggleIcon({ block, className }: { block: ToggleListNode; className: string }) {
+ const editor = useSlateStatic();
+ const { collapsed } = block.data;
+
+ const toggleToggleList = useCallback(() => {
+ CustomEditor.toggleToggleList(editor, block);
+ }, [editor, block]);
+
+ return (
+ <span
+ data-playwright-selected={false}
+ contentEditable={false}
+ onClick={toggleToggleList}
+ onMouseDown={(e) => {
+ e.preventDefault();
+ }}
+ className={`${className} cursor-pointer pr-1 text-xl hover:text-fill-default`}
+ >
+ {collapsed ? <RightSvg /> : <RightSvg className={'rotate-90 transform'} />}
+ </span>
+ );
+}
+
+export default ToggleIcon;
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/toggle_list/ToggleList.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/toggle_list/ToggleList.tsx
index 6e8810a204..8af826ae22 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/toggle_list/ToggleList.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/blocks/toggle_list/ToggleList.tsx
@@ -1,30 +1,13 @@
-import React, { forwardRef, memo, useCallback, useMemo } from 'react';
+import React, { forwardRef, memo } from 'react';
import { EditorElementProps, ToggleListNode } from '$app/application/document/document.types';
-import { ReactEditor, useSlateStatic } from 'slate-react';
-import { ReactComponent as RightSvg } from '$app/assets/more.svg';
-import { CustomEditor } from '$app/components/editor/command';
export const ToggleList = memo(
forwardRef<HTMLDivElement, EditorElementProps<ToggleListNode>>(({ node, children, ...attributes }, ref) => {
const { collapsed } = node.data;
- const editor = useSlateStatic() as ReactEditor;
- const className = useMemo(() => {
- return `pl-6 ${attributes.className ?? ''} ${collapsed ? 'collapsed' : ''}`;
- }, [attributes.className, collapsed]);
- const toggleToggleList = useCallback(() => {
- CustomEditor.toggleToggleList(editor, node);
- }, [editor, node]);
+ const className = `${attributes.className ?? ''} flex w-full flex-col ${collapsed ? 'collapsed' : ''}`;
return (
<>
- <span
- data-playwright-selected={false}
- contentEditable={false}
- onClick={toggleToggleList}
- className='absolute cursor-pointer select-none pt-[3px] text-xl hover:text-fill-default'
- >
- {collapsed ? <RightSvg /> : <RightSvg className={'rotate-90 transform'} />}
- </span>
<div {...attributes} ref={ref} className={className}>
{children}
</div>
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/CustomEditable.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/CustomEditable.tsx
index 90d7c508a3..c6bdd330b6 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/CustomEditable.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/CustomEditable.tsx
@@ -7,5 +7,15 @@ type CustomEditableProps = Omit<ComponentProps<typeof Editable>, 'renderElement'
Partial<Pick<ComponentProps<typeof Editable>, 'renderElement' | 'renderLeaf'>>;
export function CustomEditable({ renderElement = Element, renderLeaf = Leaf, ...props }: CustomEditableProps) {
- return <Editable {...props} renderElement={renderElement} renderLeaf={renderLeaf} />;
+ return (
+ <Editable
+ {...props}
+ autoCorrect={'off'}
+ autoComplete={'off'}
+ autoFocus
+ spellCheck={false}
+ renderElement={renderElement}
+ renderLeaf={renderLeaf}
+ />
+ );
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/Editor.hooks.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/Editor.hooks.ts
index f2a8cb2a36..0dcb54cfd3 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/Editor.hooks.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/Editor.hooks.ts
@@ -1,17 +1,16 @@
-import { createContext, useCallback, useEffect, useMemo, useState } from 'react';
-import { EditorNodeType, CodeNode } from '$app/application/document/document.types';
+import { KeyboardEvent, useCallback, useEffect, useMemo } from 'react';
-import { createEditor, NodeEntry, BaseRange, Editor, Element, Range } from 'slate';
+import { BaseRange, createEditor, Editor, NodeEntry, Range, Transforms, Element } from 'slate';
import { ReactEditor, withReact } from 'slate-react';
import { withBlockPlugins } from '$app/components/editor/plugins/withBlockPlugins';
-import { decorateCode } from '$app/components/editor/components/blocks/code/utils';
-import { withShortcuts } from '$app/components/editor/components/editor/shortcuts';
+import { withShortcuts } from 'src/appflowy_app/components/editor/plugins/shortcuts';
import { withInlines } from '$app/components/editor/components/inline_nodes';
-import { withYjs, YjsEditor, withYHistory } from '@slate-yjs/core';
+import { withYHistory, withYjs, YjsEditor } from '@slate-yjs/core';
import * as Y from 'yjs';
import { CustomEditor } from '$app/components/editor/command';
-import { proxySet, subscribeKey } from 'valtio/utils';
-import { useSnapshot } from 'valtio';
+import { CodeNode, EditorNodeType } from '$app/application/document/document.types';
+import { decorateCode } from '$app/components/editor/components/blocks/code/utils';
+import isHotkey from 'is-hotkey';
export function useEditor(sharedType: Y.XmlText) {
const editor = useMemo(() => {
@@ -63,7 +62,12 @@ export function useDecorateCodeHighlight(editor: ReactEditor) {
(entry: NodeEntry): BaseRange[] => {
const path = entry[1];
- const blockEntry = path.length > 1 ? editor.node([path[0]]) : editor.node(path);
+ const blockEntry = editor.above({
+ at: path,
+ match: (n) => !Editor.isEditor(n) && Element.isElement(n) && n.blockId !== undefined,
+ });
+
+ if (!blockEntry) return [];
const block = blockEntry[0] as CodeNode;
@@ -79,103 +83,38 @@ export function useDecorateCodeHighlight(editor: ReactEditor) {
);
}
-export function useEditorState(editor: ReactEditor) {
- const selectedBlocks = useMemo(() => proxySet([]), []);
- const decorateState = useMemo(
- () =>
- proxySet<{
- range: BaseRange;
- class_name: string;
- }>([]),
- []
- );
-
- const [selectedLength, setSelectedLength] = useState(0);
-
- const ranges = useSnapshot(decorateState);
-
- subscribeKey(selectedBlocks, 'size', (v) => setSelectedLength(v));
-
- useEffect(() => {
- const { onChange } = editor;
-
- const onKeydown = (e: KeyboardEvent) => {
- if (!ReactEditor.isFocused(editor) && selectedLength > 0) {
- e.preventDefault();
- e.stopPropagation();
- const selectedBlockId = selectedBlocks.values().next().value;
- const [selectedBlock] = editor.nodes({
- at: [],
- match: (n) => Element.isElement(n) && n.blockId === selectedBlockId,
- });
- const [, path] = selectedBlock;
-
- editor.select(path);
- ReactEditor.focus(editor);
- }
- };
-
- if (selectedLength > 0) {
- editor.onChange = (...args) => {
- const isSelectionChange = editor.operations.every((arg) => arg.type === 'set_selection');
-
- if (isSelectionChange) {
- selectedBlocks.clear();
+export function useInlineKeyDown(editor: ReactEditor) {
+ return useCallback(
+ (e: KeyboardEvent<HTMLDivElement>) => {
+ const selection = editor.selection;
+
+ // Default left/right behavior is unit:'character'.
+ // This fails to distinguish between two cursor positions, such as
+ // <inline>foo<cursor/></inline> vs <inline>foo</inline><cursor/>.
+ // Here we modify the behavior to unit:'offset'.
+ // This lets the user step into and out of the inline without stepping over characters.
+ // You may wish to customize this further to only use unit:'offset' in specific cases.
+ if (selection && Range.isCollapsed(selection)) {
+ const { nativeEvent } = e;
+
+ if (
+ isHotkey('left', nativeEvent) &&
+ CustomEditor.beforeIsInlineNode(editor, selection, {
+ unit: 'offset',
+ })
+ ) {
+ e.preventDefault();
+ Transforms.move(editor, { unit: 'offset', reverse: true });
+ return;
}
- onChange(...args);
- };
-
- document.addEventListener('keydown', onKeydown);
- } else {
- editor.onChange = onChange;
- document.removeEventListener('keydown', onKeydown);
- }
-
- return () => {
- editor.onChange = onChange;
- document.removeEventListener('keydown', onKeydown);
- };
- }, [editor, selectedBlocks, selectedLength]);
-
- const decorate = useCallback(
- ([, path]: NodeEntry): BaseRange[] => {
- const highlightRanges: (Range & {
- class_name: string;
- })[] = [];
-
- ranges.forEach((state) => {
- const intersection = Range.intersection(state.range, Editor.range(editor, path));
-
- if (intersection) {
- highlightRanges.push({
- ...intersection,
- class_name: state.class_name,
- });
+ if (isHotkey('right', nativeEvent) && CustomEditor.afterIsInlineNode(editor, selection, { unit: 'offset' })) {
+ e.preventDefault();
+ Transforms.move(editor, { unit: 'offset' });
+ return;
}
- });
-
- return highlightRanges;
+ }
},
- [editor, ranges]
+ [editor]
);
-
- return {
- selectedBlocks,
- decorate,
- decorateState,
- };
}
-
-export const EditorSelectedBlockContext = createContext<Set<string>>(new Set());
-
-export const EditorSelectedBlockProvider = EditorSelectedBlockContext.Provider;
-
-export const DecorateStateContext = createContext<
- Set<{
- range: BaseRange;
- class_name: string;
- }>
->(new Set());
-
-export const DecorateStateProvider = DecorateStateContext.Provider;
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/Editor.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/Editor.tsx
index 352e981510..ee2eced340 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/Editor.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/Editor.tsx
@@ -1,27 +1,39 @@
import React, { memo, useCallback } from 'react';
import {
- DecorateStateProvider,
- EditorSelectedBlockProvider,
useDecorateCodeHighlight,
useEditor,
- useEditorState,
+ useInlineKeyDown,
} from '$app/components/editor/components/editor/Editor.hooks';
import { Slate } from 'slate-react';
import { CustomEditable } from '$app/components/editor/components/editor/CustomEditable';
import { SelectionToolbar } from '$app/components/editor/components/tools/selection_toolbar';
-import { useShortcuts } from '$app/components/editor/components/editor/shortcuts';
+import { useShortcuts } from 'src/appflowy_app/components/editor/plugins/shortcuts';
import { BlockActionsToolbar } from '$app/components/editor/components/tools/block_actions';
-import { SlashCommandPanel } from '$app/components/editor/components/tools/command_panel/slash_command_panel';
-import { MentionPanel } from '$app/components/editor/components/tools/command_panel/mention_panel';
+
import { CircularProgress } from '@mui/material';
import * as Y from 'yjs';
import { NodeEntry } from 'slate';
+import {
+ DecorateStateProvider,
+ EditorSelectedBlockProvider,
+ useInitialEditorState,
+ SlashStateProvider,
+ EditorInlineBlockStateProvider,
+} from '$app/components/editor/stores';
+import CommandPanel from '../tools/command_panel/CommandPanel';
function Editor({ sharedType }: { sharedType: Y.XmlText; id: string }) {
const { editor, initialValue, handleOnClickEnd, ...props } = useEditor(sharedType);
const decorateCodeHighlight = useDecorateCodeHighlight(editor);
const { onDOMBeforeInput, onKeyDown: onShortcutsKeyDown } = useShortcuts(editor);
- const { selectedBlocks, decorate: decorateCustomRange, decorateState } = useEditorState(editor);
+ const withInlineKeyDown = useInlineKeyDown(editor);
+ const {
+ selectedBlocks,
+ decorate: decorateCustomRange,
+ decorateState,
+ slashState,
+ inlineBlockState,
+ } = useInitialEditorState(editor);
const decorate = useCallback(
(entry: NodeEntry) => {
@@ -33,6 +45,14 @@ function Editor({ sharedType }: { sharedType: Y.XmlText; id: string }) {
[decorateCodeHighlight, decorateCustomRange]
);
+ const onKeyDown = useCallback(
+ (event: React.KeyboardEvent<HTMLDivElement>) => {
+ withInlineKeyDown(event);
+ onShortcutsKeyDown(event);
+ },
+ [onShortcutsKeyDown, withInlineKeyDown]
+ );
+
if (editor.sharedRoot.length === 0) {
return <CircularProgress className='m-auto' />;
}
@@ -40,20 +60,24 @@ function Editor({ sharedType }: { sharedType: Y.XmlText; id: string }) {
return (
<EditorSelectedBlockProvider value={selectedBlocks}>
<DecorateStateProvider value={decorateState}>
- <Slate editor={editor} initialValue={initialValue}>
- <SelectionToolbar />
- <BlockActionsToolbar />
- <CustomEditable
- {...props}
- onDOMBeforeInput={onDOMBeforeInput}
- onKeyDown={onShortcutsKeyDown}
- decorate={decorate}
- className={'px-16 caret-text-title outline-none focus:outline-none'}
- />
- <SlashCommandPanel />
- <MentionPanel />
- <div onClick={handleOnClickEnd} className={'relative bottom-0 left-0 h-10 w-full cursor-text'} />
- </Slate>
+ <EditorInlineBlockStateProvider value={inlineBlockState}>
+ <SlashStateProvider value={slashState}>
+ <Slate editor={editor} initialValue={initialValue}>
+ <BlockActionsToolbar />
+ <SelectionToolbar />
+
+ <CustomEditable
+ {...props}
+ onDOMBeforeInput={onDOMBeforeInput}
+ onKeyDown={onKeyDown}
+ decorate={decorate}
+ className={'px-16 caret-text-title outline-none focus:outline-none'}
+ />
+ <CommandPanel />
+ <div onClick={handleOnClickEnd} className={'relative bottom-0 left-0 h-10 w-full cursor-text'} />
+ </Slate>
+ </SlashStateProvider>
+ </EditorInlineBlockStateProvider>
</DecorateStateProvider>
</EditorSelectedBlockProvider>
);
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/Element.hooks.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/Element.hooks.ts
index ecebc47162..bf7045705d 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/Element.hooks.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/Element.hooks.ts
@@ -1,31 +1,30 @@
import { Element } from 'slate';
import { useContext, useEffect, useMemo } from 'react';
-import { EditorSelectedBlockContext } from '$app/components/editor/components/editor/Editor.hooks';
import { useSnapshot } from 'valtio';
-import { useSelected, useSlateStatic } from 'slate-react';
+import { useSelected } from 'slate-react';
+
+import { EditorSelectedBlockContext } from '$app/components/editor/stores/selected';
export function useElementState(element: Element) {
const blockId = element.blockId;
- const editor = useSlateStatic();
const selectedBlockContext = useContext(EditorSelectedBlockContext);
const selected = useSelected();
useEffect(() => {
if (!blockId) return;
- if (selected && !editor.isSelectable(element)) {
- selectedBlockContext.add(blockId);
- } else {
+
+ if (!selected) {
selectedBlockContext.delete(blockId);
}
- }, [blockId, editor, element, selected, selectedBlockContext]);
+ }, [blockId, selected, selectedBlockContext]);
const selectedBlockIds = useSnapshot(selectedBlockContext);
- const isSelected = useMemo(() => {
+ const blockSelected = useMemo(() => {
if (!blockId) return false;
return selectedBlockIds.has(blockId);
}, [blockId, selectedBlockIds]);
return {
- isSelected,
+ blockSelected,
};
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/Element.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/Element.tsx
index 42e5d98777..f11551c5d8 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/Element.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/Element.tsx
@@ -1,5 +1,5 @@
import React, { FC, HTMLAttributes, useMemo } from 'react';
-import { RenderElementProps } from 'slate-react';
+import { RenderElementProps, useSlateStatic } from 'slate-react';
import {
BlockData,
EditorElementProps,
@@ -73,7 +73,9 @@ function Element({ element, attributes, children }: RenderElementProps) {
}
}, [node.type]) as FC<EditorElementProps & HTMLAttributes<HTMLElement>>;
- const { isSelected } = useElementState(node);
+ const editor = useSlateStatic();
+ const { blockSelected } = useElementState(node);
+ const isEmbed = editor.isEmbed(node);
const className = useMemo(() => {
const align =
@@ -83,10 +85,10 @@ function Element({ element, attributes, children }: RenderElementProps) {
}
)?.align || 'left';
- return `block-element flex rounded ${isSelected ? 'bg-content-blue-100' : ''} ${
- align ? `block-align-${align}` : ''
+ return `block-element flex rounded ${align ? `block-align-${align}` : ''} ${
+ blockSelected && !isEmbed ? 'bg-content-blue-100' : ''
}`;
- }, [isSelected, node.data]);
+ }, [node.data, blockSelected, isEmbed]);
const style = useMemo(() => {
const data = (node.data as BlockData) || {};
@@ -99,9 +101,9 @@ function Element({ element, attributes, children }: RenderElementProps) {
if (InlineComponent) {
return (
- <span {...attributes}>
- <InlineComponent node={node}>{children}</InlineComponent>
- </span>
+ <InlineComponent {...attributes} node={node}>
+ {children}
+ </InlineComponent>
);
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/Leaf.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/Leaf.tsx
index 2f64bcaa58..468cc3d380 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/Leaf.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/Leaf.tsx
@@ -1,6 +1,6 @@
import React, { CSSProperties } from 'react';
import { RenderLeafProps } from 'slate-react';
-import { Link } from '$app/components/editor/components/marks';
+import { Link } from '$app/components/editor/components/inline_nodes/link';
export function Leaf({ attributes, children, leaf }: RenderLeafProps) {
let newChildren = children;
@@ -9,7 +9,12 @@ export function Leaf({ attributes, children, leaf }: RenderLeafProps) {
if (leaf.code) {
newChildren = (
- <code className={'bg-gray-300 bg-opacity-50 text-xs font-normal tracking-wider text-[#EB5757]'}>
+ <code
+ style={{
+ fontSize: '0.85em',
+ }}
+ className={'bg-fill-list-active bg-opacity-50 font-normal tracking-wider text-[#EB5757]'}
+ >
{newChildren}
</code>
);
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/index.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/index.ts
index 1a0dfa04d5..c0c3c728d1 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/index.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/index.ts
@@ -1,2 +1,3 @@
export * from './CollaborativeEditor';
export * from './Editor';
+export { useDecorateCodeHighlight } from '$app/components/editor/components/editor/Editor.hooks';
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/shortcuts/withCommandShortcuts.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/shortcuts/withCommandShortcuts.ts
deleted file mode 100644
index e178ca1dcd..0000000000
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/shortcuts/withCommandShortcuts.ts
+++ /dev/null
@@ -1,97 +0,0 @@
-import { ReactEditor } from 'slate-react';
-import { Editor, Range } from 'slate';
-import { CustomEditor } from '$app/components/editor/command';
-
-export enum EditorCommand {
- Mention = '@',
- SlashCommand = '/',
-}
-
-// pop mention panel when @ is typed
-// pop slash command panel when / is typed
-const commands = [EditorCommand.Mention, EditorCommand.SlashCommand] as string[];
-
-export const commandPanelClsSelector: Record<string, string> = {
- [EditorCommand.Mention]: '.mention-panel',
- [EditorCommand.SlashCommand]: '.slash-command-panel',
-};
-
-export const commandPanelShowProperty = 'is-show';
-
-export function withCommandShortcuts(editor: ReactEditor) {
- const { insertText, deleteBackward } = editor;
-
- editor.insertText = (text) => {
- if (CustomEditor.isCodeBlock(editor) || CustomEditor.selectionIncludeRoot(editor)) {
- insertText(text);
- return;
- }
-
- const { selection } = editor;
-
- const endOfPanelChar = commands.find((char) => {
- return text.endsWith(char);
- });
-
- if (endOfPanelChar !== undefined && selection && Range.isCollapsed(selection)) {
- const block = CustomEditor.getBlock(editor);
- const path = block ? block[1] : [];
- const { anchor } = selection;
- const beforeText = Editor.string(editor, { anchor, focus: Editor.start(editor, path) }) + text.slice(0, -1);
- // show the panel when insert char at after space or at start of element
- const showPanel = !beforeText || beforeText.endsWith(' ');
-
- if (showPanel) {
- const slateDom = ReactEditor.toDOMNode(editor, editor);
-
- if (commands.includes(endOfPanelChar)) {
- const selector = commandPanelClsSelector[endOfPanelChar] || '';
-
- slateDom.parentElement?.querySelector(selector)?.classList.add(commandPanelShowProperty);
- }
- }
- }
-
- insertText(text);
- };
-
- editor.deleteBackward = (...args) => {
- if (CustomEditor.isCodeBlock(editor)) {
- deleteBackward(...args);
- return;
- }
-
- const { selection } = editor;
-
- if (selection && Range.isCollapsed(selection)) {
- const { anchor } = selection;
- const block = CustomEditor.getBlock(editor);
- const path = block ? block[1] : [];
- const beforeText = Editor.string(editor, { anchor, focus: Editor.start(editor, path) });
-
- // if delete backward at start of panel char, and then it will be deleted, so we should close the panel if it is open
- if (commands.includes(beforeText)) {
- const slateDom = ReactEditor.toDOMNode(editor, editor);
-
- const selector = commandPanelClsSelector[beforeText] || '';
-
- slateDom.parentElement?.querySelector(selector)?.classList.remove(commandPanelShowProperty);
- }
-
- // if delete backward at start of paragraph, and then it will be deleted, so we should close the panel if it is open
- if (CustomEditor.focusAtStartOfBlock(editor)) {
- const slateDom = ReactEditor.toDOMNode(editor, editor);
-
- commands.forEach((char) => {
- const selector = commandPanelClsSelector[char] || '';
-
- slateDom.parentElement?.querySelector(selector)?.classList.remove(commandPanelShowProperty);
- });
- }
- }
-
- deleteBackward(...args);
- };
-
- return editor;
-}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/shortcuts/withShortcuts.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/shortcuts/withShortcuts.ts
deleted file mode 100644
index 04d03a11af..0000000000
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/shortcuts/withShortcuts.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-import { ReactEditor } from 'slate-react';
-import { withMarkdownShortcuts } from '$app/components/editor/components/editor/shortcuts/withMarkdownShortcuts';
-import { withCommandShortcuts } from '$app/components/editor/components/editor/shortcuts/withCommandShortcuts';
-
-export function withShortcuts(editor: ReactEditor) {
- return withMarkdownShortcuts(withCommandShortcuts(editor));
-}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/inline_formula/FormulaEditPopover.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/inline_formula/FormulaEditPopover.tsx
index ae2ce5590f..7da9a36f19 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/inline_formula/FormulaEditPopover.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/inline_formula/FormulaEditPopover.tsx
@@ -37,26 +37,36 @@ function FormulaEditPopover({
horizontal: 'center',
}}
>
- <div className='flex p-2 '>
+ <div className='flex gap-1 p-3'>
<TextField
variant={'standard'}
size={'small'}
autoFocus={true}
value={text}
+ spellCheck={false}
placeholder={'E = mc^2'}
onChange={(e) => setText(e.target.value)}
fullWidth={true}
onKeyDown={(e) => {
+ e.stopPropagation();
if (e.key === 'Enter') {
+ e.preventDefault();
onDone(text);
}
+
+ if (e.key === 'Escape') {
+ e.preventDefault();
+ onClose();
+ }
+
+ if (e.key === 'Tab') {
+ e.preventDefault();
+ }
}}
/>
- <div className={'ml-2'}>
- <Button size={'small'} variant={'text'} onClick={() => onDone(text)}>
- {t('button.done')}
- </Button>
- </div>
+ <Button size={'small'} variant={'text'} onClick={() => onDone(text)}>
+ {t('button.done')}
+ </Button>
</div>
</Popover>
);
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/inline_formula/FormulaLeaf.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/inline_formula/FormulaLeaf.tsx
index 5899630867..324d273ab3 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/inline_formula/FormulaLeaf.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/inline_formula/FormulaLeaf.tsx
@@ -4,7 +4,10 @@ import KatexMath from '$app/components/_shared/katex_math/KatexMath';
function FormulaLeaf({ formula, children }: { formula: string; children: React.ReactNode }) {
return (
<span className={'relative'}>
- <KatexMath latex={formula || ''} isInline />
+ <span className={'select-none'} contentEditable={false}>
+ <KatexMath latex={formula || ''} isInline />
+ </span>
+
<span className={'absolute left-0 right-0 h-0 w-0 opacity-0'}>{children}</span>
</span>
);
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/inline_formula/InlineFormula.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/inline_formula/InlineFormula.tsx
index d0d452d29e..fe2eba8a59 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/inline_formula/InlineFormula.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/inline_formula/InlineFormula.tsx
@@ -1,56 +1,46 @@
-import React, { forwardRef, memo, useCallback, MouseEvent, useRef, useEffect, useState } from 'react';
+import React, { forwardRef, memo, useCallback, MouseEvent, useRef } from 'react';
import { ReactEditor, useSelected, useSlate } from 'slate-react';
-import { Transforms, Range, Editor } from 'slate';
+import { Transforms } from 'slate';
import { EditorElementProps, FormulaNode } from '$app/application/document/document.types';
import FormulaLeaf from '$app/components/editor/components/inline_nodes/inline_formula/FormulaLeaf';
import FormulaEditPopover from '$app/components/editor/components/inline_nodes/inline_formula/FormulaEditPopover';
import { getNodePath, moveCursorToNodeEnd } from '$app/components/editor/components/editor/utils';
import { CustomEditor } from '$app/components/editor/command';
+import { useEditorInlineBlockState } from '$app/components/editor/stores';
+import { InlineChromiumBugfix } from '$app/components/editor/components/inline_nodes/InlineChromiumBugfix';
export const InlineFormula = memo(
forwardRef<HTMLSpanElement, EditorElementProps<FormulaNode>>(({ node, children, ...attributes }, ref) => {
const editor = useSlate();
const formula = node.data;
- const selected = useSelected();
-
+ const { popoverOpen = false, setRange, openPopover, closePopover } = useEditorInlineBlockState('formula');
const anchor = useRef<HTMLSpanElement | null>(null);
- const [openEditPopover, setOpenEditPopover] = useState<boolean>(false);
+ const selected = useSelected();
+ const open = popoverOpen && selected;
const handleClick = useCallback(
(e: MouseEvent<HTMLSpanElement>) => {
const target = e.currentTarget;
const path = getNodePath(editor, target);
- setOpenEditPopover(true);
ReactEditor.focus(editor);
Transforms.select(editor, path);
+ if (editor.selection) {
+ setRange(editor.selection);
+ openPopover();
+ }
},
- [editor]
+ [editor, openPopover, setRange]
);
- useEffect(() => {
- const selection = editor.selection;
-
- if (selected && selection) {
- const path = ReactEditor.findPath(editor, node);
- const nodeRange = Editor.range(editor, path);
-
- if (Range.includes(nodeRange, selection.anchor) && Range.includes(nodeRange, selection.focus)) {
- setOpenEditPopover(true);
- }
- } else {
- setOpenEditPopover(false);
- }
- }, [editor, node, selected]);
-
const handleEditPopoverClose = useCallback(() => {
- setOpenEditPopover(false);
+ closePopover();
if (anchor.current === null) {
return;
}
moveCursorToNodeEnd(editor, anchor.current);
- }, [editor]);
+ }, [closePopover, editor]);
return (
<>
@@ -69,16 +59,23 @@ export const InlineFormula = memo(
contentEditable={false}
onDoubleClick={handleClick}
onClick={handleClick}
- className={`relative rounded px-1 py-0.5 text-xs ${selected ? 'bg-fill-list-active' : ''}`}
- data-playwright-selected={selected}
+ className={`${attributes.className ?? ''} formula-inline relative rounded px-1 py-0.5 ${
+ selected ? 'selected' : ''
+ }`}
>
+ <InlineChromiumBugfix />
<FormulaLeaf formula={formula}>{children}</FormulaLeaf>
+ <InlineChromiumBugfix />
</span>
- {openEditPopover && (
+ {open && (
<FormulaEditPopover
defaultText={formula}
onDone={(newFormula) => {
- if (anchor.current === null || newFormula === formula) return;
+ if (anchor.current === null || newFormula === formula) {
+ handleEditPopoverClose();
+ return;
+ }
+
const path = getNodePath(editor, anchor.current);
// select the node before updating the formula
@@ -87,7 +84,7 @@ export const InlineFormula = memo(
const point = editor.before(path);
CustomEditor.deleteFormula(editor);
- setOpenEditPopover(false);
+ closePopover();
if (point) {
ReactEditor.focus(editor);
editor.select(point);
@@ -100,7 +97,7 @@ export const InlineFormula = memo(
}
}}
anchorEl={anchor.current}
- open={openEditPopover}
+ open={open}
onClose={handleEditPopoverClose}
/>
)}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/link/Link.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/link/Link.tsx
new file mode 100644
index 0000000000..dff7b7dae6
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/link/Link.tsx
@@ -0,0 +1,48 @@
+import { memo, useCallback, useRef } from 'react';
+import { ReactEditor, useSlate } from 'slate-react';
+import { getNodePath } from '$app/components/editor/components/editor/utils';
+import { Transforms, Text } from 'slate';
+import { useDecorateDispatch } from '$app/components/editor/stores';
+
+export const Link = memo(({ children }: { leaf: Text; children: React.ReactNode }) => {
+ const { add: addDecorate } = useDecorateDispatch();
+
+ const editor = useSlate();
+
+ const ref = useRef<HTMLSpanElement | null>(null);
+
+ const handleClick = useCallback(
+ (e: React.MouseEvent) => {
+ e.stopPropagation();
+ e.preventDefault();
+ if (ref.current === null) {
+ return;
+ }
+
+ const path = getNodePath(editor, ref.current);
+
+ ReactEditor.focus(editor);
+ Transforms.select(editor, path);
+
+ if (!editor.selection) return;
+ addDecorate({
+ range: editor.selection,
+ class_name: 'bg-content-blue-100 rounded',
+ type: 'link',
+ });
+ },
+ [addDecorate, editor]
+ );
+
+ return (
+ <>
+ <span
+ ref={ref}
+ onMouseDown={handleClick}
+ className={`cursor-pointer rounded px-1 py-0.5 text-fill-default underline`}
+ >
+ {children}
+ </span>
+ </>
+ );
+});
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/link/LinkEditContent.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/link/LinkEditContent.tsx
new file mode 100644
index 0000000000..dd980c7faa
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/link/LinkEditContent.tsx
@@ -0,0 +1,169 @@
+import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
+import Typography from '@mui/material/Typography';
+import { addMark, removeMark } from 'slate';
+import { EditorMarkFormat } from '$app/application/document/document.types';
+import { open as openWindow } from '@tauri-apps/api/shell';
+import { notify } from '$app/components/editor/components/tools/notify';
+import { CustomEditor } from '$app/components/editor/command';
+import { useTranslation } from 'react-i18next';
+import { useSlateStatic } from 'slate-react';
+import { ReactComponent as RemoveSvg } from '$app/assets/delete.svg';
+import { ReactComponent as LinkSvg } from '$app/assets/link.svg';
+import { ReactComponent as CopySvg } from '$app/assets/copy.svg';
+import KeyboardNavigation, {
+ KeyboardNavigationOption,
+} from '$app/components/_shared/keyboard_navigation/KeyboardNavigation';
+import isHotkey from 'is-hotkey';
+import LinkEditInput, { pattern } from '$app/components/editor/components/inline_nodes/link/LinkEditInput';
+
+function LinkEditContent({ onClose, defaultHref }: { onClose: () => void; defaultHref: string }) {
+ const editor = useSlateStatic();
+ const { t } = useTranslation();
+ const isActivated = CustomEditor.isMarkActive(editor, EditorMarkFormat.Href);
+
+ const [focusMenu, setFocusMenu] = useState<boolean>(false);
+ const scrollRef = useRef<HTMLDivElement>(null);
+ const inputRef = useRef<HTMLElement>(null);
+ const [link, setLink] = useState<string>(defaultHref);
+
+ const setNodeMark = useCallback(() => {
+ if (link === '') {
+ removeMark(editor, EditorMarkFormat.Href);
+ } else {
+ addMark(editor, EditorMarkFormat.Href, link);
+ }
+ }, [editor, link]);
+
+ const removeNodeMark = useCallback(() => {
+ onClose();
+ editor.removeMark(EditorMarkFormat.Href);
+ }, [editor, onClose]);
+
+ useEffect(() => {
+ const input = inputRef.current;
+
+ if (!input) return;
+
+ const handleKeyDown = (e: KeyboardEvent) => {
+ e.stopPropagation();
+
+ if (e.key === 'Enter') {
+ e.preventDefault();
+ if (pattern.test(link)) {
+ onClose();
+ setNodeMark();
+ }
+
+ return;
+ }
+
+ if (e.key === 'Escape') {
+ e.preventDefault();
+ onClose();
+ return;
+ }
+
+ if (e.key === 'Tab') {
+ e.preventDefault();
+ setFocusMenu(true);
+ return;
+ }
+
+ if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
+ notify.clear();
+ notify.info(`Press Tab to focus on the menu`);
+ return;
+ }
+ };
+
+ input.addEventListener('keydown', handleKeyDown);
+ return () => {
+ input.removeEventListener('keydown', handleKeyDown);
+ };
+ }, [link, onClose, setNodeMark]);
+
+ const onConfirm = useCallback(
+ (key: string) => {
+ if (key === 'open') {
+ const linkPrefix = ['http://', 'https://', 'file://', 'ftp://', 'ftps://', 'mailto:'];
+
+ if (linkPrefix.some((prefix) => link.startsWith(prefix))) {
+ void openWindow(link);
+ } else {
+ void openWindow('https://' + link);
+ }
+ } else if (key === 'copy') {
+ void navigator.clipboard.writeText(link);
+ notify.success(t('message.copy.success'));
+ } else if (key === 'remove') {
+ removeNodeMark();
+ }
+ },
+ [link, removeNodeMark, t]
+ );
+
+ const renderOption = useCallback((icon: React.ReactNode, label: string) => {
+ return (
+ <div key={label} className={'flex items-center gap-2'}>
+ {icon}
+ <div className={'flex-1'}>{label}</div>
+ </div>
+ );
+ }, []);
+
+ const editOptions: KeyboardNavigationOption[] = useMemo(() => {
+ return [
+ {
+ key: 'open',
+ disabled: !pattern.test(link),
+ content: renderOption(<LinkSvg className={'h-4 w-4'} />, t('editor.openLink')),
+ },
+ {
+ key: 'copy',
+ content: renderOption(<CopySvg className={'h-4 w-4'} />, t('editor.copyLink')),
+ },
+ {
+ key: 'remove',
+ content: renderOption(<RemoveSvg className={'h-4 w-4'} />, t('editor.removeLink')),
+ },
+ ];
+ }, [link, renderOption, t]);
+
+ return (
+ <>
+ {!isActivated && (
+ <Typography className={'w-full justify-start pb-2 font-medium'}>{t('editor.addYourLink')}</Typography>
+ )}
+ <LinkEditInput link={link} setLink={setLink} inputRef={inputRef} />
+
+ <div ref={scrollRef} className={'mt-1 flex w-full flex-col items-start'}>
+ {isActivated && (
+ <KeyboardNavigation
+ disableFocus={!focusMenu}
+ scrollRef={scrollRef}
+ options={editOptions}
+ onConfirm={onConfirm}
+ onFocus={() => {
+ setFocusMenu(true);
+ }}
+ onBlur={() => {
+ setFocusMenu(false);
+ }}
+ onEscape={onClose}
+ disableSelect={!focusMenu}
+ onKeyDown={(e) => {
+ e.stopPropagation();
+ if (isHotkey('Tab', e)) {
+ e.preventDefault();
+ setFocusMenu(false);
+ inputRef.current?.focus();
+ }
+ }}
+ />
+ )}
+ </div>
+ </>
+ );
+}
+
+export default LinkEditContent;
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/link/LinkEditInput.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/link/LinkEditInput.tsx
new file mode 100644
index 0000000000..4a0cc3e33c
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/link/LinkEditInput.tsx
@@ -0,0 +1,48 @@
+import React, { useEffect, useState } from 'react';
+import { TextField } from '@mui/material';
+import { useTranslation } from 'react-i18next';
+
+export const pattern = /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([/\w.-]*)*\/?$/;
+
+function LinkEditInput({
+ link,
+ setLink,
+ inputRef,
+}: {
+ link: string;
+ setLink: (link: string) => void;
+ inputRef: React.RefObject<HTMLElement>;
+}) {
+ const { t } = useTranslation();
+ const [error, setError] = useState<string | null>(null);
+
+ useEffect(() => {
+ if (pattern.test(link)) {
+ setError(null);
+ return;
+ }
+
+ setError(t('editor.incorrectLink'));
+ }, [link, t]);
+
+ return (
+ <form>
+ <TextField
+ variant={'outlined'}
+ size={'small'}
+ error={!!error}
+ helperText={error}
+ autoFocus={true}
+ value={link}
+ onChange={(e) => setLink(e.target.value)}
+ spellCheck={false}
+ inputRef={inputRef}
+ className={'my-1 p-0'}
+ placeholder={'https://example.com'}
+ fullWidth={true}
+ />
+ </form>
+ );
+}
+
+export default LinkEditInput;
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/link/LinkEditPopover.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/link/LinkEditPopover.tsx
new file mode 100644
index 0000000000..55714a2efb
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/link/LinkEditPopover.tsx
@@ -0,0 +1,69 @@
+import React from 'react';
+import Popover from '@mui/material/Popover';
+import { PopoverCommonProps } from '$app/components/editor/components/tools/popover';
+
+import usePopoverAutoPosition from '$app/components/_shared/popover/Popover.hooks';
+import { PopoverOrigin } from '@mui/material/Popover/Popover';
+import LinkEditContent from '$app/components/editor/components/inline_nodes/link/LinkEditContent';
+
+const initialAnchorOrigin: PopoverOrigin = {
+ vertical: 'bottom',
+ horizontal: 'center',
+};
+
+const initialTransformOrigin: PopoverOrigin = {
+ vertical: 'top',
+ horizontal: 'center',
+};
+
+export function LinkEditPopover({
+ defaultHref,
+ open,
+ onClose,
+ anchorPosition,
+ anchorReference,
+}: {
+ defaultHref: string;
+ open: boolean;
+ onClose: () => void;
+ anchorPosition?: { top: number; left: number; height: number };
+ anchorReference?: 'anchorPosition' | 'anchorEl';
+}) {
+ const {
+ paperHeight,
+ anchorPosition: newAnchorPosition,
+ transformOrigin,
+ anchorOrigin,
+ } = usePopoverAutoPosition({
+ anchorPosition,
+ open,
+ initialAnchorOrigin,
+ initialTransformOrigin,
+ initialPaperWidth: 340,
+ initialPaperHeight: 180,
+ });
+
+ return (
+ <Popover
+ {...PopoverCommonProps}
+ open={open}
+ anchorPosition={newAnchorPosition}
+ anchorReference={anchorReference}
+ onClose={() => {
+ onClose();
+ }}
+ transformOrigin={transformOrigin}
+ anchorOrigin={anchorOrigin}
+ onMouseDown={(e) => e.stopPropagation()}
+ >
+ <div
+ style={{
+ maxHeight: paperHeight,
+ }}
+ className='flex flex-col p-4'
+ >
+ <LinkEditContent defaultHref={defaultHref} onClose={onClose} />
+ </div>
+ </Popover>
+ );
+}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/marks/link/index.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/link/index.ts
similarity index 100%
rename from frontend/appflowy_tauri/src/appflowy_app/components/editor/components/marks/link/index.ts
rename to frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/link/index.ts
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/mention/Mention.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/mention/Mention.tsx
index 423ec4161a..638973d975 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/mention/Mention.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/mention/Mention.tsx
@@ -2,13 +2,16 @@ import React, { forwardRef, memo } from 'react';
import { EditorElementProps, MentionNode } from '$app/application/document/document.types';
import MentionLeaf from '$app/components/editor/components/inline_nodes/mention/MentionLeaf';
+import { InlineChromiumBugfix } from '$app/components/editor/components/inline_nodes/InlineChromiumBugfix';
export const Mention = memo(
forwardRef<HTMLSpanElement, EditorElementProps<MentionNode>>(({ node, children, ...attributes }, ref) => {
return (
<>
<span {...attributes} contentEditable={false} ref={ref}>
+ <InlineChromiumBugfix />
<MentionLeaf mention={node.data}>{children}</MentionLeaf>
+ <InlineChromiumBugfix />
</span>
</>
);
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/mention/MentionLeaf.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/mention/MentionLeaf.tsx
index adf22a53a0..4a4baebf2a 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/mention/MentionLeaf.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/inline_nodes/mention/MentionLeaf.tsx
@@ -5,11 +5,13 @@ import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import { pageTypeMap } from '$app_reducers/pages/slice';
import { getPage } from '$app/application/folder/page.service';
+import { useSelected } from 'slate-react';
export function MentionLeaf({ children, mention }: { mention: Mention; children: React.ReactNode }) {
const { t } = useTranslation();
const [page, setPage] = useState<MentionPage | null>(null);
const navigate = useNavigate();
+ const selected = useSelected();
const loadPage = useCallback(async () => {
if (!mention.page) return;
const page = await getPage(mention.page);
@@ -32,13 +34,17 @@ export function MentionLeaf({ children, mention }: { mention: Mention; children:
<span className={'relative'}>
{page && (
<span
- className={'relative inline-flex cursor-pointer items-center hover:bg-content-blue-100'}
+ className={'relative mx-1 inline-flex cursor-pointer items-center hover:rounded hover:bg-content-blue-100'}
onClick={openPage}
+ style={{
+ backgroundColor: selected ? 'var(--content-blue-100)' : undefined,
+ }}
>
- <span className={'text-sx absolute left-0'}>{page.icon?.value || <DocumentSvg />}</span>
- <span className={'ml-6 underline'}>{page.name || t('document.title.placeholder')}</span>
+ <span className={'text-sx absolute left-0.5'}>{page.icon?.value || <DocumentSvg />}</span>
+ <span className={'ml-6 mr-0.5 underline'}>{page.name || t('document.title.placeholder')}</span>
</span>
)}
+
<span className={'invisible'}>{children}</span>
</span>
);
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/marks/index.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/marks/index.ts
deleted file mode 100644
index e33728e03e..0000000000
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/marks/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from './link';
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/marks/link/Link.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/marks/link/Link.tsx
deleted file mode 100644
index 08e3404a10..0000000000
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/marks/link/Link.tsx
+++ /dev/null
@@ -1,106 +0,0 @@
-import { memo, useCallback, useEffect, useRef, useState } from 'react';
-import { ReactEditor, useSelected, useSlate } from 'slate-react';
-import { getNodePath, moveCursorToNodeEnd, moveCursorToPoint } from '$app/components/editor/components/editor/utils';
-import { BasePoint, Transforms, Text, Range, Editor } from 'slate';
-import { LinkEditPopover } from '$app/components/editor/components/marks/link/LinkEditPopover';
-
-export const Link = memo(({ leaf, children }: { leaf: Text; children: React.ReactNode }) => {
- const nodeSelected = useSelected();
-
- const editor = useSlate();
-
- const [selected, setSelected] = useState(false);
- const ref = useRef<HTMLSpanElement | null>(null);
- const [openEditPopover, setOpenEditPopover] = useState<boolean>(false);
-
- const getSelected = useCallback(
- (el: HTMLSpanElement, selection: Range) => {
- const entry = Editor.node(editor, selection);
- const [node, path] = entry;
- const dom = ReactEditor.toDOMNode(editor, node);
-
- if (!dom.contains(el)) return false;
-
- const offset = Editor.string(editor, path).length;
- const range = {
- anchor: {
- path,
- offset: 0,
- },
- focus: {
- path,
- offset,
- },
- };
-
- return Range.equals(range, selection);
- },
- [editor]
- );
-
- useEffect(() => {
- if (!ref.current) return;
- const selection = editor.selection;
-
- if (!nodeSelected || !selection) {
- setOpenEditPopover(false);
- setSelected(false);
- return;
- }
-
- const selected = getSelected(ref.current, selection);
-
- setOpenEditPopover(selected);
- setSelected(selected);
- }, [getSelected, editor, nodeSelected]);
-
- const handleClick = useCallback(() => {
- if (ref.current === null) {
- return;
- }
-
- const path = getNodePath(editor, ref.current);
-
- setOpenEditPopover(true);
- setSelected(true);
- ReactEditor.focus(editor);
- Transforms.select(editor, path);
- }, [editor]);
-
- const handleEditPopoverClose = useCallback(
- (at?: BasePoint) => {
- setOpenEditPopover(false);
- setSelected(false);
- if (ref.current === null) {
- return;
- }
-
- if (!at) {
- moveCursorToNodeEnd(editor, ref.current);
- } else {
- moveCursorToPoint(editor, at);
- }
- },
- [editor]
- );
-
- return (
- <>
- <span
- ref={ref}
- onClick={handleClick}
- className={`rounded px-1 py-0.5 text-fill-default underline ${selected ? 'bg-content-blue-50' : ''}`}
- >
- {children}
- </span>
- {openEditPopover && (
- <LinkEditPopover
- open={openEditPopover}
- anchorEl={ref.current}
- onClose={handleEditPopoverClose}
- defaultHref={leaf.href || ''}
- />
- )}
- </>
- );
-});
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/marks/link/LinkEditPopover.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/marks/link/LinkEditPopover.tsx
deleted file mode 100644
index 1d72058bac..0000000000
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/marks/link/LinkEditPopover.tsx
+++ /dev/null
@@ -1,141 +0,0 @@
-import React, { useCallback, useMemo, useState } from 'react';
-import { useTranslation } from 'react-i18next';
-import Popover from '@mui/material/Popover';
-import { PopoverCommonProps } from '$app/components/editor/components/tools/popover';
-import Button from '@mui/material/Button';
-import { getNodePath } from '$app/components/editor/components/editor/utils';
-import { addMark, BasePoint, Editor, Transforms, removeMark } from 'slate';
-import { useSlate } from 'slate-react';
-import { ReactComponent as RemoveSvg } from '$app/assets/delete.svg';
-import { ReactComponent as LinkSvg } from '$app/assets/link.svg';
-import { ReactComponent as CopySvg } from '$app/assets/copy.svg';
-import { open as openWindow } from '@tauri-apps/api/shell';
-import { OutlinedInput } from '@mui/material';
-import { notify } from '$app/components/editor/components/tools/notify';
-import { EditorMarkFormat } from '$app/application/document/document.types';
-
-const pattern = /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([/\w.-]*)*\/?$/;
-
-export function LinkEditPopover({
- defaultHref,
- open,
- anchorEl,
- onClose,
-}: {
- defaultHref: string;
- open: boolean;
- anchorEl: HTMLElement | null;
- onClose: (at?: BasePoint) => void;
-}) {
- const { t } = useTranslation();
- const editor = useSlate();
- const [link, setLink] = useState<string>(defaultHref);
-
- const setNodeMark = useCallback(() => {
- if (!anchorEl) return;
- const path = getNodePath(editor, anchorEl);
-
- // select the node before updating the formula
- Transforms.select(editor, path);
-
- if (link === '') {
- removeMark(editor, EditorMarkFormat.Href);
- } else {
- addMark(editor, EditorMarkFormat.Href, link);
- }
-
- onClose();
- }, [editor, anchorEl, link, onClose]);
-
- const removeNodeMark = useCallback(() => {
- if (!anchorEl) return;
- const path = getNodePath(editor, anchorEl);
- const beforePath = Editor.before(editor, path);
- const beforePathEnd = beforePath ? Editor.end(editor, beforePath) : undefined;
-
- // select the node before updating the formula
- Transforms.select(editor, path);
-
- editor.removeMark(EditorMarkFormat.Href);
-
- onClose(beforePathEnd);
- }, [editor, anchorEl, onClose]);
-
- const linkActions = useMemo(
- () => [
- {
- icon: <LinkSvg />,
- tooltip: t('editor.openLink'),
- onClick: () => {
- void openWindow(link);
- },
- disabled: !pattern.test(link),
- },
- {
- icon: <CopySvg />,
- tooltip: t('editor.copyLink'),
- onClick: async () => {
- await navigator.clipboard.writeText(link);
- notify.success(t('message.copy.success'));
- },
- },
- {
- icon: <RemoveSvg />,
- tooltip: t('editor.removeLink'),
- onClick: removeNodeMark,
- },
- ],
- [link, t, removeNodeMark]
- );
-
- return (
- <Popover
- {...PopoverCommonProps}
- open={open}
- anchorEl={anchorEl}
- onClose={() => {
- onClose();
- }}
- anchorOrigin={{
- vertical: 'bottom',
- horizontal: 'center',
- }}
- transformOrigin={{
- vertical: 'top',
- horizontal: 'center',
- }}
- >
- <div className='flex flex-col p-2'>
- <OutlinedInput
- size={'small'}
- autoFocus={true}
- onKeyDown={(e) => {
- if (e.key === 'Enter' && link) {
- setNodeMark();
- }
- }}
- className={'my-1 p-0'}
- value={link}
- placeholder={'https://example.com'}
- onChange={(e) => setLink(e.target.value)}
- fullWidth={true}
- />
- <div className={'mt-1 flex w-full flex-col items-start'}>
- {linkActions.map((action, index) => (
- <Button
- key={index}
- disabled={action.disabled}
- className={'w-full justify-start'}
- size={'small'}
- color={'inherit'}
- startIcon={action.icon}
- onClick={action.onClick}
- >
- {action.tooltip}
- </Button>
- ))}
- </div>
- </div>
- </Popover>
- );
-}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/_shared/BgColorPicker.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/_shared/BgColorPicker.tsx
deleted file mode 100644
index 8db1e42e8e..0000000000
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/_shared/BgColorPicker.tsx
+++ /dev/null
@@ -1,9 +0,0 @@
-import React from 'react';
-import { useTranslation } from 'react-i18next';
-import { ColorPicker, ColorPickerProps } from '$app/components/editor/components/tools/_shared/ColorPicker';
-
-export function BgColorPicker(props: ColorPickerProps) {
- const { t } = useTranslation();
-
- return <ColorPicker {...props} label={t('editor.backgroundColor')} />;
-}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/_shared/ColorPicker.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/_shared/ColorPicker.tsx
index 9f3c892248..10b86e503a 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/_shared/ColorPicker.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/_shared/ColorPicker.tsx
@@ -1,155 +1,173 @@
-import React, { useEffect, useMemo, useRef, useState } from 'react';
-import { useTranslation } from 'react-i18next';
-import { CustomColorPicker } from '$app/components/editor/components/tools/_shared/CustomColorPicker';
+import React, { useCallback, useRef, useMemo } from 'react';
import Typography from '@mui/material/Typography';
-import { IconButton, MenuItem, MenuList } from '@mui/material';
-import { ReactComponent as DeleteSvg } from '$app/assets/delete.svg';
-import { ReactComponent as MoreSvg } from '$app/assets/more.svg';
-import { PopoverNoBackdropProps } from '$app/components/editor/components/tools/popover';
-import Tooltip from '@mui/material/Tooltip';
-import { useAppSelector } from '$app/stores/store';
-import { ThemeMode } from '$app_reducers/current-user/slice';
+import KeyboardNavigation, {
+ KeyboardNavigationOption,
+} from '$app/components/_shared/keyboard_navigation/KeyboardNavigation';
+import { useTranslation } from 'react-i18next';
+import { TitleOutlined } from '@mui/icons-material';
+import { EditorMarkFormat } from '$app/application/document/document.types';
export interface ColorPickerProps {
- onFocus?: () => void;
- onBlur?: () => void;
- label?: string;
- color?: string;
- onChange?: (color: string) => void;
+ onChange?: (format: EditorMarkFormat.FontColor | EditorMarkFormat.BgColor, color: string) => void;
+ onEscape?: () => void;
+ disableFocus?: boolean;
}
-export function ColorPicker({ onFocus, onBlur, label, color = '', onChange }: ColorPickerProps) {
+export function ColorPicker({ onEscape, onChange, disableFocus }: ColorPickerProps) {
const { t } = useTranslation();
- const isDark = useAppSelector((state) => state.currentUser?.userSetting?.themeMode === ThemeMode.Dark);
- const [selectedColor, setSelectedColor] = useState(color);
-
- useEffect(() => {
- setSelectedColor(color);
- }, [color]);
- const handleColorChange = (color: string) => {
- setSelectedColor(color);
- onChange?.(color);
- };
+ const ref = useRef<HTMLDivElement>(null);
- const colors = useMemo(() => {
- return !isDark
- ? [
- '#e8e0ff',
- '#ffd6e8',
- '#f5d0ff',
- '#e1fbff',
- '#ffebcc',
- '#fff7cc',
- '#e8ffcc',
- '#e8f4ff',
- '#fff2cd',
- '#d9d9d9',
- '#f0f0f0',
- ]
- : [
- '#4D4078',
- '#7B2CBF',
- '#FFB800',
- '#00B800',
- '#00B8FF',
- '#007BFF',
- '#B800FF',
- '#FF00B8',
- '#FF0000',
- '#FF6C00',
- '#FFD800',
- ];
- }, [isDark]);
+ const handleColorChange = useCallback(
+ (key: string) => {
+ const [format, , color = ''] = key.split('-');
+ const formatKey = format === 'font' ? EditorMarkFormat.FontColor : EditorMarkFormat.BgColor;
- const [openCustomColorPicker, setOpenCustomColorPicker] = useState(false);
-
- const customItemRef = useRef<HTMLLIElement | null>(null);
-
- useEffect(() => {
- if (openCustomColorPicker) {
- onFocus?.();
- } else {
- onBlur?.();
- }
- }, [openCustomColorPicker, onFocus, onBlur]);
+ onChange?.(formatKey, color);
+ },
+ [onChange]
+ );
- return (
- <div className={'flex min-w-[150px] flex-col'}>
- <Typography className={'px-3 pt-3 text-text-caption'} variant='subtitle2'>
- {label}
- </Typography>
- <MenuList disabledItemsFocusable={true}>
- <MenuItem
- onMouseEnter={() => {
- setOpenCustomColorPicker(true);
- }}
- onMouseLeave={() => {
- setOpenCustomColorPicker(false);
+ const renderColorItem = useCallback(
+ (name: string, color: string, backgroundColor?: string) => {
+ return (
+ <div
+ key={name}
+ onClick={() => {
+ handleColorChange(backgroundColor ? backgroundColor : color);
}}
- className={'mx-2 mb-2 flex px-2.5 py-1'}
- ref={customItemRef}
+ className={'flex w-full cursor-pointer items-center justify-center gap-2'}
>
- <div className={'flex-1 text-xs'}>{t('colors.custom')}</div>
- <MoreSvg className={'h-4 w-4'} />
- {openCustomColorPicker && (
- <CustomColorPicker
- anchorEl={customItemRef.current}
- open={openCustomColorPicker}
- onColorChange={handleColorChange}
- onMouseDown={(e) => e.stopPropagation()}
- {...PopoverNoBackdropProps}
- onClose={() => {
- setOpenCustomColorPicker(false);
- }}
- onMouseUp={(e) => {
- // prevent editor blur
- e.stopPropagation();
- }}
- anchorOrigin={{
- vertical: 'top',
- horizontal: 'right',
- }}
- transformOrigin={{
- vertical: 'top',
- horizontal: 'left',
- }}
- />
- )}
- </MenuItem>
- <div className={'flex flex-grow flex-wrap items-center gap-1 px-3 py-0'}>
- <Tooltip title={t('colors.default')}>
- <IconButton
- className={'rounded-full'}
- onClick={() => {
- handleColorChange('');
- }}
- >
- <DeleteSvg />
- </IconButton>
- </Tooltip>
- {colors.map((c) => {
- return (
- <IconButton
- key={c}
- onClick={() => {
- handleColorChange(c);
- }}
- className={'flex h-6 w-6 cursor-pointer items-center justify-center rounded-full p-1'}
- style={{
- backgroundColor: c === selectedColor ? 'var(--content-blue-100)' : undefined,
- }}
- >
- <div
- className={'h-full w-full rounded-full'}
- style={{
- backgroundColor: c,
- }}
- />
- </IconButton>
- );
- })}
+ <div
+ style={{
+ backgroundColor: backgroundColor ?? 'transparent',
+ color: color === '' ? 'var(--text-title)' : color,
+ }}
+ className={'flex h-5 w-5 items-center justify-center rounded border border-line-divider'}
+ >
+ <TitleOutlined className={'h-4 w-4'} />
+ </div>
+ <div className={'flex-1 text-xs text-text-title'}>{name}</div>
</div>
- </MenuList>
+ );
+ },
+ [handleColorChange]
+ );
+
+ const colors: KeyboardNavigationOption[] = useMemo(() => {
+ return [
+ {
+ key: 'font_color',
+ content: (
+ <Typography className={'px-3 pb-1 pt-3 text-text-caption'} variant='subtitle2'>
+ {t('editor.textColor')}
+ </Typography>
+ ),
+ children: [
+ {
+ key: 'font-default',
+ content: renderColorItem(t('editor.fontColorDefault'), ''),
+ },
+ {
+ key: `font-gray-rgb(120, 119, 116)`,
+ content: renderColorItem(t('editor.fontColorGray'), 'rgb(120, 119, 116)'),
+ },
+ {
+ key: 'font-brown-rgb(159, 107, 83)',
+ content: renderColorItem(t('editor.fontColorBrown'), 'rgb(159, 107, 83)'),
+ },
+ {
+ key: 'font-orange-rgb(217, 115, 13)',
+ content: renderColorItem(t('editor.fontColorOrange'), 'rgb(217, 115, 13)'),
+ },
+ {
+ key: 'font-yellow-rgb(203, 145, 47)',
+ content: renderColorItem(t('editor.fontColorYellow'), 'rgb(203, 145, 47)'),
+ },
+ {
+ key: 'font-green-rgb(68, 131, 97)',
+ content: renderColorItem(t('editor.fontColorGreen'), 'rgb(68, 131, 97)'),
+ },
+ {
+ key: 'font-blue-rgb(51, 126, 169)',
+ content: renderColorItem(t('editor.fontColorBlue'), 'rgb(51, 126, 169)'),
+ },
+ {
+ key: 'font-purple-rgb(144, 101, 176)',
+ content: renderColorItem(t('editor.fontColorPurple'), 'rgb(144, 101, 176)'),
+ },
+ {
+ key: 'font-pink-rgb(193, 76, 138)',
+ content: renderColorItem(t('editor.fontColorPink'), 'rgb(193, 76, 138)'),
+ },
+ {
+ key: 'font-red-rgb(212, 76, 71)',
+ content: renderColorItem(t('editor.fontColorRed'), 'rgb(212, 76, 71)'),
+ },
+ ],
+ },
+ {
+ key: 'bg_color',
+ content: (
+ <Typography className={'px-3 pb-1 pt-3 text-text-caption'} variant='subtitle2'>
+ {t('editor.backgroundColor')}
+ </Typography>
+ ),
+ children: [
+ {
+ key: 'bg-default',
+ content: renderColorItem(t('editor.backgroundColorDefault'), '', ''),
+ },
+ {
+ key: `bg-gray-rgba(161,161,159,0.61)`,
+ content: renderColorItem(t('editor.backgroundColorGray'), '', 'rgba(161,161,159,0.61)'),
+ },
+ {
+ key: `bg-brown-rgba(178,93,37,0.65)`,
+ content: renderColorItem(t('editor.backgroundColorBrown'), '', 'rgba(178,93,37,0.65)'),
+ },
+ {
+ key: `bg-orange-rgba(248,156,71,0.65)`,
+ content: renderColorItem(t('editor.backgroundColorOrange'), '', 'rgba(248,156,71,0.65)'),
+ },
+ {
+ key: `bg-yellow-rgba(229,197,137,0.6)`,
+ content: renderColorItem(t('editor.backgroundColorYellow'), '', 'rgba(229,197,137,0.6)'),
+ },
+ {
+ key: `bg-green-rgba(124,189,111,0.65)`,
+ content: renderColorItem(t('editor.backgroundColorGreen'), '', 'rgba(124,189,111,0.65)'),
+ },
+ {
+ key: `bg-blue-rgba(100,174,199,0.71)`,
+ content: renderColorItem(t('editor.backgroundColorBlue'), '', 'rgba(100,174,199,0.71)'),
+ },
+ {
+ key: `bg-purple-rgba(182,114,234,0.63)`,
+ content: renderColorItem(t('editor.backgroundColorPurple'), '', 'rgba(182,114,234,0.63)'),
+ },
+ {
+ key: `bg-pink-rgba(238,142,179,0.6)`,
+ content: renderColorItem(t('editor.backgroundColorPink'), '', 'rgba(238,142,179,0.6)'),
+ },
+ {
+ key: `bg-red-rgba(238,88,98,0.64)`,
+ content: renderColorItem(t('editor.backgroundColorRed'), '', 'rgba(238,88,98,0.64)'),
+ },
+ ],
+ },
+ ];
+ }, [renderColorItem, t]);
+
+ return (
+ <div ref={ref} className={'flex h-full max-h-[360px] w-full flex-col overflow-y-auto'}>
+ <KeyboardNavigation
+ disableFocus={disableFocus}
+ onPressLeft={onEscape}
+ scrollRef={ref}
+ options={colors}
+ onConfirm={handleColorChange}
+ onEscape={onEscape}
+ />
</div>
);
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/_shared/CustomColorPicker.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/_shared/CustomColorPicker.tsx
index edded7f874..ae463a2ff3 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/_shared/CustomColorPicker.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/_shared/CustomColorPicker.tsx
@@ -1,8 +1,7 @@
import React, { useState } from 'react';
import Popover, { PopoverProps } from '@mui/material/Popover';
-import { RGBColor, SketchPicker } from 'react-color';
-import Button from '@mui/material/Button';
-import { useTranslation } from 'react-i18next';
+import { Color, SketchPicker } from 'react-color';
+
import { Divider } from '@mui/material';
export function CustomColorPicker({
@@ -11,29 +10,18 @@ export function CustomColorPicker({
}: {
onColorChange?: (color: string) => void;
} & PopoverProps) {
- const { t } = useTranslation();
- const [color, setColor] = useState<RGBColor | undefined>();
+ const [color, setColor] = useState<Color | undefined>();
return (
<Popover {...props}>
<SketchPicker
onChange={(color) => {
setColor(color.rgb);
+ onColorChange?.(color.hex);
}}
color={color}
/>
<Divider />
- <div className={'z-10 flex justify-end bg-bg-body px-2 py-2'}>
- <Button
- size={'small'}
- onClick={() => {
- onColorChange?.(`rgba(${color?.r}, ${color?.g}, ${color?.b}, ${color?.a})`);
- }}
- variant={'outlined'}
- >
- {t('button.done')}
- </Button>
- </div>
</Popover>
);
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/_shared/FontColorPicker.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/_shared/FontColorPicker.tsx
deleted file mode 100644
index a931f8bb13..0000000000
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/_shared/FontColorPicker.tsx
+++ /dev/null
@@ -1,9 +0,0 @@
-import React from 'react';
-import { useTranslation } from 'react-i18next';
-import { ColorPicker, ColorPickerProps } from '$app/components/editor/components/tools/_shared/ColorPicker';
-
-export function FontColorPicker(props: ColorPickerProps) {
- const { t } = useTranslation();
-
- return <ColorPicker {...props} label={t('editor.textColor')} />;
-}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/_shared/index.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/_shared/index.ts
index caf74b0e96..00e212aa7f 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/_shared/index.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/_shared/index.ts
@@ -1,4 +1,2 @@
export * from './CustomColorPicker';
-export * from './FontColorPicker';
export * from './ColorPicker';
-export * from './BgColorPicker';
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/AddBlockBelow.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/AddBlockBelow.tsx
index 81e2255ed8..eb2675bc71 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/AddBlockBelow.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/AddBlockBelow.tsx
@@ -1,116 +1,54 @@
-import React, { useCallback, useMemo, useState } from 'react';
+import React from 'react';
import { ReactEditor, useSlate } from 'slate-react';
import { IconButton, Tooltip } from '@mui/material';
-import Popover from '@mui/material/Popover';
-import { PopoverPreventBlurProps } from '$app/components/editor/components/tools/popover';
-import SlashCommandPanelContent from '$app/components/editor/components/tools/command_panel/slash_command_panel/SlashCommandPanelContent';
import { ReactComponent as AddSvg } from '$app/assets/add.svg';
import { useTranslation } from 'react-i18next';
-import { Element } from 'slate';
+import { Element, Path } from 'slate';
import { CustomEditor } from '$app/components/editor/command';
import { EditorNodeType } from '$app/application/document/document.types';
+import { YjsEditor } from '@slate-yjs/core';
+import { useSlashState } from '$app/components/editor/stores';
function AddBlockBelow({ node }: { node?: Element }) {
const { t } = useTranslation();
- const [nodeEl, setNodeEl] = useState<HTMLElement | null>(null);
const editor = useSlate();
- const openSlashCommandPanel = useMemo(() => !!nodeEl, [nodeEl]);
-
- const handleSlashCommandPanelClose = useCallback(
- (deleteText?: boolean) => {
- if (!nodeEl) return;
- const node = ReactEditor.toSlateNode(editor, nodeEl) as Element;
-
- if (!node) return;
-
- if (deleteText) {
- CustomEditor.deleteAllText(editor, node);
- }
-
- setNodeEl(null);
- },
- [editor, nodeEl]
- );
+ const { setOpen: setSlashOpen } = useSlashState();
const handleAddBelow = () => {
if (!node) return;
ReactEditor.focus(editor);
- const [textNode] = node.children as Element[];
- const hasTextNode = textNode && textNode.type === EditorNodeType.Text;
-
const nodePath = ReactEditor.findPath(editor, node);
- const textPath = ReactEditor.findPath(editor, textNode);
+ const nextPath = Path.next(nodePath);
- const focusPath = hasTextNode ? textPath : nodePath;
+ editor.select(nodePath);
- editor.select(focusPath);
- editor.collapse({
- edge: 'end',
- });
+ if (editor.isSelectable(node)) {
+ editor.collapse({
+ edge: 'start',
+ });
+ }
const isEmptyNode = CustomEditor.isEmptyText(editor, node);
- if (isEmptyNode) {
- const nodeDom = ReactEditor.toDOMNode(editor, node);
-
- setNodeEl(nodeDom);
- return;
+ // if the node is not a paragraph, or it is not empty, insert a new empty line
+ if (node.type !== EditorNodeType.Paragraph || !isEmptyNode) {
+ CustomEditor.insertEmptyLine(editor as ReactEditor & YjsEditor, nextPath);
+ editor.select(nextPath);
}
- editor.insertBreak();
- CustomEditor.turnToBlock(editor, {
- type: EditorNodeType.Paragraph,
- });
-
requestAnimationFrame(() => {
- const block = CustomEditor.getBlock(editor);
-
- if (block) {
- const [node] = block;
-
- const nodeDom = ReactEditor.toDOMNode(editor, node);
-
- setNodeEl(nodeDom);
- }
+ setSlashOpen(true);
});
};
- const searchText = useMemo(() => {
- if (!nodeEl) return '';
- const node = ReactEditor.toSlateNode(editor, nodeEl) as Element;
-
- if (!node) return '';
-
- return CustomEditor.getNodeText(editor, node);
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [editor, nodeEl, editor.selection]);
-
return (
<>
- <Tooltip title={t('blockActions.addBelowTooltip')}>
+ <Tooltip disableInteractive={true} title={t('blockActions.addBelowTooltip')}>
<IconButton onClick={handleAddBelow} size={'small'}>
<AddSvg />
</IconButton>
</Tooltip>
- {openSlashCommandPanel && (
- <Popover
- {...PopoverPreventBlurProps}
- anchorOrigin={{
- vertical: 30,
- horizontal: 'left',
- }}
- transformOrigin={{
- vertical: 'top',
- horizontal: 'left',
- }}
- open={openSlashCommandPanel}
- anchorEl={nodeEl}
- onClose={() => handleSlashCommandPanelClose(false)}
- >
- <SlashCommandPanelContent searchText={searchText} closePanel={handleSlashCommandPanelClose} />
- </Popover>
- )}
</>
);
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockActions.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockActions.tsx
index 7b08b151fd..f5833e538b 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockActions.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockActions.tsx
@@ -2,13 +2,27 @@ import React from 'react';
import { Element } from 'slate';
import AddBlockBelow from '$app/components/editor/components/tools/block_actions/AddBlockBelow';
-import BlockMenu from '$app/components/editor/components/tools/block_actions/BlockMenu';
+import { ReactComponent as DragSvg } from '$app/assets/drag.svg';
+import { IconButton, Tooltip } from '@mui/material';
+import { useTranslation } from 'react-i18next';
+
+export function BlockActions({
+ node,
+ onClickDrag,
+}: {
+ node?: Element;
+ onClickDrag: (e: React.MouseEvent<HTMLButtonElement>) => void;
+}) {
+ const { t } = useTranslation();
-export function BlockActions({ node, setMenuVisible }: { node?: Element; setMenuVisible: (visible: boolean) => void }) {
return (
<>
<AddBlockBelow node={node} />
- <BlockMenu setMenuVisible={setMenuVisible} node={node} />
+ <Tooltip disableInteractive={true} title={t('blockActions.openMenuTooltip')}>
+ <IconButton onClick={onClickDrag} size={'small'}>
+ <DragSvg />
+ </IconButton>
+ </Tooltip>
</>
);
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockActionsToolbar.hooks.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockActionsToolbar.hooks.ts
index 8633b9f3fa..b35239ae21 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockActionsToolbar.hooks.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockActionsToolbar.hooks.ts
@@ -1,13 +1,36 @@
-import { useEffect, useState } from 'react';
+import { RefObject, useCallback, useEffect, useState } from 'react';
import { ReactEditor, useSlate } from 'slate-react';
-import { getBlockActionsPosition } from '$app/components/editor/components/tools/block_actions/utils';
-import { Element, Editor } from 'slate';
+import { findEventRange, getBlockActionsPosition } from '$app/components/editor/components/tools/block_actions/utils';
+import { Element, Editor, Range } from 'slate';
import { EditorNodeType } from '$app/application/document/document.types';
-export function useBlockActionsToolbar(ref: React.RefObject<HTMLDivElement>) {
+export function useBlockActionsToolbar(ref: RefObject<HTMLDivElement>, contextMenuVisible: boolean) {
const editor = useSlate();
const [node, setNode] = useState<Element | null>(null);
- const [menuVisible, setMenuVisible] = useState(false);
+
+ const recalculatePosition = useCallback(
+ (blockElement: HTMLElement) => {
+ const { top, left } = getBlockActionsPosition(editor, blockElement);
+
+ const slateEditorDom = ReactEditor.toDOMNode(editor, editor);
+
+ if (!ref.current) return;
+
+ ref.current.style.top = `${top + slateEditorDom.offsetTop}px`;
+ ref.current.style.left = `${left + slateEditorDom.offsetLeft - 64}px`;
+ },
+ [editor, ref]
+ );
+
+ const close = useCallback(() => {
+ const el = ref.current;
+
+ if (!el) return;
+
+ el.style.opacity = '0';
+ el.style.pointerEvents = 'none';
+ setNode(null);
+ }, [ref]);
useEffect(() => {
const handleMouseMove = (e: MouseEvent) => {
@@ -21,7 +44,13 @@ export function useBlockActionsToolbar(ref: React.RefObject<HTMLDivElement>) {
return;
}
- const range = ReactEditor.findEventRange(editor, e);
+ let range: Range | null = null;
+
+ try {
+ range = ReactEditor.findEventRange(editor, e);
+ } catch {
+ range = findEventRange(editor, e);
+ }
if (!range) return;
const match = editor.above({
@@ -32,9 +61,7 @@ export function useBlockActionsToolbar(ref: React.RefObject<HTMLDivElement>) {
});
if (!match) {
- el.style.opacity = '0';
- el.style.pointerEvents = 'none';
- setNode(null);
+ close();
return;
}
@@ -44,48 +71,48 @@ export function useBlockActionsToolbar(ref: React.RefObject<HTMLDivElement>) {
const blockElement = ReactEditor.toDOMNode(editor, node);
if (!blockElement) return;
-
- const { top, left } = getBlockActionsPosition(editor, blockElement);
-
- const slateEditorDom = ReactEditor.toDOMNode(editor, editor);
-
+ recalculatePosition(blockElement);
el.style.opacity = '1';
el.style.pointerEvents = 'auto';
- el.style.top = `${top + slateEditorDom.offsetTop}px`;
- el.style.left = `${left + slateEditorDom.offsetLeft - 64}px`;
const slateNode = ReactEditor.toSlateNode(editor, blockElement) as Element;
setNode(slateNode);
};
- const handleMouseLeave = (_e: MouseEvent) => {
- const el = ref.current;
+ const dom = ReactEditor.toDOMNode(editor, editor);
- if (!el) return;
+ if (!contextMenuVisible) {
+ dom.addEventListener('mousemove', handleMouseMove);
+ dom.parentElement?.addEventListener('mouseleave', close);
+ }
- el.style.opacity = '0';
- el.style.pointerEvents = 'none';
- setNode(null);
+ return () => {
+ dom.removeEventListener('mousemove', handleMouseMove);
+ dom.parentElement?.removeEventListener('mouseleave', close);
};
+ }, [close, editor, contextMenuVisible, ref, recalculatePosition]);
- const dom = ReactEditor.toDOMNode(editor, editor);
+ useEffect(() => {
+ let observer: MutationObserver | null = null;
- if (!menuVisible) {
- dom.addEventListener('mousemove', handleMouseMove);
- dom.parentElement?.addEventListener('mouseleave', handleMouseLeave);
- } else {
- dom.removeEventListener('mousemove', handleMouseMove);
- dom.parentElement?.removeEventListener('mouseleave', handleMouseLeave);
+ if (node) {
+ const dom = ReactEditor.toDOMNode(editor, node);
+
+ if (dom.parentElement) {
+ observer = new MutationObserver(close);
+
+ observer.observe(dom.parentElement, {
+ childList: true,
+ });
+ }
}
return () => {
- dom.removeEventListener('mousemove', handleMouseMove);
- dom.parentElement?.removeEventListener('mouseleave', handleMouseLeave);
+ observer?.disconnect();
};
- }, [editor, ref, menuVisible]);
+ }, [close, editor, node]);
return {
- setMenuVisible,
node: node?.type === EditorNodeType.Page ? null : node,
};
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockActionsToolbar.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockActionsToolbar.tsx
index c74fd93ab3..11e41246e5 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockActionsToolbar.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockActionsToolbar.tsx
@@ -1,33 +1,103 @@
-import React, { useRef } from 'react';
+import React, { useCallback, useContext, useEffect, useRef, useState } from 'react';
import { useBlockActionsToolbar } from './BlockActionsToolbar.hooks';
import BlockActions from '$app/components/editor/components/tools/block_actions/BlockActions';
import { getBlockCssProperty } from '$app/components/editor/components/tools/block_actions/utils';
+import BlockOperationMenu from '$app/components/editor/components/tools/block_actions/BlockOperationMenu';
+import { ReactEditor, useSlateStatic } from 'slate-react';
+import { PopoverProps } from '@mui/material/Popover';
-export function BlockActionsToolbar() {
+import { EditorSelectedBlockContext } from '$app/components/editor/stores/selected';
+import withErrorBoundary from '$app/components/_shared/error_boundary/withError';
+
+const Toolbar = () => {
const ref = useRef<HTMLDivElement | null>(null);
+ const [openContextMenu, setOpenContextMenu] = useState(false);
+ const { node } = useBlockActionsToolbar(ref, openContextMenu);
+ const cssProperty = node && getBlockCssProperty(node);
+ const selectedBlockContext = useContext(EditorSelectedBlockContext);
+ const popoverPropsRef = useRef<Partial<PopoverProps> | undefined>(undefined);
+ const editor = useSlateStatic();
- const { node, setMenuVisible } = useBlockActionsToolbar(ref);
+ const handleOpen = useCallback(() => {
+ if (!node || !node.blockId) return;
+ setOpenContextMenu(true);
+ selectedBlockContext.clear();
+ selectedBlockContext.add(node.blockId);
+ }, [node, selectedBlockContext]);
- const cssProperty = node && getBlockCssProperty(node);
+ const handleClose = useCallback(() => {
+ setOpenContextMenu(false);
+ selectedBlockContext.clear();
+ }, [selectedBlockContext]);
+
+ useEffect(() => {
+ if (!node) return;
+ const nodeDom = ReactEditor.toDOMNode(editor, node);
+ const onContextMenu = (e: MouseEvent) => {
+ e.preventDefault();
+ e.stopPropagation();
+ const { clientX, clientY } = e;
+
+ popoverPropsRef.current = {
+ transformOrigin: {
+ vertical: 'top',
+ horizontal: 'left',
+ },
+ anchorReference: 'anchorPosition',
+ anchorPosition: {
+ top: clientY,
+ left: clientX,
+ },
+ };
+ handleOpen();
+ };
+
+ nodeDom.addEventListener('contextmenu', onContextMenu);
+
+ return () => {
+ nodeDom.removeEventListener('contextmenu', onContextMenu);
+ };
+ }, [editor, handleOpen, node]);
return (
- <div
- ref={ref}
- contentEditable={false}
- className={`block-actions ${cssProperty} absolute z-10 flex min-h-[26px] w-[64px] flex-grow transform items-center justify-end px-1 opacity-0 transition-opacity`}
- onMouseDown={(e) => {
- // prevent toolbar from taking focus away from editor
- e.preventDefault();
- e.stopPropagation();
- }}
- onMouseUp={(e) => {
- e.stopPropagation();
- }}
- >
- {/* Ensure the toolbar in middle */}
- <div className={`invisible`}>$</div>
- {<BlockActions setMenuVisible={setMenuVisible} node={node || undefined} />}
- </div>
+ <>
+ <div
+ ref={ref}
+ contentEditable={false}
+ className={`block-actions absolute z-10 flex w-[64px] flex-grow transform items-center justify-end px-1 opacity-0 ${cssProperty}`}
+ >
+ {/* Ensure the toolbar in middle */}
+ <div className={`invisible`}>$</div>
+ {
+ <BlockActions
+ node={node || undefined}
+ onClickDrag={(e: React.MouseEvent<HTMLButtonElement>) => {
+ const target = e.currentTarget;
+ const rect = target.getBoundingClientRect();
+
+ popoverPropsRef.current = {
+ transformOrigin: {
+ vertical: 'center',
+ horizontal: 'right',
+ },
+ anchorReference: 'anchorPosition',
+ anchorPosition: {
+ top: rect.top + rect.height / 2,
+ left: rect.left,
+ },
+ };
+
+ handleOpen();
+ }}
+ />
+ }
+ </div>
+ {node && openContextMenu && (
+ <BlockOperationMenu node={node} open={openContextMenu} onClose={handleClose} {...popoverPropsRef.current} />
+ )}
+ </>
);
-}
+};
+
+export const BlockActionsToolbar = withErrorBoundary(Toolbar);
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockMenu.hooks.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockMenu.hooks.ts
deleted file mode 100644
index ebbea336ce..0000000000
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockMenu.hooks.ts
+++ /dev/null
@@ -1,27 +0,0 @@
-import { useCallback, KeyboardEvent } from 'react';
-
-export function useBlockMenuKeyDown({ onClose }: { onClose: () => void }) {
- const onKeyDown = useCallback(
- (e: KeyboardEvent) => {
- e.preventDefault();
- e.stopPropagation();
- switch (e.key) {
- case 'Escape':
- onClose();
- break;
- case 'ArrowUp':
- case 'ArrowDown':
- case 'ArrowLeft':
- case 'ArrowRight':
- break;
- default:
- return;
- }
- },
- [onClose]
- );
-
- return {
- onKeyDown,
- };
-}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockMenu.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockMenu.tsx
deleted file mode 100644
index 969fd35b9b..0000000000
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockMenu.tsx
+++ /dev/null
@@ -1,72 +0,0 @@
-import React, { useCallback, useContext, useEffect, useRef, useState } from 'react';
-import { IconButton, Tooltip } from '@mui/material';
-import { useTranslation } from 'react-i18next';
-import { ReactComponent as DragSvg } from '$app/assets/drag.svg';
-import BlockOperationMenu from '$app/components/editor/components/tools/block_actions/BlockOperationMenu';
-import { Element } from 'slate';
-import { EditorSelectedBlockContext } from '$app/components/editor/components/editor/Editor.hooks';
-
-function BlockMenu({ node, setMenuVisible }: { node?: Element; setMenuVisible: (visible: boolean) => void }) {
- const dragBtnRef = useRef<HTMLButtonElement>(null);
- const [openMenu, setOpenMenu] = useState(false);
- const { t } = useTranslation();
- const [selectedNode, setSelectedNode] = useState<Element>();
- const selectedBlockContext = useContext(EditorSelectedBlockContext);
-
- useEffect(() => {
- if (openMenu) {
- setMenuVisible(true);
- } else {
- setMenuVisible(false);
- }
- }, [openMenu, setMenuVisible]);
- const handleClick = useCallback(
- (e: React.MouseEvent) => {
- e.stopPropagation();
- setOpenMenu(true);
- if (!node || !node.blockId) return;
- setSelectedNode(node);
- selectedBlockContext.clear();
- selectedBlockContext.add(node.blockId);
- },
- [node, selectedBlockContext]
- );
-
- return (
- <>
- <Tooltip title={t('blockActions.openMenuTooltip')}>
- <IconButton onClick={handleClick} ref={dragBtnRef} size={'small'}>
- <DragSvg />
- </IconButton>
- </Tooltip>
- {openMenu && selectedNode && (
- <BlockOperationMenu
- onMouseMove={(e) => {
- e.stopPropagation();
- }}
- anchorOrigin={{
- vertical: 'center',
- horizontal: 'left',
- }}
- transformOrigin={{
- vertical: 'center',
- horizontal: 'right',
- }}
- PaperProps={{
- onClick: (e) => {
- e.stopPropagation();
- },
- }}
- node={selectedNode}
- open={openMenu}
- anchorEl={dragBtnRef.current}
- onClose={() => {
- setOpenMenu(false);
- }}
- />
- )}
- </>
- );
-}
-
-export default BlockMenu;
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockOperationMenu.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockOperationMenu.tsx
index 9bc3782aa6..15dc3214ef 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockOperationMenu.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/BlockOperationMenu.tsx
@@ -3,13 +3,29 @@ import Popover, { PopoverProps } from '@mui/material/Popover';
import { ReactComponent as DeleteSvg } from '$app/assets/delete.svg';
import { ReactComponent as CopySvg } from '$app/assets/copy.svg';
import { useTranslation } from 'react-i18next';
-import { Button, Divider } from '@mui/material';
+import { Divider } from '@mui/material';
import { PopoverCommonProps } from '$app/components/editor/components/tools/popover';
-import { Element } from 'slate';
+import { Element, Path } from 'slate';
import { ReactEditor, useSlateStatic } from 'slate-react';
import { CustomEditor } from '$app/components/editor/command';
-import { useBlockMenuKeyDown } from '$app/components/editor/components/tools/block_actions/BlockMenu.hooks';
-import { Color } from './color';
+import KeyboardNavigation, {
+ KeyboardNavigationOption,
+} from '$app/components/_shared/keyboard_navigation/KeyboardNavigation';
+import { Color } from '$app/components/editor/components/tools/block_actions/color';
+import { getModifier } from '$app/components/editor/plugins/shortcuts';
+import isHotkey from 'is-hotkey';
+import { EditorNodeType } from '$app/application/document/document.types';
+import { EditorSelectedBlockContext } from '$app/components/editor/stores/selected';
+
+export const canSetColorBlocks: EditorNodeType[] = [
+ EditorNodeType.Paragraph,
+ EditorNodeType.HeadingBlock,
+ EditorNodeType.TodoListBlock,
+ EditorNodeType.BulletedListBlock,
+ EditorNodeType.NumberedListBlock,
+ EditorNodeType.ToggleListBlock,
+ EditorNodeType.QuoteBlock,
+];
export function BlockOperationMenu({
node,
@@ -20,80 +36,170 @@ export function BlockOperationMenu({
const editor = useSlateStatic();
const { t } = useTranslation();
+ const canSetColor = useMemo(() => {
+ return canSetColorBlocks.includes(node.type as EditorNodeType);
+ }, [node]);
+ const selectedBlockContext = React.useContext(EditorSelectedBlockContext);
+ const [openColorMenu, setOpenColorMenu] = React.useState(false);
+ const ref = React.useRef<HTMLDivElement>(null);
const handleClose = useCallback(() => {
props.onClose?.({}, 'backdropClick');
ReactEditor.focus(editor);
- const path = ReactEditor.findPath(editor, node);
+ try {
+ const path = ReactEditor.findPath(editor, node);
- editor.select(path);
- if (editor.isSelectable(node)) {
- editor.collapse({
- edge: 'start',
- });
+ editor.select(path);
+ } catch (e) {
+ // do nothing
}
- }, [editor, node, props]);
- const { onKeyDown } = useBlockMenuKeyDown({
- onClose: handleClose,
- });
+ editor.collapse({
+ edge: 'start',
+ });
+ }, [editor, node, props]);
- const operationOptions = useMemo(
- () => [
- {
- icon: <DeleteSvg />,
- text: t('button.delete'),
- onClick: () => {
+ const onConfirm = useCallback(
+ (optionKey: string) => {
+ switch (optionKey) {
+ case 'delete': {
CustomEditor.deleteNode(editor, node);
+ break;
+ }
+
+ case 'duplicate': {
+ const path = ReactEditor.findPath(editor, node);
+ const newNode = CustomEditor.duplicateNode(editor, node);
+
handleClose();
+
+ const newBlockId = newNode.blockId;
+
+ if (!newBlockId) return;
+ requestAnimationFrame(() => {
+ selectedBlockContext.clear();
+ selectedBlockContext.add(newBlockId);
+ const nextPath = Path.next(path);
+
+ editor.select(nextPath);
+ editor.collapse({
+ edge: 'start',
+ });
+ });
+ return;
+ }
+
+ case 'color': {
+ setOpenColorMenu(true);
+ return;
+ }
+ }
+
+ handleClose();
+ },
+ [editor, handleClose, node, selectedBlockContext]
+ );
+
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore
+ const options: KeyboardNavigationOption[] = useMemo(
+ () =>
+ [
+ {
+ key: 'block-operation',
+ children: [
+ {
+ key: 'delete',
+ content: (
+ <div className={'flex w-full items-center justify-between gap-2'}>
+ <DeleteSvg className={'h-5 w-5'} />
+ <div className={'flex-1'}>{t('button.delete')}</div>
+ <div className={'text-right text-text-caption'}>{'Del'}</div>
+ </div>
+ ),
+ },
+ {
+ key: 'duplicate',
+ content: (
+ <div className={'flex w-full items-center justify-between gap-2'}>
+ <CopySvg className={'h-5 w-5'} />
+ <div className={'flex-1'}>{t('button.duplicate')}</div>
+ <div className={'text-right text-text-caption'}>{`${getModifier()} + D`}</div>
+ </div>
+ ),
+ },
+ ],
},
- },
- {
- icon: <CopySvg />,
- text: t('button.duplicate'),
- onClick: () => {
- CustomEditor.duplicateNode(editor, node);
- handleClose();
+ canSetColor && {
+ key: 'color',
+ content: <Divider />,
+ children: [
+ {
+ key: 'color',
+ content: (
+ <Color
+ node={
+ node as {
+ data?: {
+ font_color?: string;
+ bg_color?: string;
+ };
+ } & Element
+ }
+ onClosePicker={() => {
+ setOpenColorMenu(false);
+ }}
+ openPicker={openColorMenu}
+ onOpenPicker={() => setOpenColorMenu(true)}
+ />
+ ),
+ },
+ ],
},
- },
- ],
- [editor, node, handleClose, t]
+ ].filter(Boolean),
+ [node, canSetColor, openColorMenu, t]
+ );
+
+ const handleKeyDown = useCallback(
+ (e: KeyboardEvent) => {
+ e.stopPropagation();
+ if (isHotkey('mod+d', e)) {
+ e.preventDefault();
+ onConfirm('duplicate');
+ }
+
+ if (isHotkey('del', e) || isHotkey('backspace', e)) {
+ e.preventDefault();
+ onConfirm('delete');
+ }
+ },
+ [onConfirm]
);
return (
<Popover
{...PopoverCommonProps}
disableAutoFocus={false}
- onKeyDown={onKeyDown}
onMouseDown={(e) => e.stopPropagation()}
{...props}
onClose={handleClose}
>
- <div className={'flex flex-col p-2'}>
- {operationOptions.map((option, index) => (
- <Button
- color={'inherit'}
- onClick={option.onClick}
- startIcon={option.icon}
- size={'small'}
- className={'w-full justify-start'}
- key={index}
- >
- {option.text}
- </Button>
- ))}
+ <div className={'max-h-[360px] w-full overflow-y-auto py-1'} ref={ref}>
+ <KeyboardNavigation
+ onKeyDown={handleKeyDown}
+ onPressLeft={handleClose}
+ onPressRight={(key) => {
+ if (key === 'color') {
+ onConfirm(key);
+ } else {
+ handleClose();
+ }
+ }}
+ options={options}
+ scrollRef={ref}
+ onEscape={handleClose}
+ onConfirm={onConfirm}
+ />
</div>
- <Divider className={'my-1'} />
- <Color
- node={
- node as Element & {
- data?: {
- font_color?: string;
- bg_color?: string;
- };
- }
- }
- onClose={handleClose}
- />
</Popover>
);
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/color/Color.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/color/Color.tsx
index 8489539045..d0ad1b2b4b 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/color/Color.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/color/Color.tsx
@@ -1,17 +1,29 @@
-import React, { useCallback, useMemo, useRef, useState } from 'react';
+import React, { useCallback, useRef } from 'react';
import { Element } from 'slate';
-import { Button, Popover } from '@mui/material';
+import { Popover } from '@mui/material';
import ColorLensOutlinedIcon from '@mui/icons-material/ColorLensOutlined';
import { useTranslation } from 'react-i18next';
-import { BgColorPicker, FontColorPicker } from '$app/components/editor/components/tools/_shared';
+import { ColorPicker } from '$app/components/editor/components/tools/_shared';
import { CustomEditor } from '$app/components/editor/command';
import { ReactComponent as MoreSvg } from '$app/assets/more.svg';
import { useSlateStatic } from 'slate-react';
-import { PopoverNoBackdropProps } from '$app/components/editor/components/tools/popover';
+import { PopoverOrigin } from '@mui/material/Popover/Popover';
+
+const initialOrigin: {
+ transformOrigin?: PopoverOrigin;
+ anchorOrigin?: PopoverOrigin;
+} = {
+ anchorOrigin: {
+ vertical: 'top',
+ horizontal: 'right',
+ },
+};
export function Color({
node,
- onClose,
+ openPicker,
+ onOpenPicker,
+ onClosePicker,
}: {
node: Element & {
data?: {
@@ -19,82 +31,56 @@ export function Color({
bg_color?: string;
};
};
- onClose?: () => void;
+ openPicker?: boolean;
+ onOpenPicker?: () => void;
+ onClosePicker?: () => void;
}) {
const { t } = useTranslation();
const editor = useSlateStatic();
- const ref = useRef<HTMLButtonElement>(null);
- const [open, setOpen] = useState(false);
-
- const fontColor = useMemo(() => {
- return node.data?.font_color;
- }, [node]);
-
- const bgColor = useMemo(() => {
- return node.data?.bg_color;
- }, [node]);
+ const ref = useRef<HTMLDivElement>(null);
const onColorChange = useCallback(
(format: 'font_color' | 'bg_color', color: string) => {
CustomEditor.setBlockColor(editor, node, {
[format]: color,
});
- onClose?.();
+ onClosePicker?.();
},
- [editor, node, onClose]
+ [editor, node, onClosePicker]
);
return (
- <Button
- ref={ref}
- color={'inherit'}
- onMouseEnter={() => {
- setOpen(true);
- }}
- onMouseLeave={() => {
- setOpen(false);
- }}
- endIcon={<MoreSvg />}
- startIcon={<ColorLensOutlinedIcon />}
- size={'small'}
- className={'mx-2 my-1 justify-start'}
- >
- {t('editor.color')}
- {open && (
+ <>
+ <div ref={ref} onClick={onOpenPicker} className={'flex w-full items-center justify-between gap-2'}>
+ <ColorLensOutlinedIcon className={'h-5 w-5'} />
+ <div className={'flex-1'}>{t('editor.color')}</div>
+ <MoreSvg className={'h-5 w-5'} />
+ </div>
+ {openPicker && (
<Popover
- {...PopoverNoBackdropProps}
- anchorOrigin={{
- vertical: 'center',
- horizontal: 'right',
+ anchorOrigin={initialOrigin.anchorOrigin}
+ transformOrigin={initialOrigin.transformOrigin}
+ autoFocus={true}
+ open={openPicker}
+ onKeyDown={(e) => {
+ e.stopPropagation();
+ e.nativeEvent.stopImmediatePropagation();
+ if (e.key === 'Escape' || e.key === 'ArrowLeft') {
+ e.preventDefault();
+ onClosePicker?.();
+ }
}}
- PaperProps={{
- ...PopoverNoBackdropProps.PaperProps,
- className: 'w-[200px] max-h-[360px] overflow-x-hidden overflow-y-auto',
- }}
- open={open}
+ onClick={(e) => e.stopPropagation()}
anchorEl={ref.current}
- onClose={() => {
- setOpen(false);
- }}
+ onClose={onClosePicker}
>
<div className={'flex flex-col'}>
- <FontColorPicker
- onChange={(color) => {
- onColorChange('font_color', color);
- }}
- color={fontColor}
- />
- <BgColorPicker
- onChange={(color) => {
- onColorChange('bg_color', color);
- }}
- color={bgColor}
- />
+ <ColorPicker onEscape={onClosePicker} onChange={onColorChange} />
</div>
</Popover>
)}
- </Button>
+ </>
);
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/utils.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/utils.ts
index 9991d03f62..4166022182 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/utils.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/block_actions/utils.ts
@@ -20,9 +20,77 @@ export function getBlockActionsPosition(editor: ReactEditor, blockElement: HTMLE
export function getBlockCssProperty(node: Element) {
switch (node.type) {
case EditorNodeType.HeadingBlock:
- return getHeadingCssProperty((node as HeadingNode).data.level);
+ return `${getHeadingCssProperty((node as HeadingNode).data.level)} mt-1`;
case EditorNodeType.CodeBlock:
case EditorNodeType.CalloutBlock:
- return 'my-2';
+ case EditorNodeType.EquationBlock:
+ case EditorNodeType.GridBlock:
+ return 'my-3';
+ case EditorNodeType.DividerBlock:
+ return 'my-0';
+ default:
+ return 'mt-1';
+ }
+}
+
+/**
+ * Resolve can not find the range when the drop occurs on the icon.
+ * @param editor
+ * @param e
+ */
+export function findEventRange(editor: ReactEditor, e: MouseEvent) {
+ const { clientX: x, clientY: y } = e;
+
+ // Else resolve a range from the caret position where the drop occured.
+ let domRange;
+ const { document } = ReactEditor.getWindow(editor);
+
+ // COMPAT: In Firefox, `caretRangeFromPoint` doesn't exist. (2016/07/25)
+ if (document.caretRangeFromPoint) {
+ domRange = document.caretRangeFromPoint(x, y);
+ } else if ('caretPositionFromPoint' in document && typeof document.caretPositionFromPoint === 'function') {
+ const position = document.caretPositionFromPoint(x, y);
+
+ if (position) {
+ domRange = document.createRange();
+ domRange.setStart(position.offsetNode, position.offset);
+ domRange.setEnd(position.offsetNode, position.offset);
+ }
+ }
+
+ if (domRange && domRange.startContainer) {
+ const startContainer = domRange.startContainer;
+
+ let element: HTMLElement | null = startContainer as HTMLElement;
+ const nodeType = element.nodeType;
+
+ if (nodeType === 3 || typeof element === 'string') {
+ const parent = element.parentElement?.closest('.text-block-icon') as HTMLElement;
+
+ element = parent;
+ }
+
+ if (element && element.nodeType < 3) {
+ if (element.classList?.contains('text-block-icon')) {
+ const sibling = domRange.startContainer.parentElement;
+
+ if (sibling) {
+ domRange.selectNode(sibling);
+ }
+ }
+ }
+ }
+
+ if (!domRange) {
+ return null;
+ }
+
+ try {
+ return ReactEditor.toSlateRange(editor, domRange, {
+ exactMatch: false,
+ suppressThrow: false,
+ });
+ } catch {
+ return null;
}
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/Command.hooks.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/Command.hooks.ts
new file mode 100644
index 0000000000..2697187386
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/Command.hooks.ts
@@ -0,0 +1,297 @@
+import { useEffect, useState, useCallback, useRef } from 'react';
+import { getPanelPosition } from '$app/components/editor/components/tools/command_panel/utils';
+import { useSlate } from 'slate-react';
+import { PopoverPreventBlurProps } from '$app/components/editor/components/tools/popover';
+import { PopoverProps } from '@mui/material/Popover';
+
+import { Editor, Point, Range, Transforms } from 'slate';
+import { CustomEditor } from '$app/components/editor/command';
+import { PopoverOrigin } from '@mui/material/Popover/Popover';
+import { useSlashState } from '$app/components/editor/stores';
+
+export enum EditorCommand {
+ Mention = '@',
+ SlashCommand = '/',
+}
+
+export const PanelPopoverProps: Partial<PopoverProps> = {
+ ...PopoverPreventBlurProps,
+ anchorReference: 'anchorPosition',
+};
+
+const commands = Object.values(EditorCommand);
+
+export interface PanelProps {
+ anchorPosition?: { left: number; top: number; height: number };
+ closePanel: (deleteText?: boolean) => void;
+ searchText: string;
+ openPanel: () => void;
+}
+
+export function useCommandPanel() {
+ const editor = useSlate();
+ const { open: slashOpen, setOpen: setSlashOpen } = useSlashState();
+ const [command, setCommand] = useState<EditorCommand | undefined>(undefined);
+ const [anchorPosition, setAnchorPosition] = useState<
+ | {
+ top: number;
+ left: number;
+ height: number;
+ }
+ | undefined
+ >(undefined);
+ const startPoint = useRef<Point>();
+ const endPoint = useRef<Point>();
+ const open = Boolean(anchorPosition);
+ const [searchText, setSearchText] = useState('');
+
+ const closePanel = useCallback(
+ (deleteText?: boolean) => {
+ if (deleteText && startPoint.current && endPoint.current) {
+ const anchor = {
+ path: startPoint.current.path,
+ offset: startPoint.current.offset - 1,
+ };
+ const focus = {
+ path: endPoint.current.path,
+ offset: endPoint.current.offset,
+ };
+
+ Transforms.delete(editor, {
+ at: {
+ anchor,
+ focus,
+ },
+ });
+ }
+
+ setSlashOpen(false);
+ setCommand(undefined);
+ setAnchorPosition(undefined);
+ setSearchText('');
+ },
+ [editor, setSlashOpen]
+ );
+
+ const setPosition = useCallback(
+ (position?: { left: number; top: number; height: number }) => {
+ if (!position) {
+ closePanel(false);
+ return;
+ }
+
+ const nodeEntry = CustomEditor.getBlock(editor);
+
+ if (!nodeEntry) return;
+
+ setAnchorPosition(position);
+ },
+ [closePanel, editor]
+ );
+
+ const openPanel = useCallback(() => {
+ const position = getPanelPosition(editor);
+
+ if (position && editor.selection) {
+ startPoint.current = Editor.start(editor, editor.selection);
+ endPoint.current = Editor.end(editor, editor.selection);
+ setPosition(position);
+ } else {
+ setPosition(undefined);
+ }
+ }, [editor, setPosition]);
+
+ useEffect(() => {
+ if (!slashOpen && command === EditorCommand.SlashCommand) {
+ closePanel();
+ return;
+ }
+
+ if (slashOpen && !open) {
+ setCommand(EditorCommand.SlashCommand);
+ openPanel();
+ return;
+ }
+ }, [slashOpen, closePanel, command, open, openPanel]);
+ /**
+ * listen to editor insertText and deleteBackward event
+ */
+ useEffect(() => {
+ const { insertText, deleteBackward } = editor;
+
+ /**
+ * insertText: when insert char at after space or at start of element, show the panel
+ * open condition:
+ * 1. open is false
+ * 2. current block is not code block
+ * 3. current selection is not include root
+ * 4. current selection is collapsed
+ * 5. insert char is command char
+ * 6. before text is empty or end with space
+ * --------- start -----------------
+ * | - selection point
+ * @ - panel char
+ * _ - space
+ * - - other text
+ * -------- open panel ----------------
+ * ---_@|--- => insert text is panel char and before text is end with space, open the panel
+ * @|--- => insert text is panel char and before text is empty, open the panel
+ */
+ editor.insertText = (text, opts) => {
+ if (open || CustomEditor.isCodeBlock(editor) || CustomEditor.selectionIncludeRoot(editor)) {
+ insertText(text, opts);
+ return;
+ }
+
+ const { selection } = editor;
+
+ const command = commands.find((c) => text.endsWith(c));
+ const endOfPanelChar = !!command;
+
+ if (command === EditorCommand.SlashCommand) {
+ setSlashOpen(true);
+ }
+
+ setCommand(command);
+ if (!selection || !endOfPanelChar || !Range.isCollapsed(selection)) {
+ insertText(text, opts);
+ return;
+ }
+
+ const block = CustomEditor.getBlock(editor);
+ const path = block ? block[1] : [];
+ const { anchor } = selection;
+ const beforeText = Editor.string(editor, { anchor, focus: Editor.start(editor, path) }) + text.slice(0, -1);
+ // show the panel when insert char at after space or at start of element
+ const showPanel = !beforeText || beforeText.endsWith(' ');
+
+ insertText(text, opts);
+
+ if (!showPanel) return;
+ openPanel();
+ };
+
+ /**
+ * deleteBackward: when delete char at start of panel char, and then it will be deleted, so we should close the panel if it is open
+ * close condition:
+ * 1. open is true
+ * 2. current block is not code block
+ * 3. current selection is not include root
+ * 4. current selection is collapsed
+ * 5. before text is command char
+ * --------- start -----------------
+ * | - selection point
+ * @ - panel char
+ * - - other text
+ * -------- close panel ----------------
+ * --@|--- => delete text is panel char, close the panel
+ * -------- delete text ----------------
+ * ---@__|--- => delete text is not panel char, delete the text
+ */
+ editor.deleteBackward = (...args) => {
+ if (!open || CustomEditor.isCodeBlock(editor)) {
+ deleteBackward(...args);
+ return;
+ }
+
+ const { selection } = editor;
+
+ if (selection && Range.isCollapsed(selection)) {
+ const { anchor } = selection;
+ const block = CustomEditor.getBlock(editor);
+ const path = block ? block[1] : [];
+ const beforeText = Editor.string(editor, { anchor, focus: Editor.start(editor, path) });
+
+ deleteBackward(...args);
+ // if delete backward at start of panel char, and then it will be deleted, so we should close the panel if it is open
+ if (beforeText === command) {
+ closePanel();
+ }
+ }
+ };
+
+ return () => {
+ editor.insertText = insertText;
+ editor.deleteBackward = deleteBackward;
+ };
+ }, [setSlashOpen, command, open, setPosition, editor, closePanel, openPanel]);
+
+ /**
+ * listen to editor onChange event
+ */
+ useEffect(() => {
+ const { onChange } = editor;
+
+ if (!open) return;
+
+ /**
+ * onChange: when selection change, update the search text or close the panel
+ * --------- start -----------------
+ * | - selection point
+ * @ - panel char
+ * __ - search text
+ * - - other text
+ * -------- close panel ----------------
+ * --|@--- => selection is backward to start point, close the panel
+ * ---@__-|--- => selection is forward to end point, close the panel
+ * -------- update search text ----------------
+ * ---@__|---
+ * ---@_|_--- => selection is forward to start point and backward to end point, update the search text
+ * ---@|__---
+ * --------- end -----------------
+ */
+ editor.onChange = (...args) => {
+ if (!editor.selection || !startPoint.current || !endPoint.current) return;
+ onChange(...args);
+ const isSelectionChange = editor.operations.every((op) => op.type === 'set_selection');
+ const currentPoint = Editor.end(editor, editor.selection);
+ const isBackward = currentPoint.offset < startPoint.current.offset;
+
+ if (isBackward) {
+ closePanel(false);
+ return;
+ }
+
+ if (!isSelectionChange) {
+ if (currentPoint.offset > endPoint.current?.offset) {
+ endPoint.current = currentPoint;
+ }
+
+ const text = Editor.string(editor, {
+ anchor: startPoint.current,
+ focus: endPoint.current,
+ });
+
+ setSearchText(text);
+ } else {
+ const isForward = currentPoint.offset > endPoint.current.offset;
+
+ if (isForward) {
+ closePanel(false);
+ }
+ }
+ };
+
+ return () => {
+ editor.onChange = onChange;
+ };
+ }, [open, editor, closePanel]);
+
+ return {
+ anchorPosition,
+ closePanel,
+ searchText,
+ openPanel,
+ command,
+ };
+}
+
+export const initialTransformOrigin: PopoverOrigin = {
+ vertical: 'top',
+ horizontal: 'left',
+};
+
+export const initialAnchorOrigin: PopoverOrigin = {
+ vertical: 'bottom',
+ horizontal: 'right',
+};
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/CommandPanel.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/CommandPanel.tsx
new file mode 100644
index 0000000000..db58e2deca
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/CommandPanel.tsx
@@ -0,0 +1,17 @@
+import React from 'react';
+import { SlashCommandPanel } from '$app/components/editor/components/tools/command_panel/slash_command_panel';
+import { MentionPanel } from '$app/components/editor/components/tools/command_panel/mention_panel';
+import { EditorCommand, useCommandPanel } from '$app/components/editor/components/tools/command_panel/Command.hooks';
+import withErrorBoundary from '$app/components/_shared/error_boundary/withError';
+
+function CommandPanel() {
+ const { anchorPosition, searchText, openPanel, closePanel, command } = useCommandPanel();
+
+ const Component = command === EditorCommand.SlashCommand ? SlashCommandPanel : MentionPanel;
+
+ return (
+ <Component closePanel={closePanel} searchText={searchText} openPanel={openPanel} anchorPosition={anchorPosition} />
+ );
+}
+
+export default withErrorBoundary(CommandPanel);
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/mention_panel/MentionPanel.hooks.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/mention_panel/MentionPanel.hooks.ts
deleted file mode 100644
index d5834ff965..0000000000
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/mention_panel/MentionPanel.hooks.ts
+++ /dev/null
@@ -1,81 +0,0 @@
-import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
-import { useTranslation } from 'react-i18next';
-import { useSlate } from 'slate-react';
-import { Mention, MentionPage, MentionType } from '$app/application/document/document.types';
-import { CustomEditor } from '$app/components/editor/command';
-import { useAppSelector } from '$app/stores/store';
-
-export function useMentionPanel({
- closePanel,
- searchText,
-}: {
- searchText: string;
- closePanel: (deleteText?: boolean) => void;
-}) {
- const { t } = useTranslation();
- const editor = useSlate();
- const [selectedOptionId, setSelectedOptionId] = useState<string>('');
-
- const onClick = useCallback(
- (type: MentionType, mention: Mention) => {
- closePanel(true);
- CustomEditor.insertMention(editor, mention);
- },
- [closePanel, editor]
- );
- const pagesMap = useAppSelector((state) => state.pages.pageMap);
-
- const pagesRef = useRef<MentionPage[]>([]);
- const [recentPages, setPages] = useState<MentionPage[]>([]);
-
- const loadPages = useCallback(async () => {
- const pages = Object.values(pagesMap);
-
- pagesRef.current = pages;
- setPages(pages);
- }, [pagesMap]);
-
- useEffect(() => {
- void loadPages();
- }, [loadPages]);
-
- useEffect(() => {
- if (!searchText) {
- setPages(pagesRef.current);
- return;
- }
-
- const filteredPages = pagesRef.current.filter((page) => {
- return page.name.toLowerCase().includes(searchText.toLowerCase());
- });
-
- setPages(filteredPages);
- }, [searchText]);
-
- const options = useMemo(() => {
- return [
- {
- key: MentionType.PageRef,
- label: t('document.mention.page.label'),
- options: recentPages.map((page) => {
- return {
- key: page.id,
- label: page.name,
- icon: page.icon,
- onClick: () => {
- onClick(MentionType.PageRef, {
- page: page.id,
- });
- },
- };
- }),
- },
- ].filter((option) => option.options.length > 0);
- }, [onClick, recentPages, t]);
-
- return {
- options,
- selectedOptionId,
- setSelectedOptionId,
- };
-}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/mention_panel/MentionPanel.hooks.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/mention_panel/MentionPanel.hooks.tsx
new file mode 100644
index 0000000000..76ba3d3404
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/mention_panel/MentionPanel.hooks.tsx
@@ -0,0 +1,91 @@
+import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
+import { useTranslation } from 'react-i18next';
+import { useSlate } from 'slate-react';
+import { MentionPage, MentionType } from '$app/application/document/document.types';
+import { CustomEditor } from '$app/components/editor/command';
+import { useAppSelector } from '$app/stores/store';
+import { KeyboardNavigationOption } from '$app/components/_shared/keyboard_navigation/KeyboardNavigation';
+import { ReactComponent as DocumentSvg } from '$app/assets/document.svg';
+
+export function useMentionPanel({
+ closePanel,
+ searchText,
+}: {
+ searchText: string;
+ closePanel: (deleteText?: boolean) => void;
+}) {
+ const { t } = useTranslation();
+ const editor = useSlate();
+
+ const pagesMap = useAppSelector((state) => state.pages.pageMap);
+
+ const pagesRef = useRef<MentionPage[]>([]);
+ const [recentPages, setPages] = useState<MentionPage[]>([]);
+
+ const loadPages = useCallback(async () => {
+ const pages = Object.values(pagesMap);
+
+ pagesRef.current = pages;
+ setPages(pages);
+ }, [pagesMap]);
+
+ useEffect(() => {
+ void loadPages();
+ }, [loadPages]);
+
+ useEffect(() => {
+ if (!searchText) {
+ setPages(pagesRef.current);
+ return;
+ }
+
+ const filteredPages = pagesRef.current.filter((page) => {
+ return page.name.toLowerCase().includes(searchText.toLowerCase());
+ });
+
+ setPages(filteredPages);
+ }, [searchText]);
+
+ const onConfirm = useCallback(
+ (key: string) => {
+ const [, id] = key.split(',');
+
+ closePanel(true);
+ CustomEditor.insertMention(editor, {
+ page: id,
+ });
+ },
+ [closePanel, editor]
+ );
+
+ const renderPage = useCallback(
+ (page: MentionPage) => {
+ return {
+ key: `${MentionType.PageRef},${page.id}`,
+ content: (
+ <div className={'flex items-center gap-2'}>
+ <div className={'flex h-5 w-5 items-center justify-center'}>{page.icon?.value || <DocumentSvg />}</div>
+
+ <div className={'flex-1'}>{page.name || t('document.title.placeholder')}</div>
+ </div>
+ ),
+ };
+ },
+ [t]
+ );
+
+ const options: KeyboardNavigationOption<MentionType | string>[] = useMemo(() => {
+ return [
+ {
+ key: MentionType.PageRef,
+ content: <div className={'px-3 pb-1 pt-2 text-sm'}>{t('document.mention.page.label')}</div>,
+ children: recentPages.map(renderPage),
+ },
+ ].filter((option) => option.children.length > 0);
+ }, [recentPages, renderPage, t]);
+
+ return {
+ options,
+ onConfirm,
+ };
+}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/mention_panel/MentionPanel.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/mention_panel/MentionPanel.tsx
index 33daced920..eba84e4ac4 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/mention_panel/MentionPanel.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/mention_panel/MentionPanel.tsx
@@ -1,20 +1,53 @@
import React, { useRef } from 'react';
-import { PanelPopoverProps, usePanel } from '$app/components/editor/components/tools/command_panel/usePanel.hooks';
+import {
+ initialAnchorOrigin,
+ initialTransformOrigin,
+ PanelPopoverProps,
+ PanelProps,
+} from '$app/components/editor/components/tools/command_panel/Command.hooks';
import Popover from '@mui/material/Popover';
import MentionPanelContent from '$app/components/editor/components/tools/command_panel/mention_panel/MentionPanelContent';
+import usePopoverAutoPosition from '$app/components/_shared/popover/Popover.hooks';
-export function MentionPanel() {
+export function MentionPanel({ anchorPosition, closePanel, searchText }: PanelProps) {
const ref = useRef<HTMLDivElement>(null);
- const { anchorPosition, closePanel, searchText } = usePanel(ref);
const open = Boolean(anchorPosition);
+ const {
+ paperHeight,
+ anchorPosition: newAnchorPosition,
+ paperWidth,
+ transformOrigin,
+ anchorOrigin,
+ isEntered,
+ } = usePopoverAutoPosition({
+ initialPaperWidth: 300,
+ initialPaperHeight: 360,
+ anchorPosition,
+ initialTransformOrigin,
+ initialAnchorOrigin,
+ open,
+ });
+
return (
<div ref={ref} className={'mention-panel'}>
{open && (
- <Popover {...PanelPopoverProps} open={open} anchorPosition={anchorPosition} onClose={() => closePanel(false)}>
- <MentionPanelContent closePanel={closePanel} searchText={searchText} />
+ <Popover
+ {...PanelPopoverProps}
+ open={open && isEntered}
+ anchorPosition={newAnchorPosition}
+ anchorOrigin={anchorOrigin}
+ transformOrigin={transformOrigin}
+ onClose={() => closePanel(false)}
+ >
+ <MentionPanelContent
+ width={paperWidth}
+ maxHeight={paperHeight}
+ closePanel={closePanel}
+ searchText={searchText}
+ />
</Popover>
)}
</div>
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/mention_panel/MentionPanelContent.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/mention_panel/MentionPanelContent.tsx
index 53dbdb0dd3..9664479fbd 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/mention_panel/MentionPanelContent.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/mention_panel/MentionPanelContent.tsx
@@ -1,75 +1,42 @@
-import React, { useCallback, useRef } from 'react';
+import React, { useRef } from 'react';
import { useMentionPanel } from '$app/components/editor/components/tools/command_panel/mention_panel/MentionPanel.hooks';
-import { useKeyDown } from '$app/components/editor/components/tools/command_panel/usePanel.hooks';
-import { useTranslation } from 'react-i18next';
-import { MenuItem, MenuList, Typography } from '@mui/material';
-import { ReactComponent as DocumentSvg } from '$app/assets/document.svg';
+
+import KeyboardNavigation from '$app/components/_shared/keyboard_navigation/KeyboardNavigation';
function MentionPanelContent({
closePanel,
searchText,
+ maxHeight,
+ width,
}: {
closePanel: (deleteText?: boolean) => void;
searchText: string;
+ maxHeight: number;
+ width: number;
}) {
- const { t } = useTranslation();
const scrollRef = useRef<HTMLDivElement>(null);
- const { options, selectedOptionId, setSelectedOptionId } = useMentionPanel({
+ const { options, onConfirm } = useMentionPanel({
closePanel,
searchText,
});
- const handleSelectKey = useCallback(
- (key?: string | number) => {
- setSelectedOptionId(String(key));
- },
- [setSelectedOptionId]
- );
-
- useKeyDown({
- scrollRef,
- panelOpen: true,
- closePanel,
- options,
- selectedKey: selectedOptionId,
- setSelectedKey: handleSelectKey,
- });
return (
- <div ref={scrollRef} className={'max-h-[360px] w-[300px] overflow-auto overflow-x-hidden'}>
- {options.length === 0 ? (
- <Typography variant='body1' className={'p-3 text-text-caption'}>
- No results
- </Typography>
- ) : (
- options.map((option, index) => (
- <div key={option.key} className={`${index !== 0 ? 'border-t border-line-divider' : ''}`}>
- <Typography variant='body1' className={'p-3 px-4 text-text-caption'}>
- {option.label}
- </Typography>
- <MenuList className={'px-2 pb-3 pt-0'}>
- {option.options.map((subOption) => {
- return (
- <MenuItem
- onMouseEnter={() => setSelectedOptionId(subOption.key)}
- selected={selectedOptionId === subOption.key}
- data-type={subOption.key}
- className={'ml-0 flex w-full items-center justify-start px-2 py-1'}
- key={subOption.key}
- onClick={subOption.onClick}
- >
- <div className={'h-4 w-4'}>{subOption.icon?.value || <DocumentSvg />}</div>
-
- <Typography variant='body1' className={'ml-2 text-xs'}>
- {subOption.label || t('document.title.placeholder')}
- </Typography>
- </MenuItem>
- );
- })}
- </MenuList>
- </div>
- ))
- )}
+ <div
+ ref={scrollRef}
+ style={{
+ maxHeight,
+ width,
+ }}
+ className={' overflow-auto overflow-x-hidden'}
+ >
+ <KeyboardNavigation
+ scrollRef={scrollRef}
+ onEscape={closePanel}
+ onConfirm={onConfirm}
+ options={options}
+ disableFocus={true}
+ />
</div>
);
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/slash_command_panel/SlashCommandPanel.hooks.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/slash_command_panel/SlashCommandPanel.hooks.tsx
similarity index 75%
rename from frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/slash_command_panel/SlashCommandPanel.hooks.ts
rename to frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/slash_command_panel/SlashCommandPanel.hooks.tsx
index 54f054f1da..91c9ed7024 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/slash_command_panel/SlashCommandPanel.hooks.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/slash_command_panel/SlashCommandPanel.hooks.tsx
@@ -1,8 +1,8 @@
import { EditorNodeType } from '$app/application/document/document.types';
-import { useCallback, useEffect, useMemo, useState } from 'react';
+import { useCallback, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
-import { useSlate } from 'slate-react';
-import { Transforms } from 'slate';
+import { ReactEditor, useSlate } from 'slate-react';
+import { Path } from 'slate';
import { getBlock } from '$app/components/editor/plugins/utils';
import { ReactComponent as TextIcon } from '$app/assets/text.svg';
import { ReactComponent as TodoListIcon } from '$app/assets/todo-list.svg';
@@ -17,13 +17,15 @@ import { ReactComponent as GridIcon } from '$app/assets/grid.svg';
import { DataObjectOutlined, FunctionsOutlined, HorizontalRuleOutlined, MenuBookOutlined } from '@mui/icons-material';
import { CustomEditor } from '$app/components/editor/command';
import { randomEmoji } from '$app/utils/emoji';
+import { KeyboardNavigationOption } from '$app/components/_shared/keyboard_navigation/KeyboardNavigation';
+import { YjsEditor } from '@slate-yjs/core';
enum SlashCommandPanelTab {
BASIC = 'basic',
ADVANCED = 'advanced',
}
-enum SlashOptionType {
+export enum SlashOptionType {
Paragraph,
TodoList,
Heading1,
@@ -89,16 +91,13 @@ const headingTypes = [SlashOptionType.Heading1, SlashOptionType.Heading2, SlashO
export function useSlashCommandPanel({
searchText,
closePanel,
- open,
}: {
searchText: string;
closePanel: (deleteText?: boolean) => void;
- open: boolean;
}) {
const { t } = useTranslation();
const editor = useSlate();
- const [selectedType, setSelectedType] = useState(SlashOptionType.Paragraph);
- const onClick = useCallback(
+ const onConfirm = useCallback(
(type: SlashOptionType) => {
const node = getBlock(editor);
@@ -124,20 +123,26 @@ export function useSlashCommandPanel({
if (nodeType === EditorNodeType.CodeBlock) {
Object.assign(data, {
- language: 'javascript',
+ language: 'json',
});
}
closePanel(true);
const newNode = getBlock(editor);
+ const block = CustomEditor.getBlock(editor);
- if (!newNode) return;
+ const path = block ? block[1] : null;
- const isEmpty = CustomEditor.isEmptyText(editor, newNode);
+ if (!newNode || !path) return;
+
+ const isEmpty = CustomEditor.isEmptyText(editor, newNode) && newNode.type === EditorNodeType.Paragraph;
if (!isEmpty) {
- Transforms.splitNodes(editor, { always: true });
+ const nextPath = Path.next(path);
+
+ CustomEditor.insertEmptyLine(editor as ReactEditor & YjsEditor, nextPath);
+ editor.select(nextPath);
}
CustomEditor.turnToBlock(editor, {
@@ -155,7 +160,7 @@ export function useSlashCommandPanel({
Icon: TextIcon,
},
[SlashOptionType.TodoList]: {
- label: t('document.plugins.todoList'),
+ label: t('editor.checkbox'),
Icon: TodoListIcon,
},
[SlashOptionType.Heading1]: {
@@ -217,50 +222,55 @@ export function useSlashCommandPanel({
};
}, []);
- const options = useMemo(() => {
+ const renderOptionContent = useCallback(
+ (type: SlashOptionType) => {
+ const Icon = typeToLabelIconMap[type].Icon;
+
+ return (
+ <div className={'flex items-center gap-2'}>
+ <div className={'flex h-6 w-6 items-center justify-center'}>
+ <Icon className={'h-4 w-4'} />
+ </div>
+
+ <div className={'flex-1'}>{typeToLabelIconMap[type].label}</div>
+ </div>
+ );
+ },
+ [typeToLabelIconMap]
+ );
+
+ const options: KeyboardNavigationOption<SlashOptionType | SlashCommandPanelTab>[] = useMemo(() => {
return slashOptionGroup
.map((group) => {
return {
key: group.key,
- label: groupTypeToLabelMap[group.key],
- options: group.options
+ content: <div className={'px-3 pb-1 pt-2 text-sm'}>{groupTypeToLabelMap[group.key]}</div>,
+ children: group.options
.map((type) => {
return {
key: type,
- label: typeToLabelIconMap[type].label,
- Icon: typeToLabelIconMap[type].Icon,
- onClick: () => onClick(type),
+ content: renderOptionContent(type),
};
})
.filter((option) => {
if (!searchText) return true;
- return option.label.toLowerCase().includes(searchText.toLowerCase());
- }),
- };
- })
- .filter((group) => group.options.length > 0);
- }, [groupTypeToLabelMap, onClick, searchText, typeToLabelIconMap]);
+ const label = typeToLabelIconMap[option.key].label;
- useEffect(() => {
- if (open) {
- const node = getBlock(editor);
-
- if (!node) return;
- const nodeType = node.type;
+ let newSearchText = searchText;
- const optionType = Object.entries(slashOptionMapToEditorNodeType).find(([, type]) => type === nodeType);
+ if (searchText.startsWith('/')) {
+ newSearchText = searchText.slice(1);
+ }
- if (optionType) {
- setSelectedType(Number(optionType[0]));
- }
- } else {
- setSelectedType(SlashOptionType.Paragraph);
- }
- }, [editor, open]);
+ return label.toLowerCase().includes(newSearchText.toLowerCase());
+ }),
+ };
+ })
+ .filter((group) => group.children.length > 0);
+ }, [searchText, groupTypeToLabelMap, typeToLabelIconMap, renderOptionContent]);
return {
options,
- selectedType,
- setSelectedType,
+ onConfirm,
};
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/slash_command_panel/SlashCommandPanel.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/slash_command_panel/SlashCommandPanel.tsx
index 967a699bad..b09af97b39 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/slash_command_panel/SlashCommandPanel.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/slash_command_panel/SlashCommandPanel.tsx
@@ -1,34 +1,69 @@
-import React, { useRef } from 'react';
-import { PanelPopoverProps, usePanel } from '$app/components/editor/components/tools/command_panel/usePanel.hooks';
+import React, { useCallback, useRef } from 'react';
+import {
+ initialAnchorOrigin,
+ initialTransformOrigin,
+ PanelPopoverProps,
+ PanelProps,
+} from '$app/components/editor/components/tools/command_panel/Command.hooks';
import Popover from '@mui/material/Popover';
import SlashCommandPanelContent from '$app/components/editor/components/tools/command_panel/slash_command_panel/SlashCommandPanelContent';
import { useSlate } from 'slate-react';
+import usePopoverAutoPosition from '$app/components/_shared/popover/Popover.hooks';
-export function SlashCommandPanel() {
+export function SlashCommandPanel({ anchorPosition, closePanel, searchText }: PanelProps) {
const ref = useRef<HTMLDivElement>(null);
const editor = useSlate();
- const { anchorPosition, closePanel, searchText } = usePanel(ref);
const open = Boolean(anchorPosition);
+ const handleClose = useCallback(
+ (deleteText?: boolean) => {
+ closePanel(deleteText);
+ },
+ [closePanel]
+ );
+
+ const {
+ paperHeight,
+ paperWidth,
+ anchorPosition: newAnchorPosition,
+ transformOrigin,
+ anchorOrigin,
+ isEntered,
+ } = usePopoverAutoPosition({
+ initialPaperWidth: 220,
+ initialPaperHeight: 360,
+ anchorPosition,
+ initialTransformOrigin,
+ initialAnchorOrigin,
+ open,
+ });
+
return (
<div ref={ref} className={'slash-command-panel'}>
{open && (
<Popover
{...PanelPopoverProps}
- open={open}
- anchorPosition={anchorPosition}
+ open={open && isEntered}
+ anchorPosition={newAnchorPosition}
+ anchorOrigin={anchorOrigin}
+ transformOrigin={transformOrigin}
onClose={() => {
const selection = editor.selection;
- closePanel(false);
+ handleClose(false);
if (selection) {
editor.select(selection);
}
}}
>
- <SlashCommandPanelContent closePanel={closePanel} searchText={searchText} />
+ <SlashCommandPanelContent
+ width={paperWidth}
+ maxHeight={paperHeight}
+ closePanel={handleClose}
+ searchText={searchText}
+ />
</Popover>
)}
</div>
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/slash_command_panel/SlashCommandPanelContent.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/slash_command_panel/SlashCommandPanelContent.tsx
index 1fe412549c..c5f2df0ae5 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/slash_command_panel/SlashCommandPanelContent.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/slash_command_panel/SlashCommandPanelContent.tsx
@@ -1,74 +1,89 @@
-import React, { useCallback, useRef } from 'react';
-import { MenuItem, MenuList, Typography } from '@mui/material';
-import { useSlashCommandPanel } from '$app/components/editor/components/tools/command_panel/slash_command_panel/SlashCommandPanel.hooks';
-import { useKeyDown } from '$app/components/editor/components/tools/command_panel/usePanel.hooks';
+import React, { useEffect, useRef } from 'react';
+import KeyboardNavigation from '$app/components/_shared/keyboard_navigation/KeyboardNavigation';
+import {
+ SlashOptionType,
+ useSlashCommandPanel,
+} from '$app/components/editor/components/tools/command_panel/slash_command_panel/SlashCommandPanel.hooks';
+import { useSlateStatic } from 'slate-react';
+
+const noResultBuffer = 2;
function SlashCommandPanelContent({
closePanel,
searchText,
+ maxHeight,
+ width,
}: {
closePanel: (deleteText?: boolean) => void;
searchText: string;
+ maxHeight: number;
+ width: number;
}) {
const scrollRef = useRef<HTMLDivElement>(null);
- const { options, selectedType, setSelectedType } = useSlashCommandPanel({
- searchText,
+ const { options, onConfirm } = useSlashCommandPanel({
closePanel,
- open: true,
+ searchText,
});
- const handleSelectType = useCallback(
- (type?: string | number) => {
- if (type === undefined) return;
+ // Used to keep track of how many times the user has typed and not found any result
+ const noResultCount = useRef(0);
- setSelectedType(Number(type));
- },
- [setSelectedType]
- );
+ const editor = useSlateStatic();
- useKeyDown({
- scrollRef,
- panelOpen: true,
- closePanel,
- options,
- selectedKey: selectedType,
- setSelectedKey: handleSelectType,
- });
- return (
- <div ref={scrollRef} className={'max-h-[360px] w-[220px] overflow-auto overflow-x-hidden py-1 pl-1'}>
- {options.length > 0 ? (
- options.map((group) => (
- <div key={group.key}>
- <Typography variant='body1' className={'p-2 text-text-caption'}>
- {group.label}
- </Typography>
- <MenuList className={'py-0 pl-1'}>
- {group.options.map((subOption) => {
- const Icon = subOption.Icon;
+ useEffect(() => {
+ const { insertText, deleteBackward } = editor;
+
+ editor.insertText = (text, opts) => {
+ // close panel if track of no result is greater than buffer
+ if (noResultCount.current >= noResultBuffer) {
+ closePanel(false);
+ }
- return (
- <MenuItem
- onMouseEnter={() => setSelectedType(subOption.key)}
- selected={selectedType === subOption.key}
- data-type={subOption.key}
- className={'ml-0 flex w-full items-center justify-start'}
- key={subOption.key}
- onClick={subOption.onClick}
- >
- <Icon className={'mr-2 h-4 w-4'} />
- <div className={'flex-1'}>{subOption.label}</div>
- </MenuItem>
- );
- })}
- </MenuList>
- </div>
- ))
- ) : (
- <Typography variant='body1' className={'p-3 text-text-caption'}>
- No results
- </Typography>
- )}
+ if (options.length === 0) {
+ noResultCount.current += 1;
+ }
+
+ insertText(text, opts);
+ };
+
+ editor.deleteBackward = (unit) => {
+ // reset no result count
+ if (noResultCount.current > 0) {
+ noResultCount.current -= 1;
+ }
+
+ // close panel if no text
+ if (!searchText) {
+ closePanel(true);
+ return;
+ }
+
+ deleteBackward(unit);
+ };
+
+ return () => {
+ editor.insertText = insertText;
+ editor.deleteBackward = deleteBackward;
+ };
+ }, [closePanel, editor, searchText, options.length]);
+
+ return (
+ <div
+ ref={scrollRef}
+ style={{
+ maxHeight,
+ width,
+ }}
+ className={'overflow-auto overflow-x-hidden py-1'}
+ >
+ <KeyboardNavigation
+ scrollRef={scrollRef}
+ onEscape={closePanel}
+ onConfirm={(key) => onConfirm(key as SlashOptionType)}
+ options={options}
+ disableFocus={true}
+ />
</div>
);
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/usePanel.hooks.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/usePanel.hooks.ts
deleted file mode 100644
index 0adf61704b..0000000000
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/usePanel.hooks.ts
+++ /dev/null
@@ -1,256 +0,0 @@
-import { useEffect, RefObject, useState, useCallback, useRef } from 'react';
-import { getPanelPosition } from '$app/components/editor/components/tools/command_panel/utils';
-import { ReactEditor, useSlate } from 'slate-react';
-import { PopoverPreventBlurProps } from '$app/components/editor/components/tools/popover';
-import { PopoverProps } from '@mui/material/Popover';
-import { commandPanelShowProperty } from '$app/components/editor/components/editor/shortcuts/withCommandShortcuts';
-import { Editor, Point, Transforms } from 'slate';
-import { CustomEditor } from '$app/components/editor/command';
-
-export const PanelPopoverProps: Partial<PopoverProps> = {
- ...PopoverPreventBlurProps,
- onMouseUp: (e) => e.stopPropagation(),
- transformOrigin: {
- vertical: -28,
- horizontal: 'left',
- },
- anchorReference: 'anchorPosition',
-};
-
-export function usePanel(ref: RefObject<HTMLDivElement | null>) {
- const editor = useSlate();
- const [anchorPosition, setAnchorPosition] = useState<
- | {
- top: number;
- left: number;
- }
- | undefined
- >(undefined);
- const startPoint = useRef<Point>();
- const endPoint = useRef<Point>();
- const open = Boolean(anchorPosition);
- const [searchText, setSearchText] = useState('');
-
- const closePanel = useCallback(
- (deleteText?: boolean) => {
- ref.current?.classList.remove(commandPanelShowProperty);
-
- if (deleteText && startPoint.current && endPoint.current) {
- const anchor = {
- path: startPoint.current.path,
- offset: startPoint.current.offset - 1,
- };
- const focus = {
- path: endPoint.current.path,
- offset: endPoint.current.offset,
- };
-
- Transforms.delete(editor, {
- at: {
- anchor,
- focus,
- },
- });
- }
-
- setAnchorPosition(undefined);
- setSearchText('');
- },
- [editor, ref]
- );
-
- const setPosition = useCallback(
- (position?: { left: number; top: number }) => {
- if (!position) {
- closePanel(false);
- return;
- }
-
- const nodeEntry = CustomEditor.getBlock(editor);
-
- if (!nodeEntry) return;
-
- setAnchorPosition({
- top: position.top,
- left: position.left,
- });
- },
- [closePanel, editor]
- );
-
- useEffect(() => {
- const el = ref.current;
-
- if (!el) return;
-
- let prevState = el.classList.contains(commandPanelShowProperty);
- const observer = new MutationObserver((mutations) => {
- mutations.forEach((mutation) => {
- const { target } = mutation;
-
- if (mutation.attributeName === 'class') {
- const currentState = (target as HTMLElement).classList.contains(commandPanelShowProperty);
-
- if (prevState !== currentState) {
- prevState = currentState;
- if (currentState) {
- const position = getPanelPosition(editor);
-
- if (position && editor.selection) {
- startPoint.current = Editor.start(editor, editor.selection);
- endPoint.current = Editor.end(editor, editor.selection);
- setPosition(position);
- } else {
- setPosition(undefined);
- }
- } else {
- setPosition(undefined);
- }
- }
- }
- });
- });
-
- observer.observe(el, { attributes: true });
-
- return () => {
- observer.disconnect();
- };
- }, [setPosition, editor, ref]);
-
- useEffect(() => {
- const { onChange } = editor;
-
- if (open) {
- editor.onChange = (...args) => {
- if (!editor.selection || !startPoint.current || !endPoint.current) return;
- onChange(...args);
- const isSelectionChange = editor.operations.every((op) => op.type === 'set_selection');
- const currentPoint = Editor.end(editor, editor.selection);
- const isBackward = currentPoint.offset < startPoint.current.offset;
-
- if (isBackward) {
- closePanel(false);
- return;
- }
-
- if (!isSelectionChange) {
- if (currentPoint.offset > endPoint.current?.offset) {
- endPoint.current = currentPoint;
- }
-
- const text = Editor.string(editor, {
- anchor: startPoint.current,
- focus: endPoint.current,
- });
-
- setSearchText(text);
- } else {
- const isForward = currentPoint.offset > endPoint.current.offset;
-
- if (isForward) {
- closePanel(false);
- }
- }
- };
- } else {
- editor.onChange = onChange;
- }
-
- return () => {
- editor.onChange = onChange;
- };
- }, [open, editor, closePanel]);
-
- return {
- anchorPosition,
- closePanel,
- searchText,
- };
-}
-
-export function useKeyDown({
- scrollRef: ref,
- options,
- panelOpen: open,
- setSelectedKey,
- selectedKey,
- closePanel,
-}: {
- panelOpen: boolean;
- selectedKey?: string | number;
- setSelectedKey: (key?: string | number) => void;
- closePanel: (deleteText?: boolean) => void;
- scrollRef: RefObject<HTMLDivElement | null>;
- options: {
- key: string | number;
- label: string;
- options: { key: string | number; label: string; onClick: () => void }[];
- }[];
-}) {
- const editor = useSlate();
- const handleKeyDown = useCallback(
- (e: KeyboardEvent) => {
- const flattenOptions = options.flatMap((group) => group.options);
-
- const index = flattenOptions.findIndex((option) => option.key === selectedKey);
- const option = flattenOptions[index];
- const nextIndex = (index + 1) % flattenOptions.length;
- const prevIndex = (index - 1 + flattenOptions.length) % flattenOptions.length;
-
- switch (e.key) {
- case 'Escape':
- e.preventDefault();
- closePanel(false);
- break;
- case 'ArrowDown': {
- e.preventDefault();
- const nextOption = flattenOptions[nextIndex].key;
- const dom = ref.current?.querySelector(`[data-type="${nextOption}"]`);
-
- setSelectedKey(nextOption);
-
- dom?.scrollIntoView({
- behavior: 'smooth',
- block: 'end',
- });
- break;
- }
-
- case 'ArrowUp': {
- e.preventDefault();
- const prevOption = flattenOptions[prevIndex].key;
- const prevDom = ref.current?.querySelector(`[data-type="${prevOption}"]`);
-
- setSelectedKey(prevOption);
- prevDom?.scrollIntoView({
- behavior: 'smooth',
- block: 'end',
- });
- break;
- }
-
- case 'Enter':
- case 'Tab':
- e.preventDefault();
- option.onClick();
- break;
- }
- },
- [selectedKey, options, closePanel, ref, setSelectedKey]
- );
-
- useEffect(() => {
- const slateDom = ReactEditor.toDOMNode(editor, editor);
-
- if (open) {
- slateDom.addEventListener('keydown', handleKeyDown);
- } else {
- slateDom.removeEventListener('keydown', handleKeyDown);
- }
-
- return () => {
- slateDom.removeEventListener('keydown', handleKeyDown);
- };
- }, [editor, handleKeyDown, open]);
-}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/utils.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/utils.ts
index b10aa021ff..65a095dc58 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/utils.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/command_panel/utils.ts
@@ -19,8 +19,12 @@ export function getPanelPosition(editor: ReactEditor) {
const rect = domRange?.getBoundingClientRect();
if (!rect) return null;
+ const nodeDom = domSelection.anchorNode?.parentElement?.closest('.text-element');
+ const height = (nodeDom?.getBoundingClientRect().height ?? 0) + 8;
+
return {
...rect,
+ height,
top: rect.top,
left: rect.left,
};
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/notify/index.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/notify/index.ts
index 0cfd6a5f10..1086cabdfd 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/notify/index.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/notify/index.ts
@@ -18,4 +18,10 @@ export const notify = {
loading: (message: string) => {
toast.loading(message, commonOptions);
},
+ info: (message: string) => {
+ toast(message, commonOptions);
+ },
+ clear: () => {
+ toast.dismiss();
+ },
};
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/SelectionActions.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/SelectionActions.tsx
index de79011a17..5cb6e96596 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/SelectionActions.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/SelectionActions.tsx
@@ -1,7 +1,5 @@
-import React, { useMemo } from 'react';
-import { useSlate } from 'slate-react';
-import { Range } from 'slate';
-import { CustomEditor } from '$app/components/editor/command';
+import React from 'react';
+
import { Paragraph } from '$app/components/editor/components/tools/selection_toolbar/actions/paragraph';
import { Heading } from '$app/components/editor/components/tools/selection_toolbar/actions/heading';
import { Divider } from '@mui/material';
@@ -21,38 +19,22 @@ import { Align } from '$app/components/editor/components/tools/selection_toolbar
import { Color } from '$app/components/editor/components/tools/selection_toolbar/actions/color';
function SelectionActions({
+ isAcrossBlocks,
storeSelection,
restoreSelection,
}: {
storeSelection: () => void;
restoreSelection: () => void;
+ isAcrossBlocks: boolean;
+ visible: boolean;
}) {
- const editor = useSlate();
- const isAcrossBlockSelection = useMemo(() => {
- if (!editor.selection) return false;
- const selection = editor.selection;
- const start = selection.anchor;
- const end = selection.focus;
-
- if (!start || !end) return false;
-
- if (!Range.isExpanded(selection)) return false;
-
- const startNode = CustomEditor.getBlock(editor, start);
-
- const endNode = CustomEditor.getBlock(editor, end);
-
- return Boolean(startNode && endNode && startNode[0].blockId !== endNode[0].blockId);
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [editor.selection, editor]);
-
return (
<div className={'flex w-fit flex-grow items-center gap-1'}>
- {!isAcrossBlockSelection && (
+ {!isAcrossBlocks && (
<>
<Paragraph />
<Heading />
- <Divider className={'opacity-40'} orientation={'vertical'} flexItem={true} />
+ <Divider className={'my-1.5 bg-line-on-toolbar opacity-40'} orientation={'vertical'} flexItem={true} />
</>
)}
<Bold />
@@ -60,26 +42,26 @@ function SelectionActions({
<Underline />
<StrikeThrough />
<InlineCode />
- {!isAcrossBlockSelection && (
+ {!isAcrossBlocks && (
<>
<Formula />
- <Divider className={'opacity-40'} orientation={'vertical'} flexItem={true} />
+ <Divider className={'my-1.5 bg-line-on-toolbar opacity-40'} orientation={'vertical'} flexItem={true} />
</>
)}
- {!isAcrossBlockSelection && (
+ {!isAcrossBlocks && (
<>
<TodoList />
<Quote />
<ToggleList />
<BulletedList />
<NumberedList />
- <Divider className={'opacity-40'} orientation={'vertical'} flexItem={true} />
+ <Divider className={'my-1.5 bg-line-on-toolbar opacity-40'} orientation={'vertical'} flexItem={true} />
</>
)}
- {!isAcrossBlockSelection && <Href />}
+ {!isAcrossBlocks && <Href />}
<Align />
- <Color onOpen={storeSelection} onClose={restoreSelection} />
+ <Color onClose={restoreSelection} onOpen={storeSelection} />
</div>
);
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/SelectionToolbar.hooks.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/SelectionToolbar.hooks.ts
index ce0399c640..648ca84079 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/SelectionToolbar.hooks.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/SelectionToolbar.hooks.ts
@@ -1,16 +1,44 @@
-import { ReactEditor, useSlate } from 'slate-react';
-import { MutableRefObject, useCallback, useContext, useEffect, useRef, useState } from 'react';
+import { ReactEditor, useFocused, useSlate } from 'slate-react';
+import { MutableRefObject, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { getSelectionPosition } from '$app/components/editor/components/tools/selection_toolbar/utils';
import debounce from 'lodash-es/debounce';
import { CustomEditor } from '$app/components/editor/command';
-import { DecorateStateContext } from '$app/components/editor/components/editor/Editor.hooks';
-import { BaseRange } from 'slate';
+import { BaseRange, Editor, Range as SlateRange } from 'slate';
+import { useDecorateDispatch } from '$app/components/editor/stores/decorate';
+
+const DELAY = 300;
export function useSelectionToolbar(ref: MutableRefObject<HTMLDivElement | null>) {
const editor = useSlate() as ReactEditor;
-
+ const isDraggingRef = useRef(false);
+ const [isAcrossBlocks, setIsAcrossBlocks] = useState(false);
const [visible, setVisible] = useState(false);
- const rangeRef = useRef<Range | null>(null);
+ const isFocusedEditor = useFocused();
+
+ // paint the selection when the editor is blurred
+ const { add: addDecorate, clear: clearDecorate, getStaticState } = useDecorateDispatch();
+
+ // Restore selection after the editor is focused
+ const restoreSelection = useCallback(() => {
+ const decorateState = getStaticState();
+
+ if (!decorateState) return;
+
+ editor.select({
+ ...decorateState.range,
+ });
+
+ clearDecorate();
+ ReactEditor.focus(editor);
+ }, [getStaticState, clearDecorate, editor]);
+
+ // Store selection when the editor is blurred
+ const storeSelection = useCallback(() => {
+ addDecorate({
+ range: editor.selection as BaseRange,
+ class_name: 'bg-content-blue-100',
+ });
+ }, [addDecorate, editor]);
const closeToolbar = useCallback(() => {
const el = ref.current;
@@ -19,11 +47,12 @@ export function useSelectionToolbar(ref: MutableRefObject<HTMLDivElement | null>
return;
}
- rangeRef.current = null;
+ restoreSelection();
+
setVisible(false);
el.style.opacity = '0';
el.style.pointerEvents = 'none';
- }, [ref]);
+ }, [ref, restoreSelection]);
const recalculatePosition = useCallback(() => {
const el = ref.current;
@@ -32,16 +61,12 @@ export function useSelectionToolbar(ref: MutableRefObject<HTMLDivElement | null>
return;
}
+ // Close toolbar when selection include root
if (CustomEditor.selectionIncludeRoot(editor)) {
closeToolbar();
return;
}
- if (rangeRef.current) {
- closeToolbar();
- return;
- }
-
const position = getSelectionPosition(editor);
if (!position) {
@@ -53,11 +78,20 @@ export function useSelectionToolbar(ref: MutableRefObject<HTMLDivElement | null>
setVisible(true);
el.style.opacity = '1';
- el.style.pointerEvents = 'auto';
+
+ // if dragging, disable pointer events
+ if (isDraggingRef.current) {
+ el.style.pointerEvents = 'none';
+ } else {
+ el.style.pointerEvents = 'auto';
+ }
+
+ // If toolbar is out of editor, move it to the top
el.style.top = `${position.top + slateEditorDom.offsetTop - el.offsetHeight}px`;
- const left = position.left + slateEditorDom.offsetLeft - el.offsetWidth / 2 + position.width / 2;
+ const left = position.left + slateEditorDom.offsetLeft;
+ // If toolbar is out of editor, move it to the left edge of the editor
if (left < 0) {
el.style.left = '0';
return;
@@ -65,6 +99,7 @@ export function useSelectionToolbar(ref: MutableRefObject<HTMLDivElement | null>
const right = left + el.offsetWidth;
+ // If toolbar is out of editor, move the right edge to the right edge of the editor
if (right > slateEditorDom.offsetWidth) {
el.style.left = `${slateEditorDom.offsetWidth - el.offsetWidth}px`;
return;
@@ -73,47 +108,90 @@ export function useSelectionToolbar(ref: MutableRefObject<HTMLDivElement | null>
el.style.left = `${left}px`;
}, [closeToolbar, editor, ref]);
+ const debounceRecalculatePosition = useMemo(() => debounce(recalculatePosition, DELAY), [recalculatePosition]);
+
+ // eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => {
- const debounceRecalculatePosition = debounce(recalculatePosition, 100);
+ const decorateState = getStaticState();
- document.addEventListener('mouseup', debounceRecalculatePosition);
- document.addEventListener('keydown', debounceRecalculatePosition);
- return () => {
- document.removeEventListener('mouseup', debounceRecalculatePosition);
- document.addEventListener('keydown', debounceRecalculatePosition);
+ if (decorateState) return;
+
+ const { selection } = editor;
+
+ if (!isFocusedEditor || !selection || SlateRange.isCollapsed(selection) || Editor.string(editor, selection) === '') {
+ debounceRecalculatePosition.cancel();
+ closeToolbar();
+ return;
+ }
+
+ const start = selection.anchor;
+ const end = selection.focus;
+
+ setIsAcrossBlocks(!CustomEditor.blockEqual(editor, start, end));
+ debounceRecalculatePosition();
+ });
+
+ // Update drag status
+ useEffect(() => {
+ const el = ReactEditor.toDOMNode(editor, editor);
+
+ const toolbar = ref.current;
+
+ if (!el || !toolbar) {
+ return;
+ }
+
+ const onMouseDown = () => {
+ isDraggingRef.current = true;
};
- }, [editor, recalculatePosition, ref]);
- const decorateStateContext = useContext(DecorateStateContext);
+ const onMouseUp = () => {
+ if (visible) {
+ toolbar.style.pointerEvents = 'auto';
+ }
- const restoreSelection = useCallback(() => {
- if (!rangeRef.current) return;
- const windowSelection = window.getSelection();
+ isDraggingRef.current = false;
+ };
- if (!windowSelection) return;
- windowSelection.removeAllRanges();
- windowSelection.addRange(rangeRef.current);
- rangeRef.current = null;
- decorateStateContext.clear();
- }, [decorateStateContext]);
+ el.addEventListener('mousedown', onMouseDown);
+ document.addEventListener('mouseup', onMouseUp);
- const storeSelection = useCallback(() => {
- decorateStateContext.add({
- range: editor.selection as BaseRange,
- class_name: 'bg-content-blue-100',
- });
- const windowSelection = window.getSelection();
+ return () => {
+ el.removeEventListener('mousedown', onMouseDown);
+ document.removeEventListener('mouseup', onMouseUp);
+ };
+ }, [visible, editor, ref]);
- if (!windowSelection) return;
+ useEffect(() => {
+ const slateEditorDom = ReactEditor.toDOMNode(editor, editor);
+ const onKeyDown = (e: KeyboardEvent) => {
+ // Close toolbar when press ESC
+ if (e.key === 'Escape') {
+ e.preventDefault();
+ e.stopPropagation();
+ editor.collapse({
+ edge: 'end',
+ });
+ debounceRecalculatePosition.cancel();
+ closeToolbar();
+ }
+ };
- if (windowSelection.rangeCount === 0) return;
+ if (visible) {
+ slateEditorDom.addEventListener('keydown', onKeyDown);
+ } else {
+ slateEditorDom.removeEventListener('keydown', onKeyDown);
+ }
- rangeRef.current = windowSelection.getRangeAt(0);
- }, [decorateStateContext, editor.selection]);
+ return () => {
+ slateEditorDom.removeEventListener('keydown', onKeyDown);
+ };
+ }, [closeToolbar, debounceRecalculatePosition, editor, visible]);
return {
visible,
restoreSelection,
storeSelection,
+ isAcrossBlocks,
};
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/SelectionToolbar.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/SelectionToolbar.tsx
index 8928e9b219..214fa1730a 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/SelectionToolbar.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/SelectionToolbar.tsx
@@ -1,31 +1,32 @@
import React, { memo, useRef } from 'react';
import { useSelectionToolbar } from '$app/components/editor/components/tools/selection_toolbar/SelectionToolbar.hooks';
import SelectionActions from '$app/components/editor/components/tools/selection_toolbar/SelectionActions';
+import withErrorBoundary from '$app/components/_shared/error_boundary/withError';
-export const SelectionToolbar = memo(() => {
+const Toolbar = memo(() => {
const ref = useRef<HTMLDivElement | null>(null);
- const { visible, restoreSelection, storeSelection } = useSelectionToolbar(ref);
+ const { visible, restoreSelection, storeSelection, isAcrossBlocks } = useSelectionToolbar(ref);
return (
<div
ref={ref}
className={
- 'selection-toolbar pointer-events-none absolute z-[100] flex min-h-[37px] w-fit flex-grow transform items-center rounded-lg bg-[var(--fill-toolbar)] px-2 opacity-0 shadow-lg transition-opacity'
+ 'selection-toolbar pointer-events-none absolute z-[100] flex min-h-[32px] w-fit flex-grow items-center rounded-lg bg-[var(--fill-toolbar)] px-2 opacity-0 shadow-lg'
}
onMouseDown={(e) => {
// prevent toolbar from taking focus away from editor
e.preventDefault();
}}
- onMouseUp={(e) => {
- e.stopPropagation();
- }}
>
- {visible ? (
- <SelectionActions storeSelection={storeSelection} restoreSelection={restoreSelection} />
- ) : (
- <div className={'w-[541px]'} />
- )}
+ <SelectionActions
+ isAcrossBlocks={isAcrossBlocks}
+ storeSelection={storeSelection}
+ restoreSelection={restoreSelection}
+ visible={visible}
+ />
</div>
);
});
+
+export const SelectionToolbar = withErrorBoundary(Toolbar);
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/_shared/ActionButton.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/_shared/ActionButton.tsx
index 0a81dea859..21cdb4d614 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/_shared/ActionButton.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/_shared/ActionButton.tsx
@@ -5,12 +5,12 @@ import { Tooltip } from '@mui/material';
const ActionButton = forwardRef<
HTMLButtonElement,
{
- tooltip: string;
+ tooltip: string | React.ReactNode;
onClick?: (e: React.MouseEvent<HTMLButtonElement>) => void;
children: React.ReactNode;
active?: boolean;
} & IconButtonProps
->(({ tooltip, onClick, children, active, className, ...props }, ref) => {
+>(({ tooltip, onClick, disabled, children, active, className, ...props }, ref) => {
return (
<Tooltip placement={'top'} title={tooltip}>
<IconButton
@@ -18,10 +18,13 @@ const ActionButton = forwardRef<
onClick={onClick}
size={'small'}
style={{
- color: active ? 'var(--fill-default)' : undefined,
+ color: active ? 'var(--fill-default)' : disabled ? 'var(--line-on-toolbar)' : undefined,
}}
+ disabled={disabled}
{...props}
- className={`${className ?? ''} bg-transparent px-1 py-2 text-bg-body hover:bg-transparent hover:text-fill-hover`}
+ className={`${
+ className ?? ''
+ } bg-transparent px-1 py-1 text-icon-on-toolbar hover:bg-transparent hover:text-fill-hover`}
>
{children}
</IconButton>
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/align/Align.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/align/Align.tsx
index 06625b7ec9..2bab1331ac 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/align/Align.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/align/Align.tsx
@@ -16,13 +16,13 @@ export function Align() {
const align = CustomEditor.getAlign(editor);
const [open, setOpen] = React.useState(false);
- const handleClose = () => {
+ const handleClose = useCallback(() => {
setOpen(false);
- };
+ }, []);
- const handleOpen = () => {
+ const handleOpen = useCallback(() => {
setOpen(true);
- };
+ }, []);
const Icon = useMemo(() => {
switch (align) {
@@ -41,9 +41,10 @@ export function Align() {
(align: string) => {
return () => {
CustomEditor.toggleAlign(editor, align);
+ handleClose();
};
},
- [editor]
+ [editor, handleClose]
);
const getAlignIcon = useCallback((key: string) => {
@@ -72,9 +73,10 @@ export function Align() {
return (
<IconButton
key={key}
- className={'text-content-on-fill hover:bg-transparent hover:text-content-blue-400'}
+ className={`text-icon-on-toolbar ${
+ align === key ? 'text-fill-hover' : ''
+ } hover:bg-transparent hover:text-content-blue-400`}
onClick={toggleAlign(key)}
- disabled={align === key}
>
{getAlignIcon(key)}
</IconButton>
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/bold/Bold.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/bold/Bold.tsx
index 67ae0364a5..fe2b690068 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/bold/Bold.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/bold/Bold.tsx
@@ -1,16 +1,17 @@
-import React, { useCallback } from 'react';
+import React, { useCallback, useMemo } from 'react';
import ActionButton from '$app/components/editor/components/tools/selection_toolbar/actions/_shared/ActionButton';
import { useTranslation } from 'react-i18next';
import { useSlateStatic } from 'slate-react';
import { CustomEditor } from '$app/components/editor/command';
import { ReactComponent as BoldSvg } from '$app/assets/bold.svg';
import { EditorMarkFormat } from '$app/application/document/document.types';
+import { getHotKey } from '$app/components/editor/plugins/shortcuts';
export function Bold() {
const { t } = useTranslation();
const editor = useSlateStatic();
const isActivated = CustomEditor.isMarkActive(editor, EditorMarkFormat.Bold);
-
+ const modifier = useMemo(() => getHotKey(EditorMarkFormat.Bold).modifier, []);
const onClick = useCallback(() => {
CustomEditor.toggleMark(editor, {
key: EditorMarkFormat.Bold,
@@ -19,7 +20,16 @@ export function Bold() {
}, [editor]);
return (
- <ActionButton onClick={onClick} active={isActivated} tooltip={t('editor.bold')}>
+ <ActionButton
+ onClick={onClick}
+ active={isActivated}
+ tooltip={
+ <>
+ <div>{t('toolbar.bold')}</div>
+ <div className={'text-xs text-text-caption'}>{modifier}</div>
+ </>
+ }
+ >
<BoldSvg />
</ActionButton>
);
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/color/Color.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/color/Color.tsx
index 372043f0f8..60c44423bd 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/color/Color.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/color/Color.tsx
@@ -1,58 +1,42 @@
-import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
+import React, { useCallback, useMemo, useRef, useState } from 'react';
import ActionButton from '$app/components/editor/components/tools/selection_toolbar/actions/_shared/ActionButton';
import { CustomEditor } from '$app/components/editor/command';
import { useTranslation } from 'react-i18next';
import { useSlateStatic } from 'slate-react';
import ColorLensOutlinedIcon from '@mui/icons-material/ColorLensOutlined';
-import { addMark } from 'slate';
-import { Popover } from '@mui/material';
-import { BgColorPicker, FontColorPicker } from '$app/components/editor/components/tools/_shared';
import { ReactComponent as MoreSvg } from '$app/assets/more.svg';
import { EditorMarkFormat } from '$app/application/document/document.types';
-import { PopoverNoBackdropProps } from '$app/components/editor/components/tools/popover';
+import debounce from 'lodash-es/debounce';
+import ColorPopover from './ColorPopover';
-export function Color({ onClose, onOpen }: { onClose?: () => void; onOpen?: () => void }) {
+export function Color(_: { onOpen?: () => void; onClose?: () => void }) {
const { t } = useTranslation();
const editor = useSlateStatic();
const [open, setOpen] = useState(false);
const ref = useRef<HTMLButtonElement>(null);
- useEffect(() => {
- if (open) {
- onOpen?.();
- } else {
- onClose?.();
- }
- }, [onClose, onOpen, open]);
const isActivated =
CustomEditor.isMarkActive(editor, EditorMarkFormat.FontColor) ||
CustomEditor.isMarkActive(editor, EditorMarkFormat.BgColor);
-
- const handleChange = useCallback(
- (format: EditorMarkFormat.FontColor | EditorMarkFormat.BgColor, color: string) => {
- addMark(editor, format, color);
- },
- [editor]
+ const debouncedClose = useMemo(
+ () =>
+ debounce(() => {
+ setOpen(false);
+ }, 200),
+ []
);
- const fontColor = useMemo(() => {
- return editor.getMarks()?.[EditorMarkFormat.FontColor];
- }, [editor]);
-
- const bgColor = useMemo(() => {
- return editor.getMarks()?.[EditorMarkFormat.BgColor];
- }, [editor]);
+ const handleOpen = useCallback(() => {
+ debouncedClose.cancel();
+ setOpen(true);
+ }, [debouncedClose]);
return (
<>
<ActionButton
ref={ref}
- onMouseEnter={() => {
- setOpen(true);
- }}
- onMouseLeave={() => {
- setOpen(false);
- }}
+ onMouseEnter={handleOpen}
+ onMouseLeave={debouncedClose}
tooltip={t('editor.color')}
active={isActivated}
>
@@ -60,36 +44,10 @@ export function Color({ onClose, onOpen }: { onClose?: () => void; onOpen?: () =
<ColorLensOutlinedIcon className={'w-[14px]'} />
<MoreSvg className={'rotate-90 transform'} />
</div>
- {open && (
- <Popover
- {...PopoverNoBackdropProps}
- open={open}
- anchorEl={ref.current}
- onClose={() => {
- setOpen(false);
- }}
- PaperProps={{
- ...PopoverNoBackdropProps.PaperProps,
- className: 'w-[200px] max-h-[360px] overflow-x-hidden overflow-y-auto',
- }}
- anchorOrigin={{
- vertical: 'bottom',
- horizontal: 'center',
- }}
- transformOrigin={{
- vertical: 'top',
- horizontal: 'center',
- }}
- disableAutoFocus={false}
- onKeyDown={(e) => {
- e.stopPropagation();
- }}
- >
- <FontColorPicker color={fontColor} onChange={(color) => handleChange(EditorMarkFormat.FontColor, color)} />
- <BgColorPicker color={bgColor} onChange={(color) => handleChange(EditorMarkFormat.BgColor, color)} />
- </Popover>
- )}
</ActionButton>
+ {open && ref.current && (
+ <ColorPopover open={open} onOpen={handleOpen} anchorEl={ref.current} debounceClose={debouncedClose} />
+ )}
</>
);
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/color/ColorPopover.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/color/ColorPopover.tsx
new file mode 100644
index 0000000000..faf4f2e9fc
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/color/ColorPopover.tsx
@@ -0,0 +1,92 @@
+import React, { useCallback } from 'react';
+import { PopoverNoBackdropProps } from '$app/components/editor/components/tools/popover';
+import { ColorPicker } from '$app/components/editor/components/tools/_shared';
+import { Popover } from '@mui/material';
+import { PopoverOrigin } from '@mui/material/Popover/Popover';
+import { EditorMarkFormat } from '$app/application/document/document.types';
+import { addMark, removeMark } from 'slate';
+import { useSlateStatic } from 'slate-react';
+import { DebouncedFunc } from 'lodash-es/debounce';
+import usePopoverAutoPosition from '$app/components/_shared/popover/Popover.hooks';
+
+const initialOrigin: {
+ transformOrigin?: PopoverOrigin;
+ anchorOrigin?: PopoverOrigin;
+} = {
+ anchorOrigin: {
+ vertical: 'bottom',
+ horizontal: 'center',
+ },
+ transformOrigin: {
+ vertical: 'top',
+ horizontal: 'center',
+ },
+};
+
+function ColorPopover({
+ open,
+ anchorEl,
+ debounceClose,
+}: {
+ open: boolean;
+ onOpen: () => void;
+ anchorEl: HTMLButtonElement | null;
+ debounceClose: DebouncedFunc<() => void>;
+}) {
+ const editor = useSlateStatic();
+ const handleChange = useCallback(
+ (format: EditorMarkFormat.FontColor | EditorMarkFormat.BgColor, color: string) => {
+ if (color) {
+ addMark(editor, format, color);
+ } else {
+ removeMark(editor, format);
+ }
+ },
+ [editor]
+ );
+
+ const { paperHeight, transformOrigin, anchorOrigin, isEntered } = usePopoverAutoPosition({
+ initialPaperWidth: 200,
+ initialPaperHeight: 360,
+ anchorEl,
+ initialAnchorOrigin: initialOrigin.anchorOrigin,
+ initialTransformOrigin: initialOrigin.transformOrigin,
+ open,
+ });
+
+ return (
+ <Popover
+ {...PopoverNoBackdropProps}
+ open={open && isEntered}
+ anchorEl={anchorEl}
+ onClose={debounceClose}
+ PaperProps={{
+ ...PopoverNoBackdropProps.PaperProps,
+ className: 'w-[200px] overflow-hidden',
+ style: {
+ ...PopoverNoBackdropProps.PaperProps?.style,
+ height: paperHeight,
+ },
+ }}
+ anchorOrigin={anchorOrigin}
+ transformOrigin={transformOrigin}
+ onKeyDown={(e) => {
+ e.stopPropagation();
+ if (e.key === 'Escape') {
+ debounceClose();
+ }
+ }}
+ onMouseDown={(e) => {
+ e.preventDefault();
+ }}
+ onMouseEnter={() => {
+ debounceClose.cancel();
+ }}
+ onMouseLeave={debounceClose}
+ >
+ <ColorPicker disableFocus={true} onEscape={debounceClose} onChange={handleChange} />
+ </Popover>
+ );
+}
+
+export default ColorPopover;
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/formula/Formula.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/formula/Formula.tsx
index beb71cb59c..5aa190d1a7 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/formula/Formula.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/formula/Formula.tsx
@@ -4,19 +4,36 @@ import { useTranslation } from 'react-i18next';
import { useSlateStatic } from 'slate-react';
import { CustomEditor } from '$app/components/editor/command';
import Functions from '@mui/icons-material/Functions';
+import { useEditorInlineBlockState } from '$app/components/editor/stores';
export function Formula() {
const { t } = useTranslation();
const editor = useSlateStatic();
- const isActivated = CustomEditor.isFormulaActive(editor);
+ const isActivatedMention = CustomEditor.isMentionActive(editor);
+ const isActivated = !isActivatedMention && CustomEditor.isFormulaActive(editor);
+
+ const { setRange, openPopover } = useEditorInlineBlockState('formula');
const onClick = useCallback(() => {
+ const selection = editor.selection;
+
+ if (!selection) return;
CustomEditor.toggleFormula(editor);
- }, [editor]);
+
+ requestAnimationFrame(() => {
+ setRange(selection);
+ openPopover();
+ });
+ }, [editor, setRange, openPopover]);
return (
- <ActionButton onClick={onClick} active={isActivated} tooltip={t('document.plugins.createInlineMathEquation')}>
- <Functions className={'w-[14px]'} />
+ <ActionButton
+ disabled={isActivatedMention}
+ onClick={onClick}
+ active={isActivated}
+ tooltip={t('document.plugins.createInlineMathEquation')}
+ >
+ <Functions className={`w-[14px]`} />
</ActionButton>
);
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/href/Href.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/href/Href.tsx
index fa1fee27c0..7da1a3f45d 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/href/Href.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/href/Href.tsx
@@ -1,31 +1,83 @@
-import React, { useCallback } from 'react';
+import React, { useCallback, useMemo } from 'react';
import ActionButton from '$app/components/editor/components/tools/selection_toolbar/actions/_shared/ActionButton';
import { useTranslation } from 'react-i18next';
-import { useSlateStatic } from 'slate-react';
+import { ReactEditor, useSlateStatic } from 'slate-react';
import { CustomEditor } from '$app/components/editor/command';
import { ReactComponent as LinkSvg } from '$app/assets/link.svg';
import { Editor } from 'slate';
import { EditorMarkFormat } from '$app/application/document/document.types';
+import { useDecorateDispatch, useDecorateState } from '$app/components/editor/stores';
+import { LinkEditPopover } from '$app/components/editor/components/inline_nodes/link';
export function Href() {
const { t } = useTranslation();
const editor = useSlateStatic();
- const isActivated = CustomEditor.isMarkActive(editor, EditorMarkFormat.Href);
+ const isActivatedInline = CustomEditor.isInlineActive(editor);
+ const isActivated = !isActivatedInline && CustomEditor.isMarkActive(editor, EditorMarkFormat.Href);
+ const decorateState = useDecorateState('link');
+ const openEditPopover = !!decorateState;
+ const anchorPosition = useMemo(() => {
+ const range = decorateState?.range;
+
+ if (!range) return;
+
+ const domRange = ReactEditor.toDOMRange(editor, range);
+
+ const rect = domRange.getBoundingClientRect();
+
+ return {
+ top: rect.top,
+ left: rect.left,
+ height: rect.height,
+ };
+ }, [decorateState?.range, editor]);
+
+ const defaultHref = useMemo(() => {
+ const range = decorateState?.range;
+
+ if (!range) return '';
+
+ const marks = Editor.marks(editor);
+
+ return marks?.href || Editor.string(editor, range);
+ }, [decorateState?.range, editor]);
+
+ const { add: addDecorate, clear: clearDecorate } = useDecorateDispatch();
const onClick = useCallback(() => {
if (!editor.selection) return;
- const text = Editor.string(editor, editor.selection);
-
- CustomEditor.toggleMark(editor, {
- key: EditorMarkFormat.Href,
- value: text,
+ addDecorate({
+ range: editor.selection,
+ class_name: 'bg-content-blue-100 rounded',
+ type: 'link',
});
- }, [editor]);
+ }, [addDecorate, editor]);
+
+ const handleEditPopoverClose = useCallback(() => {
+ const range = decorateState?.range;
+
+ clearDecorate();
+ if (range) {
+ ReactEditor.focus(editor);
+ editor.select(range);
+ }
+ }, [clearDecorate, decorateState?.range, editor]);
return (
- <ActionButton onClick={onClick} active={isActivated} tooltip={t('editor.link')}>
- <LinkSvg />
- </ActionButton>
+ <>
+ <ActionButton disabled={isActivatedInline} onClick={onClick} active={isActivated} tooltip={t('editor.link')}>
+ <LinkSvg />
+ </ActionButton>
+ {openEditPopover && (
+ <LinkEditPopover
+ open={openEditPopover}
+ anchorPosition={anchorPosition}
+ anchorReference={'anchorPosition'}
+ onClose={handleEditPopoverClose}
+ defaultHref={defaultHref}
+ />
+ )}
+ </>
);
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/inline_code/InlineCode.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/inline_code/InlineCode.tsx
index 4894f4748b..ef761e5c8c 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/inline_code/InlineCode.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/inline_code/InlineCode.tsx
@@ -1,15 +1,17 @@
-import React, { useCallback } from 'react';
+import React, { useCallback, useMemo } from 'react';
import ActionButton from '$app/components/editor/components/tools/selection_toolbar/actions/_shared/ActionButton';
import { useTranslation } from 'react-i18next';
import { useSlateStatic } from 'slate-react';
import { CustomEditor } from '$app/components/editor/command';
import { ReactComponent as CodeSvg } from '$app/assets/inline-code.svg';
import { EditorMarkFormat } from '$app/application/document/document.types';
+import { getHotKey } from '$app/components/editor/plugins/shortcuts';
export function InlineCode() {
const { t } = useTranslation();
const editor = useSlateStatic();
const isActivated = CustomEditor.isMarkActive(editor, EditorMarkFormat.Code);
+ const modifier = useMemo(() => getHotKey(EditorMarkFormat.Code).modifier, []);
const onClick = useCallback(() => {
CustomEditor.toggleMark(editor, {
@@ -19,7 +21,16 @@ export function InlineCode() {
}, [editor]);
return (
- <ActionButton onClick={onClick} active={isActivated} tooltip={t('editor.embedCode')}>
+ <ActionButton
+ onClick={onClick}
+ active={isActivated}
+ tooltip={
+ <>
+ <div>{t('editor.embedCode')}</div>
+ <div className={'text-xs text-text-caption'}>{modifier}</div>
+ </>
+ }
+ >
<CodeSvg />
</ActionButton>
);
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/italic/Italic.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/italic/Italic.tsx
index 0f796d955d..cb3f618e24 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/italic/Italic.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/italic/Italic.tsx
@@ -1,15 +1,17 @@
-import React, { useCallback } from 'react';
+import React, { useCallback, useMemo } from 'react';
import ActionButton from '$app/components/editor/components/tools/selection_toolbar/actions/_shared/ActionButton';
import { useTranslation } from 'react-i18next';
import { useSlateStatic } from 'slate-react';
import { CustomEditor } from '$app/components/editor/command';
import { ReactComponent as ItalicSvg } from '$app/assets/italic.svg';
import { EditorMarkFormat } from '$app/application/document/document.types';
+import { getHotKey } from '$app/components/editor/plugins/shortcuts';
export function Italic() {
const { t } = useTranslation();
const editor = useSlateStatic();
const isActivated = CustomEditor.isMarkActive(editor, EditorMarkFormat.Italic);
+ const modifier = useMemo(() => getHotKey(EditorMarkFormat.Italic).modifier, []);
const onClick = useCallback(() => {
CustomEditor.toggleMark(editor, {
@@ -19,7 +21,16 @@ export function Italic() {
}, [editor]);
return (
- <ActionButton onClick={onClick} active={isActivated} tooltip={t('editor.italic')}>
+ <ActionButton
+ onClick={onClick}
+ active={isActivated}
+ tooltip={
+ <>
+ <div>{t('toolbar.italic')}</div>
+ <div className={'text-xs text-text-caption'}>{modifier}</div>
+ </>
+ }
+ >
<ItalicSvg />
</ActionButton>
);
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/strikethrough/StrikeThrough.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/strikethrough/StrikeThrough.tsx
index d55a9c71f0..5cce3a603d 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/strikethrough/StrikeThrough.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/strikethrough/StrikeThrough.tsx
@@ -1,15 +1,17 @@
-import React, { useCallback } from 'react';
+import React, { useCallback, useMemo } from 'react';
import ActionButton from '$app/components/editor/components/tools/selection_toolbar/actions/_shared/ActionButton';
import { useTranslation } from 'react-i18next';
import { useSlateStatic } from 'slate-react';
import { CustomEditor } from '$app/components/editor/command';
import { ReactComponent as StrikeThroughSvg } from '$app/assets/strikethrough.svg';
import { EditorMarkFormat } from '$app/application/document/document.types';
+import { getHotKey } from '$app/components/editor/plugins/shortcuts';
export function StrikeThrough() {
const { t } = useTranslation();
const editor = useSlateStatic();
const isActivated = CustomEditor.isMarkActive(editor, EditorMarkFormat.StrikeThrough);
+ const modifier = useMemo(() => getHotKey(EditorMarkFormat.StrikeThrough).modifier, []);
const onClick = useCallback(() => {
CustomEditor.toggleMark(editor, {
@@ -19,7 +21,16 @@ export function StrikeThrough() {
}, [editor]);
return (
- <ActionButton onClick={onClick} active={isActivated} tooltip={t('editor.strikethrough')}>
+ <ActionButton
+ onClick={onClick}
+ active={isActivated}
+ tooltip={
+ <>
+ <div>{t('editor.strikethrough')}</div>
+ <div className={'text-xs text-text-caption'}>{modifier}</div>
+ </>
+ }
+ >
<StrikeThroughSvg />
</ActionButton>
);
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/underline/Underline.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/underline/Underline.tsx
index b3b0e22b57..7d32b83795 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/underline/Underline.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/actions/underline/Underline.tsx
@@ -1,15 +1,17 @@
-import React, { useCallback } from 'react';
+import React, { useCallback, useMemo } from 'react';
import ActionButton from '$app/components/editor/components/tools/selection_toolbar/actions/_shared/ActionButton';
import { useTranslation } from 'react-i18next';
import { useSlateStatic } from 'slate-react';
import { CustomEditor } from '$app/components/editor/command';
import { ReactComponent as UnderlineSvg } from '$app/assets/underline.svg';
import { EditorMarkFormat } from '$app/application/document/document.types';
+import { getHotKey } from '$app/components/editor/plugins/shortcuts';
export function Underline() {
const { t } = useTranslation();
const editor = useSlateStatic();
const isActivated = CustomEditor.isMarkActive(editor, EditorMarkFormat.Underline);
+ const modifier = useMemo(() => getHotKey(EditorMarkFormat.Underline).modifier, []);
const onClick = useCallback(() => {
CustomEditor.toggleMark(editor, {
@@ -19,7 +21,16 @@ export function Underline() {
}, [editor]);
return (
- <ActionButton onClick={onClick} active={isActivated} tooltip={t('editor.underline')}>
+ <ActionButton
+ onClick={onClick}
+ active={isActivated}
+ tooltip={
+ <>
+ <div>{t('editor.underline')}</div>
+ <div className={'text-xs text-text-caption'}>{modifier}</div>
+ </>
+ }
+ >
<UnderlineSvg />
</ActionButton>
);
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/utils.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/utils.ts
index 7e137d1619..178da73df4 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/utils.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/tools/selection_toolbar/utils.ts
@@ -1,16 +1,7 @@
import { ReactEditor } from 'slate-react';
-import { Editor, Range } from 'slate';
import { getEditorDomNode } from '$app/components/editor/plugins/utils';
export function getSelectionPosition(editor: ReactEditor) {
- const { selection } = editor;
-
- const isFocused = ReactEditor.isFocused(editor);
-
- if (!selection || !isFocused || Range.isCollapsed(selection) || Editor.string(editor, selection) === '') {
- return null;
- }
-
const domSelection = window.getSelection();
const rangeCount = domSelection?.rangeCount;
@@ -26,7 +17,7 @@ export function getSelectionPosition(editor: ReactEditor) {
const domNodeRect = domNode.getBoundingClientRect();
// the default height of the toolbar is 30px
- const gap = 36;
+ const gap = 106;
if (rect) {
let relativeDomTop = rect.top - domNodeRect.top;
@@ -34,7 +25,7 @@ export function getSelectionPosition(editor: ReactEditor) {
// if the range is above the window, move the toolbar to the bottom of range
if (rect.top < gap) {
- relativeDomTop = rect.bottom - gap - domNodeRect.top;
+ relativeDomTop = -domNodeRect.top + gap;
}
newRect = {
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/editor.scss b/frontend/appflowy_tauri/src/appflowy_app/components/editor/editor.scss
new file mode 100644
index 0000000000..6cf7ec5e9f
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/editor.scss
@@ -0,0 +1,106 @@
+
+.block-element {
+ @apply my-[4px];
+
+}
+
+.block-element .block-element {
+ @apply mb-0;
+ margin-left: 24px;
+}
+
+
+
+.block-element.block-align-left {
+ > div > .text-element {
+ justify-content: flex-start;
+ }
+}
+.block-element.block-align-right {
+ > div > .text-element {
+ justify-content: flex-end;
+
+ }
+}
+.block-element.block-align-center {
+ > div > .text-element {
+ justify-content: center;
+
+ }
+
+
+}
+
+
+.block-element[data-block-type="todo_list"] .checked > .text-element {
+ text-decoration: line-through;
+ color: var(--text-caption);
+}
+
+.block-element .collapsed .block-element {
+ display: none !important;
+}
+
+
+span[data-slate-placeholder="true"]:not(.inline-block-content) {
+ @apply text-text-placeholder;
+ opacity: 1 !important;
+}
+
+
+[role="textbox"] {
+ ::selection {
+ @apply bg-content-blue-100;
+ }
+}
+
+[data-dark-mode="true"] [role="textbox"]{
+ ::selection {
+ background-color: #1e79a2;
+ }
+}
+
+
+.text-element:has(.text-placeholder), .divider-node {
+ ::selection {
+ @apply bg-transparent;
+ }
+}
+
+.text-placeholder {
+
+ &:after {
+ @apply text-text-placeholder absolute left-1.5 top-1/2 transform -translate-y-1/2 pointer-events-none select-none whitespace-nowrap;
+ content: (attr(placeholder));
+ }
+}
+
+.has-start-icon > .text-placeholder {
+ &:after {
+ @apply left-[30px];
+ }
+}
+
+.formula-inline {
+ &.selected {
+ @apply rounded bg-content-blue-100;
+ }
+}
+
+.bulleted-icon {
+ &:after {
+ content: "•";
+ }
+}
+
+.numbered-icon {
+ &:after {
+ content: attr(data-number) ".";
+ }
+}
+
+
+.grid-block .grid-scroll-container::-webkit-scrollbar {
+ width: 0;
+ height: 0;
+}
\ No newline at end of file
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/shortcuts/hotkey.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/shortcuts/hotkey.ts
new file mode 100644
index 0000000000..0fb85a84ce
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/shortcuts/hotkey.ts
@@ -0,0 +1,84 @@
+import { EditorMarkFormat } from '$app/application/document/document.types';
+
+export const isMac = () => {
+ return navigator.userAgent.includes('Mac OS X');
+};
+
+const MODIFIERS = {
+ control: 'Ctrl',
+ meta: '⌘',
+};
+
+export const getModifier = () => {
+ return isMac() ? MODIFIERS.meta : MODIFIERS.control;
+};
+
+/**
+ * Hotkeys shortcuts
+ * @description
+ * - bold: Mod+b
+ * - italic: Mod+i
+ * - underline: Mod+u
+ * - strikethrough: Mod+Shift+s
+ * - code: Mod+Shift+c
+ */
+export const getHotKeys: () => {
+ [key: string]: { modifier: string; hotkey: string; markKey: EditorMarkFormat; markValue: string | boolean };
+} = () => {
+ const modifier = getModifier();
+
+ return {
+ [EditorMarkFormat.Bold]: {
+ hotkey: 'mod+b',
+ modifier: `${modifier} + B`,
+ markKey: EditorMarkFormat.Bold,
+ markValue: true,
+ },
+ [EditorMarkFormat.Italic]: {
+ hotkey: 'mod+i',
+ modifier: `${modifier} + I`,
+ markKey: EditorMarkFormat.Italic,
+ markValue: true,
+ },
+ [EditorMarkFormat.Underline]: {
+ hotkey: 'mod+u',
+ modifier: `${modifier} + U`,
+ markKey: EditorMarkFormat.Underline,
+ markValue: true,
+ },
+ [EditorMarkFormat.StrikeThrough]: {
+ hotkey: 'mod+shift+s',
+ modifier: `${modifier} + Shift + S`,
+ markKey: EditorMarkFormat.StrikeThrough,
+ markValue: true,
+ },
+ [EditorMarkFormat.Code]: {
+ hotkey: 'mod+shift+c',
+ modifier: `${modifier} + Shift + C`,
+ markKey: EditorMarkFormat.Code,
+ markValue: true,
+ },
+ 'align-left': {
+ hotkey: 'control+shift+l',
+ modifier: `Ctrl + Shift + L`,
+ markKey: EditorMarkFormat.Align,
+ markValue: 'left',
+ },
+ 'align-center': {
+ hotkey: 'control+shift+e',
+ modifier: `Ctrl + Shift + E`,
+ markKey: EditorMarkFormat.Align,
+ markValue: 'center',
+ },
+ 'align-right': {
+ hotkey: 'control+shift+r',
+ modifier: `Ctrl + Shift + R`,
+ markKey: EditorMarkFormat.Align,
+ markValue: 'right',
+ },
+ };
+};
+
+export const getHotKey = (key: EditorMarkFormat) => {
+ return getHotKeys()[key];
+};
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/shortcuts/index.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/shortcuts/index.ts
similarity index 72%
rename from frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/shortcuts/index.ts
rename to frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/shortcuts/index.ts
index 7cfd550743..fc262b9036 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/shortcuts/index.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/shortcuts/index.ts
@@ -1,2 +1,3 @@
export * from './shortcuts.hooks';
export * from './withShortcuts';
+export * from './hotkey';
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/shortcuts/shortcuts.hooks.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/shortcuts/shortcuts.hooks.ts
similarity index 58%
rename from frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/shortcuts/shortcuts.hooks.ts
rename to frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/shortcuts/shortcuts.hooks.ts
index c9894b5e8d..437ec6a576 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/shortcuts/shortcuts.hooks.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/shortcuts/shortcuts.hooks.ts
@@ -1,11 +1,34 @@
import { ReactEditor } from 'slate-react';
import { useCallback, KeyboardEvent } from 'react';
-import { EditorMarkFormat, EditorNodeType, TodoListNode } from '$app/application/document/document.types';
+import {
+ EditorMarkFormat,
+ EditorNodeType,
+ TodoListNode,
+ ToggleListNode,
+} from '$app/application/document/document.types';
import isHotkey from 'is-hotkey';
-
import { getBlock } from '$app/components/editor/plugins/utils';
import { SOFT_BREAK_TYPES } from '$app/components/editor/plugins/constants';
import { CustomEditor } from '$app/components/editor/command';
+import { getHotKeys } from '$app/components/editor/plugins/shortcuts/hotkey';
+
+/**
+ * Hotkeys shortcuts
+ * @description [getHotKeys] is defined in [hotkey.ts]
+ * - bold: Mod+b
+ * - italic: Mod+i
+ * - underline: Mod+u
+ * - strikethrough: Mod+Shift+s
+ * - code: Mod+Shift+c
+ * - align left: Mod+Shift+l
+ * - align center: Mod+Shift+e
+ * - align right: Mod+Shift+r
+ * - indent: Tab
+ * - outdent: Shift+Tab
+ * - split block: Enter
+ * - insert \n: Shift+Enter
+ * - toggle todo or toggle: Mod+Enter (toggle todo list or toggle list)
+ */
const inputTypeToFormat: Record<string, EditorMarkFormat> = {
formatBold: EditorMarkFormat.Bold,
@@ -15,14 +38,6 @@ const inputTypeToFormat: Record<string, EditorMarkFormat> = {
formatCode: EditorMarkFormat.Code,
};
-const hotKeys = {
- formatBold: 'Mod+b',
- formatItalic: 'Mod+i',
- formatUnderline: 'Mod+u',
- formatStrikethrough: 'Mod+Shift+s',
- formatCode: 'Mod+Shift+c',
-};
-
export function useShortcuts(editor: ReactEditor) {
const onDOMBeforeInput = useCallback(
(e: InputEvent) => {
@@ -47,19 +62,34 @@ export function useShortcuts(editor: ReactEditor) {
// Apple Webkit does not support the input event for formatting
if (isAppleWebkit) {
- Object.entries(hotKeys).forEach(([key, hotkey]) => {
- if (isHotkey(hotkey, e)) {
+ Object.entries(getHotKeys()).forEach(([_, item]) => {
+ if (isHotkey(item.hotkey, e)) {
+ e.stopPropagation();
e.preventDefault();
+
+ if (item.markKey === EditorMarkFormat.Align) {
+ CustomEditor.toggleAlign(editor, item.markValue as string);
+ return;
+ }
+
CustomEditor.toggleMark(editor, {
- key: inputTypeToFormat[key],
- value: true,
+ key: item.markKey,
+ value: item.markValue,
});
+ return;
}
});
}
const node = getBlock(editor);
+ if (isHotkey('Escape', e)) {
+ e.preventDefault();
+ e.stopPropagation();
+ editor.deselect();
+ return;
+ }
+
if (isHotkey('Tab', e)) {
e.preventDefault();
if (SOFT_BREAK_TYPES.includes(node?.type as EditorNodeType)) {
@@ -96,9 +126,25 @@ export function useShortcuts(editor: ReactEditor) {
return;
}
- if (isHotkey('mod+Enter', e) && node && node.type === EditorNodeType.TodoListBlock) {
+ if (isHotkey('mod+Enter', e) && node) {
+ if (node.type === EditorNodeType.TodoListBlock) {
+ e.preventDefault();
+ CustomEditor.toggleTodo(editor, node as TodoListNode);
+ return;
+ }
+
+ if (node.type === EditorNodeType.ToggleListBlock) {
+ e.preventDefault();
+ CustomEditor.toggleToggleList(editor, node as ToggleListNode);
+ return;
+ }
+ }
+
+ if (isHotkey('shift+backspace', e)) {
e.preventDefault();
- CustomEditor.toggleTodo(editor, node as TodoListNode);
+ e.stopPropagation();
+
+ editor.deleteBackward('character');
return;
}
},
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/shortcuts/withMarkdownShortcuts.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/shortcuts/withMarkdownShortcuts.ts
similarity index 68%
rename from frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/shortcuts/withMarkdownShortcuts.ts
rename to frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/shortcuts/withMarkdownShortcuts.ts
index 329e428c67..f49f4d9dda 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/components/editor/shortcuts/withMarkdownShortcuts.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/shortcuts/withMarkdownShortcuts.ts
@@ -3,6 +3,25 @@ import { Editor, Range, Element as SlateElement, Transforms } from 'slate';
import { EditorMarkFormat, EditorNodeType } from '$app/application/document/document.types';
import { CustomEditor } from '$app/components/editor/command';
+/**
+ * Markdown shortcuts
+ * @description
+ * - bold: **bold** or __bold__
+ * - italic: *italic* or _italic_
+ * - strikethrough: ~~strikethrough~~ or ~strikethrough~
+ * - code: `code`
+ * - heading: # or ## or ###
+ * - bulleted list: * or - or +
+ * - number list: 1. or 2. or 3.
+ * - toggle list: >
+ * - quote: ” or “ or "
+ * - todo list: -[ ] or -[x] or -[] or [] or [x] or [ ]
+ * - code block: ```
+ * - callout: [!TIP] or [!INFO] or [!WARNING] or [!DANGER]
+ * - divider: ---or***
+ * - equation: $$formula$$
+ */
+
const regexMap: Record<
string,
{
@@ -12,7 +31,7 @@ const regexMap: Record<
> = {
[EditorNodeType.BulletedListBlock]: [
{
- pattern: /^(\*|-|\+)$/,
+ pattern: /^([*\-+])$/,
},
],
[EditorNodeType.ToggleListBlock]: [
@@ -36,19 +55,19 @@ const regexMap: Record<
],
[EditorNodeType.TodoListBlock]: [
{
- pattern: /^\[ \]$/,
+ pattern: /^(-)?\[ ]$/,
data: {
checked: false,
},
},
{
- pattern: /^\[x\]$/,
+ pattern: /^(-)?\[x]$/,
data: {
checked: true,
},
},
{
- pattern: /^\[\]$/,
+ pattern: /^(-)?\[]$/,
data: {
checked: false,
},
@@ -61,7 +80,7 @@ const regexMap: Record<
],
[EditorNodeType.HeadingBlock]: [
{
- pattern: /^#{1}$/,
+ pattern: /^#$/,
data: {
level: 1,
},
@@ -81,29 +100,55 @@ const regexMap: Record<
],
[EditorNodeType.CodeBlock]: [
{
- pattern: /^```$/,
+ pattern: /^(`{3,})$/,
data: {
- language: 'javascript',
+ language: 'json',
},
},
],
[EditorNodeType.CalloutBlock]: [
{
- pattern: /^(\[!)(TIP|INFO|WARNING|DANGER)(\])$/,
+ pattern: /^\[!TIP]$/,
+ data: {
+ icon: '💡',
+ },
+ },
+ {
+ pattern: /^\[!INFO]$/,
+ data: {
+ icon: 'ℹ️',
+ },
+ },
+ {
+ pattern: /^\[!WARNING]$/,
+ data: {
+ icon: '⚠️',
+ },
+ },
+ {
+ pattern: /^\[!DANGER]$/,
+ data: {
+ icon: '🚨',
+ },
},
],
[EditorNodeType.DividerBlock]: [
{
- pattern: /^(-{3,})$/,
+ pattern: /^(([-*]){3,})$/,
},
],
[EditorNodeType.EquationBlock]: [
{
- pattern: /^(\${2})(\s)*(.+)(\s)*(\${2})$/,
+ pattern: /^\$\$(.*)\$\$$/,
+ data: {
+ formula: '',
+ },
},
],
};
+const blockCommands = [' ', '-', '`', '$', '*'];
+
const CharToMarkTypeMap: Record<string, EditorMarkFormat> = {
'**': EditorMarkFormat.Bold,
__: EditorMarkFormat.Bold,
@@ -114,8 +159,12 @@ const CharToMarkTypeMap: Record<string, EditorMarkFormat> = {
'`': EditorMarkFormat.Code,
};
-const matchShortcutType = (beforeText: string, endChar: string) => {
- if (endChar === '-') {
+const inlineBlockCommands = ['*', '_', '~', '`'];
+const doubleCharCommands = ['*', '_', '~'];
+
+const matchBlockShortcutType = (beforeText: string, endChar: string) => {
+ // end with divider char: -
+ if (endChar === '-' || endChar === '*') {
const dividerRegex = regexMap[EditorNodeType.DividerBlock][0];
return dividerRegex.pattern.test(beforeText + endChar)
@@ -126,6 +175,35 @@ const matchShortcutType = (beforeText: string, endChar: string) => {
: null;
}
+ // end with code block char: `
+ if (endChar === '`') {
+ const codeBlockRegex = regexMap[EditorNodeType.CodeBlock][0];
+
+ return codeBlockRegex.pattern.test(beforeText + endChar)
+ ? {
+ type: EditorNodeType.CodeBlock,
+ data: codeBlockRegex.data,
+ }
+ : null;
+ }
+
+ if (endChar === '$') {
+ const equationBlockRegex = regexMap[EditorNodeType.EquationBlock][0];
+
+ const match = equationBlockRegex.pattern.exec(beforeText + endChar);
+
+ const formula = match?.[1];
+
+ return equationBlockRegex.pattern.test(beforeText + endChar)
+ ? {
+ type: EditorNodeType.EquationBlock,
+ data: {
+ formula,
+ },
+ }
+ : null;
+ }
+
for (const [type, regexes] of Object.entries(regexMap)) {
for (const regex of regexes) {
if (regex.pattern.test(beforeText)) {
@@ -156,9 +234,56 @@ export const withMarkdownShortcuts = (editor: ReactEditor) => {
return;
}
+ // block shortcuts
+ if (blockCommands.some((char) => text.endsWith(char))) {
+ const endChar = text.slice(-1);
+ const [match] = Editor.nodes(editor, {
+ match: (n) => !Editor.isEditor(n) && SlateElement.isElement(n) && n.type === EditorNodeType.Text,
+ });
+
+ if (!match) {
+ insertText(text);
+ return;
+ }
+
+ const [, path] = match;
+
+ const { anchor } = selection;
+ const start = Editor.start(editor, path);
+ const range = { anchor, focus: start };
+ const beforeText = Editor.string(editor, range) + text.slice(0, -1);
+
+ if (beforeText === undefined) {
+ insertText(text);
+ return;
+ }
+
+ const matchItem = matchBlockShortcutType(beforeText, endChar);
+
+ if (matchItem) {
+ const { type, data } = matchItem;
+
+ Transforms.select(editor, range);
+
+ if (!Range.isCollapsed(range)) {
+ Transforms.delete(editor);
+ }
+
+ const newProperties: Partial<SlateElement> = {
+ type,
+ data,
+ };
+
+ CustomEditor.turnToBlock(editor, newProperties);
+
+ return;
+ }
+ }
+
+ // inline shortcuts
// end with inline mark char: * or _ or ~ or `
// eg: **bold** or *italic* or ~strikethrough~ or `code` or _italic_ or __bold__ or ~~strikethrough~~
- const keyword = ['*', '_', '~', '`'].find((char) => text.endsWith(char));
+ const keyword = inlineBlockCommands.find((char) => text.endsWith(char));
if (keyword !== undefined) {
const { focus } = selection;
@@ -179,7 +304,7 @@ export const withMarkdownShortcuts = (editor: ReactEditor) => {
let matchChar = keyword;
- if (['*', '_', '~'].includes(keyword)) {
+ if (doubleCharCommands.includes(keyword)) {
const doubleKeyword = `${keyword}${keyword}`;
if (rangeText.includes(doubleKeyword)) {
@@ -220,52 +345,6 @@ export const withMarkdownShortcuts = (editor: ReactEditor) => {
return;
}
- // end with space
- if (text.endsWith(' ') || text.endsWith('-')) {
- const endChar = text.slice(-1);
- const [match] = Editor.nodes(editor, {
- match: (n) => !Editor.isEditor(n) && SlateElement.isElement(n) && n.type === EditorNodeType.Text,
- });
-
- if (!match) {
- insertText(text);
- return;
- }
-
- const [, path] = match;
-
- const { anchor } = selection;
- const start = Editor.start(editor, path);
- const range = { anchor, focus: start };
- const beforeText = Editor.string(editor, range) + text.slice(0, -1);
-
- if (beforeText === undefined) {
- insertText(text);
- return;
- }
-
- const matchItem = matchShortcutType(beforeText, endChar);
-
- if (matchItem) {
- const { type, data } = matchItem;
-
- Transforms.select(editor, range);
-
- if (!Range.isCollapsed(range)) {
- Transforms.delete(editor);
- }
-
- const newProperties: Partial<SlateElement> = {
- type,
- data,
- };
-
- CustomEditor.turnToBlock(editor, newProperties);
-
- return;
- }
- }
-
insertText(text);
};
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/shortcuts/withShortcuts.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/shortcuts/withShortcuts.ts
new file mode 100644
index 0000000000..42b37f2a0f
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/shortcuts/withShortcuts.ts
@@ -0,0 +1,6 @@
+import { ReactEditor } from 'slate-react';
+import { withMarkdownShortcuts } from '$app/components/editor/plugins/shortcuts/withMarkdownShortcuts';
+
+export function withShortcuts(editor: ReactEditor) {
+ return withMarkdownShortcuts(editor);
+}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/utils.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/utils.ts
index ba5cd46514..1421a3c93b 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/utils.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/utils.ts
@@ -5,11 +5,11 @@ import { CustomEditor } from '$app/components/editor/command';
export function getHeadingCssProperty(level: number) {
switch (level) {
case 1:
- return 'text-3xl py-[16px] font-bold';
+ return 'text-3xl pt-[10px] pb-[8px] font-bold';
case 2:
- return 'text-2xl py-[12px] font-bold';
+ return 'text-2xl pt-[8px] pb-[6px] font-bold';
case 3:
- return 'text-xl py-[8px] font-bold';
+ return 'text-xl pt-[4px] font-bold';
default:
return '';
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withBlockDelete.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withBlockDelete.ts
new file mode 100644
index 0000000000..02ca4dc43b
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withBlockDelete.ts
@@ -0,0 +1,195 @@
+import { ReactEditor } from 'slate-react';
+import { Editor, Element, NodeEntry, Path, Range } from 'slate';
+import { EditorNodeType } from '$app/application/document/document.types';
+import { CustomEditor } from '$app/components/editor/command';
+
+/**
+ * Delete backward.
+ * | -> cursor
+ *
+ * ------------------- delete backward to its previous sibling lift all children
+ * 1 1|2
+ * |2 3
+ * 3 => delete backward => 4
+ * 4 5
+ * 5
+ * ------------------- delete backward to its parent and lift all children
+ * 1 1|2
+ * |2 3
+ * 3 => delete backward => 4
+ * 4 5
+ * 5
+ * ------------------- outdent the node if the node has no children
+ * 1 1
+ * 2 2
+ * |3 |3
+ * 4 => delete backward => 4
+ * @param editor
+ */
+export function withBlockDelete(editor: ReactEditor) {
+ const { deleteBackward, deleteFragment, mergeNodes } = editor;
+
+ editor.deleteBackward = (unit) => {
+ const match = CustomEditor.getBlock(editor);
+
+ if (!match || !CustomEditor.focusAtStartOfBlock(editor)) {
+ deleteBackward(unit);
+ return;
+ }
+
+ const [node, path] = match;
+
+ const isEmbed = editor.isEmbed(node);
+
+ if (isEmbed) {
+ CustomEditor.deleteNode(editor, node);
+ return;
+ }
+
+ const previous = editor.previous({
+ at: path,
+ match: (n) => !Editor.isEditor(n) && Element.isElement(n) && n.blockId !== undefined,
+ });
+ const [previousNode] = previous || [undefined, undefined];
+
+ const previousIsPage = previousNode && Element.isElement(previousNode) && previousNode.type === EditorNodeType.Page;
+
+ // merge to document title
+ if (previousIsPage) {
+ const textNodePath = [...path, 0];
+ const [textNode] = editor.node(textNodePath);
+ const text = CustomEditor.getNodeTextContent(textNode);
+
+ // clear all attributes
+ editor.select(textNodePath);
+ CustomEditor.removeMarks(editor);
+ editor.insertText(text);
+
+ editor.move({
+ distance: text.length,
+ reverse: true,
+ });
+ }
+
+ // if the current node is not a paragraph, convert it to a paragraph
+ if (node.type !== EditorNodeType.Paragraph && node.type !== EditorNodeType.Page) {
+ CustomEditor.turnToBlock(editor, { type: EditorNodeType.Paragraph });
+ return;
+ }
+
+ const next = editor.next({
+ at: path,
+ });
+
+ if (!next && path.length > 1) {
+ CustomEditor.tabBackward(editor);
+ return;
+ }
+
+ const length = node.children.length;
+
+ for (let i = length - 1; i > 0; i--) {
+ editor.liftNodes({
+ at: [...path, i],
+ });
+ }
+
+ deleteBackward(unit);
+ };
+
+ editor.deleteFragment = (...args) => {
+ beforeDeleteToDocumentTitle(editor);
+
+ deleteFragment(...args);
+ };
+
+ editor.mergeNodes = (options) => {
+ mergeNodes(options);
+ if (!editor.selection || !options?.at) return;
+ const nextPath = findNextPath(editor, editor.selection.anchor.path);
+
+ const [nextNode] = editor.node(nextPath);
+
+ if (Element.isElement(nextNode) && nextNode.blockId !== undefined && nextNode.children.length === 0) {
+ editor.removeNodes({
+ at: nextPath,
+ });
+ }
+
+ return;
+ };
+
+ return editor;
+}
+
+function beforeDeleteToDocumentTitle(editor: ReactEditor) {
+ if (!editor.selection) return;
+ if (Range.isCollapsed(editor.selection)) return;
+ const start = Range.start(editor.selection);
+ const end = Range.end(editor.selection);
+ const startNode = editor.above({
+ at: start,
+ match: (n) => !Editor.isEditor(n) && Element.isElement(n) && n.type === EditorNodeType.Page,
+ });
+
+ const endNode = editor.above({
+ at: end,
+ match: (n) => !Editor.isEditor(n) && Element.isElement(n) && n.blockId !== undefined,
+ });
+
+ const startNodeIsPage = !!startNode;
+
+ if (!startNodeIsPage || !endNode) return;
+ const [node, path] = endNode as NodeEntry<Element>;
+ const selectedText = editor.string({
+ anchor: {
+ path,
+ offset: 0,
+ },
+ focus: end,
+ });
+
+ const nodeChildren = node.children;
+ const nodeChildrenLength = nodeChildren.length;
+
+ for (let i = nodeChildrenLength - 1; i > 0; i--) {
+ editor.liftNodes({
+ at: [...path, i],
+ });
+ }
+
+ const textNodePath = [...path, 0];
+ const [textNode] = editor.node(textNodePath);
+ const text = CustomEditor.getNodeTextContent(textNode);
+
+ // clear all attributes
+ editor.select([...path, 0]);
+ CustomEditor.removeMarks(editor);
+ editor.insertText(text);
+ editor.move({
+ distance: text.length - selectedText.length,
+ reverse: true,
+ });
+ editor.select({
+ anchor: start,
+ focus: editor.selection.focus,
+ });
+}
+
+function findNextPath(editor: ReactEditor, path: Path): Path {
+ if (path.length === 0) return path;
+ const parentPath = Path.parent(path);
+
+ try {
+ const nextPath = Path.next(path);
+ const [nextNode] = Editor.node(editor, nextPath);
+
+ if (Element.isElement(nextNode) && nextNode.blockId !== undefined) {
+ return nextPath;
+ }
+ } catch (e) {
+ // ignore
+ }
+
+ return findNextPath(editor, parentPath);
+}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withBlockDeleteBackward.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withBlockDeleteBackward.ts
deleted file mode 100644
index f2480d8c8e..0000000000
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withBlockDeleteBackward.ts
+++ /dev/null
@@ -1,50 +0,0 @@
-import { ReactEditor } from 'slate-react';
-
-import { EditorNodeType } from '$app/application/document/document.types';
-import { CustomEditor } from '$app/components/editor/command';
-
-export function withBlockDeleteBackward(editor: ReactEditor) {
- const { deleteBackward, removeNodes } = editor;
-
- editor.removeNodes = (...args) => {
- removeNodes(...args);
- };
-
- editor.deleteBackward = (unit) => {
- const match = CustomEditor.getBlock(editor);
-
- if (!match || !CustomEditor.focusAtStartOfBlock(editor)) {
- deleteBackward(unit);
- return;
- }
-
- const [node, path] = match;
-
- // if the current node is not a paragraph, convert it to a paragraph
- if (node.type !== EditorNodeType.Paragraph && node.type !== EditorNodeType.Page) {
- CustomEditor.turnToBlock(editor, { type: EditorNodeType.Paragraph });
- return;
- }
-
- const next = editor.next({
- at: path,
- });
-
- if (!next && path.length > 1) {
- CustomEditor.tabBackward(editor);
- return;
- }
-
- const [, ...children] = node.children;
-
- deleteBackward(unit);
-
- children.forEach((child, index) => {
- editor.liftNodes({
- at: [...path, index],
- });
- });
- };
-
- return editor;
-}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withBlockInsertBreak.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withBlockInsertBreak.ts
index 25d8d0eed3..cbb1816db2 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withBlockInsertBreak.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withBlockInsertBreak.ts
@@ -1,6 +1,8 @@
import { ReactEditor } from 'slate-react';
import { EditorNodeType } from '$app/application/document/document.types';
import { CustomEditor } from '$app/components/editor/command';
+import { Path } from 'slate';
+import { YjsEditor } from '@slate-yjs/core';
export function withBlockInsertBreak(editor: ReactEditor) {
const { insertBreak } = editor;
@@ -10,7 +12,18 @@ export function withBlockInsertBreak(editor: ReactEditor) {
if (!block) return insertBreak(...args);
- const [node] = block;
+ const [node, path] = block;
+
+ const isEmbed = editor.isEmbed(node);
+
+ if (isEmbed) {
+ const nextPath = Path.next(path);
+
+ CustomEditor.insertEmptyLine(editor as ReactEditor & YjsEditor, nextPath);
+ editor.select(nextPath);
+ return;
+ }
+
const type = node.type as EditorNodeType;
const isEmpty = CustomEditor.isEmptyText(editor, node);
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withBlockMove.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withBlockMove.ts
index d0f8fd7d5b..814c6e7333 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withBlockMove.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withBlockMove.ts
@@ -1,8 +1,16 @@
import { ReactEditor } from 'slate-react';
import { generateId } from '$app/components/editor/provider/utils/convert';
-import { Editor, Element, Location, NodeEntry, Path, Transforms } from 'slate';
+import { Editor, Element, Location, NodeEntry, Path, Node, Transforms } from 'slate';
import { EditorNodeType } from '$app/application/document/document.types';
+const matchPath = (editor: Editor, path: Path): ((node: Node) => boolean) => {
+ const [node] = Editor.node(editor, path);
+
+ return (n) => {
+ return n === node;
+ };
+};
+
export function withBlockMove(editor: ReactEditor) {
const { moveNodes } = editor;
@@ -20,7 +28,9 @@ export function withBlockMove(editor: ReactEditor) {
let { match } = args;
if (!match) {
- match = (n) => Element.isElement(n) && Editor.isBlock(editor, n) && n.blockId !== undefined;
+ match = Path.isPath(at)
+ ? matchPath(editor, at)
+ : (n) => Element.isElement(n) && Editor.isBlock(editor, n) && n.blockId !== undefined;
}
if (!at) {
@@ -28,7 +38,10 @@ export function withBlockMove(editor: ReactEditor) {
}
const matches = Editor.nodes(editor, { at, match, mode, voids });
- const pathRefs = Array.from(matches, ([, p]) => Editor.pathRef(editor, p));
+
+ const pathRefs = Array.from(matches, ([, p]) => {
+ return Editor.pathRef(editor, p);
+ });
for (const pathRef of pathRefs) {
const path = pathRef.unref();
@@ -57,15 +70,16 @@ export function withBlockMove(editor: ReactEditor) {
} else {
const toPath = Path.next(parentPath);
- parent.children.forEach((child, childIndex) => {
- if (childIndex > index) {
- Transforms.moveNodes(editor, {
- at: [...parentPath, index + 1],
- to: [...path, childIndex - index],
- mode: 'all',
- });
- }
- });
+ const node = parent.children[index] as Element;
+ const nodeChildrenLength = node.children.length;
+
+ for (let i = length - 1; i > index; i--) {
+ Transforms.moveNodes(editor, {
+ at: [...parentPath, i],
+ to: [...path, nodeChildrenLength],
+ mode: 'all',
+ });
+ }
Transforms.moveNodes(editor, { at: path, to: toPath, voids });
}
@@ -103,7 +117,7 @@ function replaceId(editor: Editor, at?: Location) {
}
);
- if (textNode.type === EditorNodeType.Text) {
+ if (textNode && textNode.type === EditorNodeType.Text) {
editor.setNodes(
{
textId: newTextId,
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withBlockPlugins.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withBlockPlugins.ts
index 66ed77fa2c..ee7489b8bf 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withBlockPlugins.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withBlockPlugins.ts
@@ -1,28 +1,30 @@
import { ReactEditor } from 'slate-react';
-import { withBlockDeleteBackward } from '$app/components/editor/plugins/withBlockDeleteBackward';
+import { withBlockDelete } from '$app/components/editor/plugins/withBlockDelete';
import { withBlockInsertBreak } from '$app/components/editor/plugins/withBlockInsertBreak';
import { withSplitNodes } from '$app/components/editor/plugins/withSplitNodes';
import { withPasted } from '$app/components/editor/plugins/withPasted';
import { withBlockMove } from '$app/components/editor/plugins/withBlockMove';
-import { EditorNodeType } from '$app/application/document/document.types';
-
-const EmbedTypes: string[] = [EditorNodeType.DividerBlock, EditorNodeType.EquationBlock, EditorNodeType.GridBlock];
+import { CustomEditor } from '$app/components/editor/command';
export function withBlockPlugins(editor: ReactEditor) {
- const { isElementReadOnly, isSelectable, isEmpty } = editor;
+ const { isElementReadOnly, isEmpty, isSelectable } = editor;
editor.isElementReadOnly = (element) => {
- return EmbedTypes.includes(element.type) || isElementReadOnly(element);
+ return CustomEditor.isEmbedNode(element) || isElementReadOnly(element);
+ };
+
+ editor.isEmbed = (element) => {
+ return CustomEditor.isEmbedNode(element);
};
editor.isSelectable = (element) => {
- return !EmbedTypes.includes(element.type) && isSelectable(element);
+ return !CustomEditor.isEmbedNode(element) && isSelectable(element);
};
editor.isEmpty = (element) => {
- return !EmbedTypes.includes(element.type) && isEmpty(element);
+ return !CustomEditor.isEmbedNode(element) && isEmpty(element);
};
- return withPasted(withBlockMove(withSplitNodes(withBlockInsertBreak(withBlockDeleteBackward(editor)))));
+ return withBlockMove(withSplitNodes(withBlockInsertBreak(withBlockDelete(withPasted(editor)))));
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withPasted.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withPasted.ts
index da607ba418..1973cdb5a4 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withPasted.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withPasted.ts
@@ -1,12 +1,47 @@
import { ReactEditor } from 'slate-react';
import { convertBlockToJson } from '$app/application/document/document.service';
-import { Editor, Element, NodeEntry, Path, Location, Range } from 'slate';
-import { generateId } from '$app/components/editor/provider/utils/convert';
+import { Editor, Element, NodeEntry, Path, Node, Text, Location, Range } from 'slate';
import { EditorNodeType } from '$app/application/document/document.types';
import { InputType } from '@/services/backend';
+import { CustomEditor } from '$app/components/editor/command';
+import { generateId } from '$app/components/editor/provider/utils/convert';
+import { LIST_TYPES } from '$app/components/editor/command/tab';
+import { Log } from '$app/utils/log';
export function withPasted(editor: ReactEditor) {
- const { insertData } = editor;
+ const { insertData, insertFragment, setFragmentData } = editor;
+
+ editor.setFragmentData = (...args) => {
+ if (!editor.selection) {
+ setFragmentData(...args);
+ return;
+ }
+
+ // selection is collapsed and the node is an embed, we need to set the data manually
+ if (Range.isCollapsed(editor.selection)) {
+ const match = Editor.above(editor, {
+ match: (n) => !Editor.isEditor(n) && Element.isElement(n) && n.blockId !== undefined,
+ });
+ const node = match ? (match[0] as Element) : undefined;
+
+ if (node && editor.isEmbed(node)) {
+ const fragment = editor.getFragment();
+
+ if (fragment.length > 0) {
+ const data = args[0];
+ const string = JSON.stringify(fragment);
+ const encoded = window.btoa(encodeURIComponent(string));
+
+ const dom = ReactEditor.toDOMNode(editor, node);
+
+ data.setData(`application/x-slate-fragment`, encoded);
+ data.setData(`text/html`, dom.innerHTML);
+ }
+ }
+ }
+
+ setFragmentData(...args);
+ };
editor.insertData = (data) => {
const fragment = data.getData('application/x-slate-fragment');
@@ -18,16 +53,35 @@ export function withPasted(editor: ReactEditor) {
const html = data.getData('text/html');
const text = data.getData('text/plain');
- const inputType = html ? InputType.Html : InputType.PlainText;
- if (html || text) {
- void convertBlockToJson(html || text, inputType).then((nodes) => {
- editor.insertFragment(nodes);
- });
+ if (!html && !text) {
+ insertData(data);
return;
}
- insertData(data);
+ void (async () => {
+ try {
+ const nodes = await convertBlockToJson(html, InputType.Html);
+
+ const htmlTransNoText = nodes.every((node) => {
+ return CustomEditor.getNodeTextContent(node).length === 0;
+ });
+
+ if (!htmlTransNoText) {
+ return editor.insertFragment(nodes);
+ }
+ } catch (e) {
+ Log.warn('pasted html error', e);
+ // ignore
+ }
+
+ if (text) {
+ const nodes = await convertBlockToJson(text, InputType.PlainText);
+
+ editor.insertFragment(nodes);
+ return;
+ }
+ })();
};
editor.insertFragment = (fragment, options = {}) => {
@@ -44,74 +98,124 @@ export function withPasted(editor: ReactEditor) {
});
}
- const mergedText = editor.above({
- match: (n) => !Editor.isEditor(n) && Element.isElement(n) && n.type === EditorNodeType.Text,
- }) as NodeEntry<
- Element & {
- textId: string;
- }
- >;
+ const selection = editor.selection;
- if (!mergedText) return;
+ if (!selection) return;
- const [mergedTextNode, mergedTextNodePath] = mergedText;
+ const [node] = editor.node(selection);
+ const isText = Text.isText(node);
+ const parent = Editor.above(editor, {
+ at: selection,
+ match: (n) => !Editor.isEditor(n) && Element.isElement(n) && n.blockId !== undefined,
+ });
- const traverse = (node: Element) => {
- if (node.type === EditorNodeType.Text) {
- node.textId = generateId();
- return;
- }
+ if (isText && parent) {
+ const [parentNode, parentPath] = parent as NodeEntry<Element>;
+ const pastedNodeIsPage = parentNode.type === EditorNodeType.Page;
+ const clonedFragment = transFragment(editor, fragment);
- node.blockId = generateId();
- node.children?.forEach((child) => traverse(child as Element));
- };
+ const [firstNode, ...otherNodes] = clonedFragment;
+ const lastNode = getLastNode(otherNodes[otherNodes.length - 1]);
+ const firstIsEmbed = editor.isEmbed(firstNode);
+ const insertNodes: Element[] = [...otherNodes];
+ const needMoveChildren = parentNode.children.length > 1 && !pastedNodeIsPage;
+ let moveStartIndex = 0;
- fragment?.forEach((node) => traverse(node as Element));
+ if (firstIsEmbed) {
+ insertNodes.unshift(firstNode);
+ } else {
+ // merge the first fragment node with the current text node
+ const [textNode, ...children] = firstNode.children as Element[];
- const firstNode = fragment[0] as Element;
+ const textElements = textNode.children;
- if (firstNode && firstNode.type !== 'text') {
- if (firstNode.children && firstNode.children.length > 0) {
- const [textNode, ...children] = firstNode.children;
+ const end = Editor.end(editor, [...parentPath, 0]);
- fragment[0] = textNode;
- fragment.splice(1, 0, ...children);
- } else {
- fragment.unshift(getEmptyText());
- }
- }
+ // merge text node
+ editor.insertNodes(textElements, {
+ at: end,
+ select: true,
+ });
- editor.insertNodes((fragment[0] as Element).children, {
- at: [...mergedTextNodePath, mergedTextNode.children.length],
- });
- editor.select(mergedTextNodePath);
- editor.collapse({
- edge: 'end',
- });
- const otherNodes = fragment.slice(1);
+ if (children.length > 0) {
+ if (pastedNodeIsPage) {
+ // lift the children of the first fragment node to current node
+ insertNodes.unshift(...children);
+ } else {
+ const lastChild = getLastNode(children[children.length - 1]);
- if (otherNodes.length > 0) {
- const parentPath = Path.parent(mergedTextNodePath);
+ const lastIsEmbed = lastChild && editor.isEmbed(lastChild);
- const nextPath = Path.next(parentPath);
- const lastNodeText = (otherNodes[otherNodes.length - 1] as Element).children?.[0] as Element;
+ // insert the children of the first fragment node to current node
+ editor.insertNodes(children, {
+ at: [...parentPath, 1],
+ select: !lastIsEmbed,
+ });
+
+ moveStartIndex += children.length;
+ }
+ }
+ }
- let canSelect = true;
+ if (insertNodes.length === 0) return;
- if (!lastNodeText || lastNodeText.type !== EditorNodeType.Text) {
- canSelect = false;
+ // insert a new paragraph if the last node is an embed
+ if ((!lastNode && firstIsEmbed) || (lastNode && editor.isEmbed(lastNode))) {
+ insertNodes.push(generateNewParagraph());
}
- editor.insertNodes(otherNodes, {
- at: nextPath,
- select: canSelect,
+ const pastedPath = Path.next(parentPath);
+
+ // insert the sibling of the current node
+ editor.insertNodes(insertNodes, {
+ at: pastedPath,
+ select: true,
});
- if (canSelect) {
- editor.collapse({
- edge: 'end',
- });
+ if (!needMoveChildren) return;
+
+ if (!editor.selection) return;
+
+ // current node is the last node of the pasted fragment
+ const currentPath = editor.selection.anchor.path;
+ const current = editor.above({
+ at: currentPath,
+ match: (n) => !Editor.isEditor(n) && Element.isElement(n) && n.blockId !== undefined,
+ });
+
+ if (!current) return;
+
+ const [currentNode, currentNodePath] = current as NodeEntry<Element>;
+
+ // split the operation into the next tick to avoid the wrong path
+ if (LIST_TYPES.includes(currentNode.type as EditorNodeType)) {
+ const length = currentNode.children.length;
+
+ setTimeout(() => {
+ // move the children of the current node to the last node of the pasted fragment
+ for (let i = parentNode.children.length - 1; i > 0; i--) {
+ editor.moveNodes({
+ at: [...parentPath, i + moveStartIndex],
+ to: [...currentNodePath, length],
+ });
+ }
+ }, 0);
+ } else {
+ // if the current node is not a list, we need to move these children to the next path
+ setTimeout(() => {
+ const nextPath = Path.next(currentNodePath);
+
+ for (let i = parentNode.children.length - 1; i > 0; i--) {
+ editor.moveNodes({
+ at: [...parentPath, i + moveStartIndex],
+ to: nextPath,
+ });
+ }
+ }, 0);
}
+ } else {
+ insertFragment(fragment);
+ return;
}
});
};
@@ -119,18 +223,6 @@ export function withPasted(editor: ReactEditor) {
return editor;
}
-function getEmptyText(): Element {
- return {
- type: EditorNodeType.Text,
- textId: generateId(),
- children: [
- {
- text: '',
- },
- ],
- };
-}
-
export const getDefaultInsertLocation = (editor: Editor): Location => {
if (editor.selection) {
return editor.selection;
@@ -140,3 +232,55 @@ export const getDefaultInsertLocation = (editor: Editor): Location => {
return [0];
}
};
+
+export const generateNewParagraph = (): Element => ({
+ type: EditorNodeType.Paragraph,
+ blockId: generateId(),
+ children: [
+ {
+ type: EditorNodeType.Text,
+ textId: generateId(),
+ children: [{ text: '' }],
+ },
+ ],
+});
+
+function getLastNode(node: Node): Element | undefined {
+ if (!Element.isElement(node) || node.blockId === undefined) return;
+
+ if (Element.isElement(node) && node.blockId !== undefined && node.children.length > 0) {
+ const child = getLastNode(node.children[node.children.length - 1]);
+
+ if (!child) {
+ return node;
+ } else {
+ return child;
+ }
+ }
+
+ return node;
+}
+
+function transFragment(editor: ReactEditor, fragment: Node[]) {
+ // flatten the fragment to avoid the empty node(doesn't have text node) in the fragment
+ const flatMap = (node: Node): Node[] => {
+ const isInputElement =
+ !Editor.isEditor(node) && Element.isElement(node) && node.blockId !== undefined && !editor.isEmbed(node);
+
+ if (
+ isInputElement &&
+ node.children?.length > 0 &&
+ Element.isElement(node.children[0]) &&
+ node.children[0].type !== EditorNodeType.Text
+ ) {
+ return node.children.flatMap((child) => flatMap(child));
+ }
+
+ return [node];
+ };
+
+ const fragmentFlatMap = fragment?.flatMap(flatMap);
+
+ // clone the node to avoid the duplicated block id
+ return fragmentFlatMap.map((item) => CustomEditor.cloneBlock(editor, item as Element));
+}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withSplitNodes.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withSplitNodes.ts
index 3db6ee5eb6..55c9b8b8f2 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withSplitNodes.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/plugins/withSplitNodes.ts
@@ -1,15 +1,37 @@
import { ReactEditor } from 'slate-react';
-import { Transforms, Editor, Element, NodeEntry, Path } from 'slate';
+import { Transforms, Editor, Element, NodeEntry, Path, Range } from 'slate';
import { EditorNodeType, ToggleListNode } from '$app/application/document/document.types';
import { CustomEditor } from '$app/components/editor/command';
import { generateId } from '$app/components/editor/provider/utils/convert';
import cloneDeep from 'lodash-es/cloneDeep';
import { SOFT_BREAK_TYPES } from '$app/components/editor/plugins/constants';
+/**
+ * Split nodes.
+ * split text node into two text nodes, and wrap the second text node with a new block node.
+ *
+ * Split to the first child condition:
+ * 1. block type is toggle list block, and the block is not collapsed.
+ *
+ * Split to the next sibling condition:
+ * 1. block type is toggle list block, and the block is collapsed.
+ * 2. block type is other block type.
+ *
+ * Split to a paragraph node: (otherwise split to the same block type)
+ * 1. block type is heading block.
+ * 2. block type is quote block.
+ * 3. block type is page.
+ * 4. block type is code block and callout block.
+ * 5. block type is paragraph.
+ *
+ * @param editor
+ */
export function withSplitNodes(editor: ReactEditor) {
const { splitNodes } = editor;
editor.splitNodes = (...args) => {
+ const selection = editor.selection;
+
const isInsertBreak = args.length === 1 && JSON.stringify(args[0]) === JSON.stringify({ always: true });
if (!isInsertBreak) {
@@ -17,6 +39,12 @@ export function withSplitNodes(editor: ReactEditor) {
return;
}
+ const isCollapsed = selection && Range.isCollapsed(selection);
+
+ if (!isCollapsed) {
+ editor.deleteFragment({ direction: 'backward' });
+ }
+
const match = CustomEditor.getBlock(editor);
if (!match) {
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/provider/utils/action.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/provider/utils/action.ts
index 06ea576239..1cd6e7c57a 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/provider/utils/action.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/provider/utils/action.ts
@@ -90,42 +90,7 @@ function blockOps2BlockActions(
let index = 0;
- let newOps = ops;
-
- if (ops.length > 1) {
- const [deleteOp, insertOp, ...otherOps] = newOps;
-
- const insert = insertOp.insert;
-
- if (deleteOp.delete === 1 && insert && insert instanceof Y.XmlText) {
- const textNode = getInsertTarget(blockYXmlText, [0]);
- const textId = textNode.getAttribute('textId');
-
- if (textId) {
- const length = textNode.length;
-
- insert.setAttribute('textId', textId);
-
- actions.push(
- ...generateApplyTextActions(insert, [
- {
- delete: length,
- },
- ...insert.toDelta(),
- ])
- );
- }
-
- newOps = [
- {
- retain: 1,
- },
- ...otherOps,
- ];
- }
- }
-
- newOps.forEach((op) => {
+ ops.forEach((op) => {
if (op.insert) {
if (op.insert instanceof Y.XmlText) {
const insertYXmlText = op.insert;
@@ -136,16 +101,34 @@ function blockOps2BlockActions(
throw new Error('blockId and textId is not exist');
}
- actions.push(...generateInsertBlockActions(insertYXmlText));
- index += 1;
+ if (blockId) {
+ actions.push(...generateInsertBlockActions(insertYXmlText));
+ index += 1;
+ }
+
+ if (textId) {
+ const target = getInsertTarget(blockYXmlText, [0]);
+
+ if (target) {
+ const length = target.length;
+
+ const delta = [{ delete: length }, ...insertYXmlText.toDelta()];
+
+ // restore textId
+ insertYXmlText.setAttribute('textId', target.getAttribute('textId'));
+ actions.push(...generateApplyTextActions(target, delta));
+ }
+ }
}
} else if (op.retain) {
index += op.retain;
} else if (op.delete) {
- for (let i = index; i < op.delete + index; i++) {
- const target = getInsertTarget(blockYXmlText, [i]);
+ let i = 0;
+
+ for (; i < op.delete; i++) {
+ const target = getInsertTarget(blockYXmlText, [i + index]);
- if (target) {
+ if (target && target !== blockYXmlText) {
const deletedId = target.getAttribute('blockId') as string;
if (deletedId) {
@@ -157,6 +140,8 @@ function blockOps2BlockActions(
}
}
}
+
+ index += i;
}
});
@@ -238,6 +223,7 @@ export function generateInsertBlockActions(
insertYXmlText: Y.XmlText
): ReturnType<typeof BlockActionPB.prototype.toObject>[] {
const childrenId = generateId();
+
const [textInsert, ...childrenInserts] = (insertYXmlText.toDelta() as YDelta).map((op) => op.insert);
const textInsertActions = textInsert instanceof Y.XmlText ? generateInsertTextActions(textInsert) : [];
const externalId = textInsertActions[0]?.payload.text_id;
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/provider/utils/convert.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/provider/utils/convert.ts
index cd8139d0a9..d63e09c796 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/editor/provider/utils/convert.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/provider/utils/convert.ts
@@ -20,7 +20,6 @@ export function transformToInlineElement(op: Op): Element | null {
children: [
{
text: op.insert as string,
- ...attributes,
},
],
};
@@ -54,6 +53,20 @@ export function getInlinesWithDelta(delta?: Op[]): (Text | Element)[] {
return matchInline;
}
+ if (op.attributes) {
+ if ('font_color' in op.attributes && op.attributes['font_color'] === '') {
+ delete op.attributes['font_color'];
+ }
+
+ if ('bg_color' in op.attributes && op.attributes['bg_color'] === '') {
+ delete op.attributes['bg_color'];
+ }
+
+ if ('code' in op.attributes && !op.attributes['code']) {
+ delete op.attributes['code'];
+ }
+ }
+
return {
text: op.insert as string,
...op.attributes,
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/stores/decorate.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/stores/decorate.ts
new file mode 100644
index 0000000000..078296aade
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/stores/decorate.ts
@@ -0,0 +1,89 @@
+import { createContext, useCallback, useContext, useMemo } from 'react';
+import { BaseRange, Editor, NodeEntry, Range } from 'slate';
+import { proxySet } from 'valtio/utils';
+import { useSnapshot } from 'valtio';
+import { ReactEditor } from 'slate-react';
+
+export const DecorateStateContext = createContext<
+ Set<{
+ range: BaseRange;
+ class_name: string;
+ type?: 'link';
+ }>
+>(new Set());
+export const DecorateStateProvider = DecorateStateContext.Provider;
+
+export function useInitialDecorateState(editor: ReactEditor) {
+ const decorateState = useMemo(
+ () =>
+ proxySet<{
+ range: BaseRange;
+ class_name: string;
+ }>([]),
+ []
+ );
+
+ const ranges = useSnapshot(decorateState);
+
+ const decorate = useCallback(
+ ([, path]: NodeEntry): BaseRange[] => {
+ const highlightRanges: (Range & {
+ class_name: string;
+ })[] = [];
+
+ ranges.forEach((state) => {
+ const intersection = Range.intersection(state.range, Editor.range(editor, path));
+
+ if (intersection) {
+ highlightRanges.push({
+ ...intersection,
+ class_name: state.class_name,
+ });
+ }
+ });
+
+ return highlightRanges;
+ },
+ [editor, ranges]
+ );
+
+ return {
+ decorate,
+ decorateState,
+ };
+}
+
+export function useDecorateState(type?: 'link') {
+ const context = useContext(DecorateStateContext);
+
+ const state = useSnapshot(context);
+
+ return useMemo(() => {
+ return Array.from(state).find((s) => !type || s.type === type);
+ }, [state, type]);
+}
+
+export function useDecorateDispatch() {
+ const context = useContext(DecorateStateContext);
+
+ const getStaticState = useCallback(() => {
+ return Array.from(context)[0];
+ }, [context]);
+
+ const add = useCallback(
+ (state: { range: BaseRange; class_name: string; type?: 'link' }) => {
+ context.add(state);
+ },
+ [context]
+ );
+
+ const clear = useCallback(() => {
+ context.clear();
+ }, [context]);
+
+ return {
+ add,
+ clear,
+ getStaticState,
+ };
+}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/stores/index.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/stores/index.ts
new file mode 100644
index 0000000000..e93794da88
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/stores/index.ts
@@ -0,0 +1,25 @@
+import { ReactEditor } from 'slate-react';
+import { useInitialDecorateState } from '$app/components/editor/stores/decorate';
+import { useInitialSelectedBlocks } from '$app/components/editor/stores/selected';
+import { useInitialSlashState } from '$app/components/editor/stores/slash';
+import { useInitialEditorInlineBlockState } from '$app/components/editor/stores/inline_node';
+
+export * from './decorate';
+export * from './selected';
+export * from './slash';
+export * from './inline_node';
+
+export function useInitialEditorState(editor: ReactEditor) {
+ const { decorate, decorateState } = useInitialDecorateState(editor);
+ const selectedBlocks = useInitialSelectedBlocks(editor);
+ const slashState = useInitialSlashState();
+ const inlineBlockState = useInitialEditorInlineBlockState();
+
+ return {
+ selectedBlocks,
+ decorate,
+ decorateState,
+ slashState,
+ inlineBlockState,
+ };
+}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/stores/inline_node.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/stores/inline_node.ts
new file mode 100644
index 0000000000..a6d536522e
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/stores/inline_node.ts
@@ -0,0 +1,58 @@
+import { createContext, useCallback, useContext, useMemo } from 'react';
+import { BaseRange } from 'slate';
+import { proxy, useSnapshot } from 'valtio';
+
+export interface EditorInlineBlockState {
+ formula: {
+ popoverOpen: boolean;
+ range?: BaseRange;
+ };
+}
+const initialState = {
+ formula: {
+ popoverOpen: false,
+ range: undefined,
+ },
+};
+
+export const EditorInlineBlockStateContext = createContext<EditorInlineBlockState>(initialState);
+
+export const EditorInlineBlockStateProvider = EditorInlineBlockStateContext.Provider;
+
+export function useInitialEditorInlineBlockState() {
+ const state = useMemo(() => {
+ return proxy({
+ ...initialState,
+ });
+ }, []);
+
+ return state;
+}
+
+export function useEditorInlineBlockState(key: 'formula') {
+ const context = useContext(EditorInlineBlockStateContext);
+
+ const state = useSnapshot(context[key]);
+
+ const openPopover = useCallback(() => {
+ context[key].popoverOpen = true;
+ }, [context, key]);
+
+ const closePopover = useCallback(() => {
+ context[key].popoverOpen = false;
+ }, [context, key]);
+
+ const setRange = useCallback(
+ (range: BaseRange) => {
+ context[key].range = range;
+ },
+ [context, key]
+ );
+
+ return {
+ ...state,
+ openPopover,
+ closePopover,
+ setRange,
+ };
+}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/stores/selected.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/stores/selected.ts
new file mode 100644
index 0000000000..e3a28ff5fd
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/stores/selected.ts
@@ -0,0 +1,70 @@
+import { createContext, useContext, useEffect, useMemo, useState } from 'react';
+import { proxySet, subscribeKey } from 'valtio/utils';
+import { ReactEditor } from 'slate-react';
+import { Element } from 'slate';
+
+export function useSelectedBlocksSize() {
+ const selectedBlocks = useContext(EditorSelectedBlockContext);
+
+ const [selectedLength, setSelectedLength] = useState(0);
+
+ useEffect(() => {
+ subscribeKey(selectedBlocks, 'size', (v) => setSelectedLength(v));
+ }, [selectedBlocks]);
+
+ return selectedLength;
+}
+
+export function useInitialSelectedBlocks(editor: ReactEditor) {
+ const selectedBlocks = useMemo(() => proxySet([]), []);
+ const [selectedLength, setSelectedLength] = useState(0);
+
+ subscribeKey(selectedBlocks, 'size', (v) => setSelectedLength(v));
+
+ useEffect(() => {
+ const { onChange } = editor;
+
+ const onKeydown = (e: KeyboardEvent) => {
+ if (!ReactEditor.isFocused(editor) && selectedLength > 0) {
+ e.preventDefault();
+ e.stopPropagation();
+ const selectedBlockId = selectedBlocks.values().next().value;
+ const [selectedBlock] = editor.nodes({
+ at: [],
+ match: (n) => Element.isElement(n) && n.blockId === selectedBlockId,
+ });
+ const [, path] = selectedBlock;
+
+ editor.select(path);
+ ReactEditor.focus(editor);
+ }
+ };
+
+ if (selectedLength > 0) {
+ editor.onChange = (...args) => {
+ const isSelectionChange = editor.operations.every((arg) => arg.type === 'set_selection');
+
+ if (isSelectionChange) {
+ selectedBlocks.clear();
+ }
+
+ onChange(...args);
+ };
+
+ document.addEventListener('keydown', onKeydown);
+ } else {
+ editor.onChange = onChange;
+ document.removeEventListener('keydown', onKeydown);
+ }
+
+ return () => {
+ editor.onChange = onChange;
+ document.removeEventListener('keydown', onKeydown);
+ };
+ }, [editor, selectedBlocks, selectedLength]);
+
+ return selectedBlocks;
+}
+
+export const EditorSelectedBlockContext = createContext<Set<string>>(new Set());
+export const EditorSelectedBlockProvider = EditorSelectedBlockContext.Provider;
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/editor/stores/slash.ts b/frontend/appflowy_tauri/src/appflowy_app/components/editor/stores/slash.ts
new file mode 100644
index 0000000000..13e9447d0c
--- /dev/null
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/editor/stores/slash.ts
@@ -0,0 +1,30 @@
+import { createContext, useCallback, useContext, useMemo } from 'react';
+import { proxyMap } from 'valtio/utils';
+import { useSnapshot } from 'valtio';
+
+export const SlashStateContext = createContext<Map<string, boolean>>(new Map());
+export const SlashStateProvider = SlashStateContext.Provider;
+
+export function useInitialSlashState() {
+ const state = useMemo(() => proxyMap([['open', false]]), []);
+
+ return state;
+}
+
+export function useSlashState() {
+ const context = useContext(SlashStateContext);
+ const state = useSnapshot(context);
+ const open = state.get('open');
+
+ const setOpen = useCallback(
+ (open: boolean) => {
+ context.set('open', open);
+ },
+ [context]
+ );
+
+ return {
+ open,
+ setOpen,
+ };
+}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/layout/Layout.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/layout/Layout.tsx
index d912c4bf6b..a0a508a975 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/layout/Layout.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/layout/Layout.tsx
@@ -34,7 +34,7 @@ function Layout({ children }: { children: ReactNode }) {
style={{
height: 'calc(100vh - 64px - 48px)',
}}
- className={'overflow-y-auto overflow-x-hidden'}
+ className={'appflowy-layout overflow-y-auto overflow-x-hidden'}
>
{children}
</div>
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/layout/bread_crumb/Breadcrumb.hooks.ts b/frontend/appflowy_tauri/src/appflowy_app/components/layout/bread_crumb/Breadcrumb.hooks.ts
index ed10c5b7b9..807a8f2f65 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/layout/bread_crumb/Breadcrumb.hooks.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/layout/bread_crumb/Breadcrumb.hooks.ts
@@ -42,7 +42,10 @@ export function useLoadExpandedPages() {
setPagePath((prev) => {
return [page, ...prev];
});
- await loadPagePath(page.parentId);
+
+ if (page.parentId) {
+ await loadPagePath(page.parentId);
+ }
},
[pageMap]
);
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/layout/nested_page/NestedPage.hooks.ts b/frontend/appflowy_tauri/src/appflowy_app/components/layout/nested_page/NestedPage.hooks.ts
index 1081fc2cd1..8b94bd81f5 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/layout/nested_page/NestedPage.hooks.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/layout/nested_page/NestedPage.hooks.ts
@@ -1,11 +1,12 @@
-import { useCallback, useEffect } from 'react';
-import { Page, pagesActions, pageTypeMap, parserViewPBToPage } from '$app_reducers/pages/slice';
+import { useCallback, useEffect, useMemo } from 'react';
+import { Page, pagesActions, pageTypeMap } from '$app_reducers/pages/slice';
import { useAppDispatch, useAppSelector } from '$app/stores/store';
import { FolderNotification, ViewLayoutPB } from '@/services/backend';
import { useNavigate, useParams } from 'react-router-dom';
import { updatePageName } from '$app_reducers/pages/async_actions';
import { createPage, deletePage, duplicatePage, getChildPages } from '$app/application/folder/page.service';
import { subscribeNotifications } from '$app/application/notification';
+import debounce from 'lodash-es/debounce';
export function useLoadChildPages(pageId: string) {
const dispatch = useAppDispatch();
@@ -19,12 +20,13 @@ export function useLoadChildPages(pageId: string) {
}
}, [dispatch, pageId, collapsed]);
- const onPageChanged = useCallback(
- (page: Page) => {
+ const onPageChanged = useMemo(() => {
+ return debounce((page: Page) => {
+ console.log('DidUpdateView');
+
dispatch(pagesActions.onPageChanged(page));
- },
- [dispatch]
- );
+ }, 200);
+ }, [dispatch]);
const loadPageChildren = useCallback(
async (pageId: string) => {
@@ -47,12 +49,15 @@ export function useLoadChildPages(pageId: string) {
useEffect(() => {
const unsubscribePromise = subscribeNotifications(
{
- [FolderNotification.DidUpdateView]: async (payload) => {
- const page = parserViewPBToPage(payload);
-
- onPageChanged(page);
+ [FolderNotification.DidUpdateView]: (_payload) => {
+ // const page = parserViewPBToPage(payload);
+ // onPageChanged(page);
},
- [FolderNotification.DidUpdateChildViews]: async () => {
+ [FolderNotification.DidUpdateChildViews]: async (payload) => {
+ if (payload.delete_child_views.length === 0 && payload.create_child_views.length === 0) {
+ return;
+ }
+
void loadPageChildren(pageId);
},
},
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/layout/nested_page/NestedPageTitle.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/layout/nested_page/NestedPageTitle.tsx
index 5b33d8a157..41058ee327 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/layout/nested_page/NestedPageTitle.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/layout/nested_page/NestedPageTitle.tsx
@@ -35,8 +35,8 @@ function NestedPageTitle({
return (
<div
- className={`m-1 cursor-pointer rounded-lg px-2 py-1 ${isHovering ? 'bg-fill-list-hover' : ''} ${
- isSelected ? 'bg-fill-list-active' : ''
+ className={`m-1 cursor-pointer rounded-lg bg-opacity-40 px-2 py-1 ${isHovering ? 'bg-fill-list-hover' : ''} ${
+ isSelected ? 'bg-fill-list-hover' : ''
}`}
onClick={onClick}
onMouseEnter={() => setIsHovering(true)}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/components/layout/nested_page/RenameDialog.tsx b/frontend/appflowy_tauri/src/appflowy_app/components/layout/nested_page/RenameDialog.tsx
index bb3e4e798f..71c00878f8 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/components/layout/nested_page/RenameDialog.tsx
+++ b/frontend/appflowy_tauri/src/appflowy_app/components/layout/nested_page/RenameDialog.tsx
@@ -32,7 +32,20 @@ function RenameDialog({
<TextField
error={error}
autoFocus
+ spellCheck={false}
value={value}
+ onKeyDown={(e) => {
+ e.stopPropagation();
+ if (e.key === 'Enter') {
+ e.preventDefault();
+ void onOk(value);
+ }
+
+ if (e.key === 'Escape') {
+ e.preventDefault();
+ onClose();
+ }
+ }}
onChange={(e) => {
setValue(e.target.value);
}}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/slate-editor.d.ts b/frontend/appflowy_tauri/src/appflowy_app/slate-editor.d.ts
index 77b41bb4cb..26b713e333 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/slate-editor.d.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/slate-editor.d.ts
@@ -1,4 +1,3 @@
-import { BaseEditor } from 'slate';
import { ReactEditor } from 'slate-react';
interface EditorInlineAttributes {
@@ -38,4 +37,8 @@ declare module 'slate' {
Element: CustomElement;
Text: CustomText;
}
+
+ interface BaseEditor {
+ isEmbed: (element: CustomElement) => boolean;
+ }
}
diff --git a/frontend/appflowy_tauri/src/appflowy_app/stores/reducers/pages/async_actions.ts b/frontend/appflowy_tauri/src/appflowy_app/stores/reducers/pages/async_actions.ts
index 0e8c9cc9ea..23f4d85cbc 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/stores/reducers/pages/async_actions.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/stores/reducers/pages/async_actions.ts
@@ -75,6 +75,7 @@ export const updatePageName = createAsyncThunk(
name,
})
);
+
await updatePage({
id,
name,
diff --git a/frontend/appflowy_tauri/src/appflowy_app/utils/mui.ts b/frontend/appflowy_tauri/src/appflowy_app/utils/mui.ts
index 78a0308a1b..2bdf102a6e 100644
--- a/frontend/appflowy_tauri/src/appflowy_app/utils/mui.ts
+++ b/frontend/appflowy_tauri/src/appflowy_app/utils/mui.ts
@@ -15,6 +15,18 @@ export const getDesignTokens = (mode: ThemeMode): ThemeOptions => {
},
},
components: {
+ MuiMenuItem: {
+ defaultProps: {
+ sx: {
+ '&.Mui-selected.Mui-focusVisible': {
+ backgroundColor: 'var(--fill-list-hover)',
+ },
+ '&.Mui-focusVisible': {
+ backgroundColor: 'unset',
+ },
+ },
+ },
+ },
MuiIconButton: {
styleOverrides: {
root: {
diff --git a/frontend/appflowy_tauri/src/styles/template.css b/frontend/appflowy_tauri/src/styles/template.css
index 1dd567990e..e8fe35e440 100644
--- a/frontend/appflowy_tauri/src/styles/template.css
+++ b/frontend/appflowy_tauri/src/styles/template.css
@@ -17,16 +17,16 @@ textarea {
}
body {
- font-family: Poppins;
+ font-family: Poppins, serif;
}
-::selection {
- @apply bg-content-blue-100;
+::-webkit-scrollbar {
+ width: 8px;
}
-::-webkit-scrollbar {
- width: 8px;
- height: 8px;
+.MuiPopover-root::-webkit-scrollbar {
+ width: 0;
+ height: 0;
}
@@ -39,43 +39,6 @@ body {
scrollbar-shadow-color: var(--bg-body);
}
-.block-element {
- @apply my-[1px];
-}
-
-.block-element .block-element {
- @apply mb-0;
-}
-
-.block-element[data-block-type="page"] .text-element {
- min-height: 52px !important;
- line-height: 52px !important;
-}
-
-.block-element[data-block-type="paragraph"] .block-element {
- margin-left: 24px;
-}
-
-.block-element.block-align-left .text-element {
- justify-content: flex-start;
-}
-
-.block-element.block-align-right .text-element {
- justify-content: flex-end;
-}
-
-.block-element.block-align-center .text-element {
- justify-content: center;
-}
-
-.block-element[data-block-type="todo_list"] .checked > .text-element {
- text-decoration: line-through;
- color: var(--text-caption);
-}
-
-.block-element .collapsed .block-element {
- display: none;
-}
.btn {
@apply rounded-xl border border-line-divider px-4 py-3;
@@ -94,10 +57,6 @@ th {
@apply text-left font-normal;
}
-span[data-slate-placeholder="true"]:not(.inline-block-content) {
- @apply text-text-placeholder;
- opacity: 1 !important;
-}
.sketch-picker {
background-color: var(--bg-body) !important;
diff --git a/frontend/appflowy_tauri/src/styles/variables/dark.variables.css b/frontend/appflowy_tauri/src/styles/variables/dark.variables.css
index 11b434ce76..9d8bb9decd 100644
--- a/frontend/appflowy_tauri/src/styles/variables/dark.variables.css
+++ b/frontend/appflowy_tauri/src/styles/variables/dark.variables.css
@@ -1,6 +1,6 @@
/**
* Do not edit directly
-* Generated on Wed, 12 Jul 2023 07:09:42 GMT
+* Generated on Mon, 29 Jan 2024 03:52:24 GMT
* Generated from $pnpm css:variables
*/
@@ -68,6 +68,7 @@
--base-black-color-light-red: #56363F;
--base-black-color-light-aqua: #1B3849;
--base-black-color-light-lime: #394027;
+ --base-black-color-light-orange: #5E3C3C;
--base-else-brand: #2c144b;
--text-title: #e2e9f2;
--text-caption: #87A0BF;
@@ -79,13 +80,13 @@
--icon-primary: #e2e9f2;
--icon-secondary: #59647a;
--icon-disabled: #525A69;
- --icon-on-toolbar: #1a202c;
+ --icon-on-toolbar: white;
--line-border: #59647a;
--line-divider: #252F41;
--line-on-toolbar: #99a6b8;
--fill-default: #00bcf0;
--fill-hover: #005174;
- --fill-toolbar: #e2e9f2;
+ --fill-toolbar: #0F111C;
--fill-selector: #232b38;
--fill-list-active: #252F41;
--fill-list-hover: #005174;
@@ -113,5 +114,6 @@
--tint-pink: #5E3C5E;
--tint-lime: #394027;
--tint-aqua: #1B3849;
+ --tint-orange: #5E3C3C;
--shadow: 0px 0px 25px 0px rgba(0,0,0,0.3);
}
\ No newline at end of file
diff --git a/frontend/appflowy_tauri/src/styles/variables/light.variables.css b/frontend/appflowy_tauri/src/styles/variables/light.variables.css
index d1b6c9a725..b0ce72e1a5 100644
--- a/frontend/appflowy_tauri/src/styles/variables/light.variables.css
+++ b/frontend/appflowy_tauri/src/styles/variables/light.variables.css
@@ -1,6 +1,6 @@
/**
* Do not edit directly
-* Generated on Wed, 12 Jul 2023 07:09:42 GMT
+* Generated on Mon, 29 Jan 2024 03:52:24 GMT
* Generated from $pnpm css:variables
*/
@@ -68,6 +68,7 @@
--base-black-color-light-red: #56363F;
--base-black-color-light-aqua: #1B3849;
--base-black-color-light-lime: #394027;
+ --base-black-color-light-orange: #5E3C3C;
--base-else-brand: #2c144b;
--text-title: #333333;
--text-caption: #828282;
diff --git a/frontend/appflowy_tauri/style-dictionary/tailwind/box-shadow.cjs b/frontend/appflowy_tauri/style-dictionary/tailwind/box-shadow.cjs
index f9daf6e9f4..a3b36ef1c0 100644
--- a/frontend/appflowy_tauri/style-dictionary/tailwind/box-shadow.cjs
+++ b/frontend/appflowy_tauri/style-dictionary/tailwind/box-shadow.cjs
@@ -1,6 +1,6 @@
/**
* Do not edit directly
-* Generated on Wed, 12 Jul 2023 07:09:42 GMT
+* Generated on Mon, 29 Jan 2024 03:52:24 GMT
* Generated from $pnpm css:variables
*/
diff --git a/frontend/appflowy_tauri/style-dictionary/tailwind/colors.cjs b/frontend/appflowy_tauri/style-dictionary/tailwind/colors.cjs
index 54c975af66..1cd0a67ada 100644
--- a/frontend/appflowy_tauri/style-dictionary/tailwind/colors.cjs
+++ b/frontend/appflowy_tauri/style-dictionary/tailwind/colors.cjs
@@ -1,6 +1,6 @@
/**
* Do not edit directly
-* Generated on Wed, 12 Jul 2023 07:09:42 GMT
+* Generated on Mon, 29 Jan 2024 03:52:24 GMT
* Generated from $pnpm css:variables
*/
@@ -66,6 +66,6 @@ module.exports = {
"pink": "var(--tint-pink)",
"lime": "var(--tint-lime)",
"aqua": "var(--tint-aqua)",
- "orange": "var(--tint-orange)",
+ "orange": "var(--tint-orange)"
}
}
\ No newline at end of file
diff --git a/frontend/appflowy_tauri/style-dictionary/tokens/base.json b/frontend/appflowy_tauri/style-dictionary/tokens/base.json
index dfa4922315..48ff92e680 100644
--- a/frontend/appflowy_tauri/style-dictionary/tokens/base.json
+++ b/frontend/appflowy_tauri/style-dictionary/tokens/base.json
@@ -272,6 +272,10 @@
"lime": {
"value": "#394027",
"type": "color"
+ },
+ "orange": {
+ "value": "#5E3C3C",
+ "type": "color"
}
}
}
diff --git a/frontend/appflowy_tauri/style-dictionary/tokens/dark.json b/frontend/appflowy_tauri/style-dictionary/tokens/dark.json
index 196c24087a..ea97844f34 100644
--- a/frontend/appflowy_tauri/style-dictionary/tokens/dark.json
+++ b/frontend/appflowy_tauri/style-dictionary/tokens/dark.json
@@ -43,7 +43,7 @@
"type": "color"
},
"on-toolbar": {
- "value": "{Base.black.neutral.N00}",
+ "value": "white",
"type": "color"
}
},
@@ -71,7 +71,7 @@
"type": "color"
},
"toolbar": {
- "value": "{Base.black.neutral.800}",
+ "value": "#0F111C",
"type": "color"
},
"selector": {
@@ -191,6 +191,10 @@
"aqua": {
"value": "{Base.black.color.light.aqua}",
"type": "color"
+ },
+ "orange": {
+ "value": "{Base.black.color.light.orange}",
+ "type": "color"
}
},
"shadow": {