Skip to content

Commit 456a0ab

Browse files
authored
Merge pull request #875 from streamich/peritext-block-menu-2
Peritext block menu clipboard actions
2 parents c202bdb + 385ce80 commit 456a0ab

File tree

13 files changed

+375
-324
lines changed

13 files changed

+375
-324
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
"json-crdt-traces": "https://github.com/streamich/json-crdt-traces#ec825401dc05cbb74b9e0b3c4d6527399f54d54d",
115115
"json-logic-js": "^2.0.2",
116116
"nano-theme": "^1.4.3",
117-
"nice-ui": "^1.29.1",
117+
"nice-ui": "^1.30.0",
118118
"quill-delta": "^5.1.0",
119119
"react": "^18.3.1",
120120
"react-dom": "^18.3.1",

src/json-crdt-peritext-ui/events/defaults/PeritextEventDefaults.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ export class PeritextEventDefaults implements PeritextEventHandlerMap {
272272
case 'text': {
273273
const text = range.text();
274274
clipboard.writeText(text)?.catch((err) => console.error(err));
275-
if (action === 'cut') editor.collapseCursors();
275+
if (action === 'cut') editor.collapseCursor(range);
276276
break;
277277
}
278278
case 'style': {
@@ -313,7 +313,7 @@ export class PeritextEventDefaults implements PeritextEventHandlerMap {
313313
break;
314314
}
315315
clipboard.writeText(text)?.catch((err) => console.error(err));
316-
if (action === 'cut') editor.collapseCursors();
316+
if (action === 'cut') editor.collapseCursor(range);
317317
break;
318318
}
319319
default: {
@@ -325,7 +325,7 @@ export class PeritextEventDefaults implements PeritextEventHandlerMap {
325325
} else {
326326
const data = transfer.toClipboard(range);
327327
clipboard.write(data as unknown as PeritextClipboardData<string>)?.catch((err) => console.error(err));
328-
if (action === 'cut') editor.collapseCursors();
328+
if (action === 'cut') editor.collapseCursor(range);
329329
}
330330
}
331331
}

src/json-crdt-peritext-ui/plugins/blocks/RenderBlock/index.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,30 @@ export const RenderBlock: React.FC<RenderBlockProps> = (props) => {
2121
case CommonSliceType.blockquote: {
2222
return <Blockquote {...props} />;
2323
}
24+
case CommonSliceType.h1: {
25+
return <h1>{children}</h1>;
26+
}
27+
case CommonSliceType.h2: {
28+
return <h2>{children}</h2>;
29+
}
30+
case CommonSliceType.h3: {
31+
return <h3>{children}</h3>;
32+
}
33+
case CommonSliceType.h4: {
34+
return <h4>{children}</h4>;
35+
}
36+
case CommonSliceType.h5: {
37+
return <h5>{children}</h5>;
38+
}
39+
case CommonSliceType.h6: {
40+
return <h6>{children}</h6>;
41+
}
42+
case CommonSliceType.title: {
43+
return <h1>{children}</h1>;
44+
}
45+
case CommonSliceType.subtitle: {
46+
return <h2>{children}</h2>;
47+
}
2448
default: {
2549
return <p style={{padding: '16px 0'}}>{children}</p>;
2650
}

src/json-crdt-peritext-ui/plugins/toolbar/RenderFocus.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22
import {rule} from 'nano-theme';
33
import {CaretToolbar} from 'nice-ui/lib/4-card/Toolbar/ToolbarMenu/CaretToolbar';
4+
import {MoveToViewport} from 'nice-ui/lib/utils/popup/MoveToViewport';
45
import {useToolbarPlugin} from './context';
56
import {useSyncStore, useSyncStoreOpt, useTimeout} from '../../web/react/hooks';
67
import type {CaretViewProps} from '../../web/react/cursor/CaretView';
@@ -27,11 +28,11 @@ export interface RenderFocusProps extends CaretViewProps {
2728
children: React.ReactNode;
2829
}
2930

30-
export const RenderFocus: React.FC<RenderFocusProps> = ({children}) => {
31+
export const RenderFocus: React.FC<RenderFocusProps> = ({children, cursor}) => {
3132
const {toolbar} = useToolbarPlugin()!;
3233
const showInlineToolbar = toolbar.showInlineToolbar;
3334
const [showInlineToolbarValue, toolbarVisibilityChangeTime] = useSyncStore(showInlineToolbar);
34-
const enableAfterCoolDown = useTimeout(500, [toolbarVisibilityChangeTime]);
35+
const enableAfterCoolDown = useTimeout(300, [toolbarVisibilityChangeTime]);
3536
const isScrubbing = useSyncStoreOpt(toolbar.surface.dom?.cursor.mouseDown) || false;
3637
// const focus = useSyncStoreOpt(toolbar.surface.dom?.cursor.focus) || false;
3738
// const blurTimeout = useTimeout(300, [focus]);
@@ -43,13 +44,15 @@ export const RenderFocus: React.FC<RenderFocusProps> = ({children}) => {
4344

4445
let toolbarElement: React.ReactNode = null;
4546

46-
if (showInlineToolbarValue && !isScrubbing)
47+
if (showInlineToolbarValue && !isScrubbing && toolbar.txt.editor.mainCursor() === cursor)
4748
toolbarElement = (
48-
<CaretToolbar
49-
disabled={!enableAfterCoolDown /* || (!focus && blurTimeout) */}
50-
menu={toolbar.getSelectionMenu()}
51-
onPopupClose={handleClose}
52-
/>
49+
<MoveToViewport>
50+
<CaretToolbar
51+
disabled={!enableAfterCoolDown /* || (!focus && blurTimeout) */}
52+
menu={toolbar.getSelectionMenu()}
53+
onPopupClose={handleClose}
54+
/>
55+
</MoveToViewport>
5356
);
5457

5558
return (

src/json-crdt-peritext-ui/plugins/toolbar/block/LeafBlockFrame/AutoExpandableToolbar.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from 'react';
2+
// import {ExpandableToolbar, type ExpandableToolbarProps} from 'nice-ui/lib/4-card/Toolbar/ToolbarMenu/ExpandableToolbar';
23
import {ExpandableToolbar, type ExpandableToolbarProps} from './ExpandableToolbar';
34
import type {MenuItem} from 'nice-ui/lib/4-card/StructuralMenu/types';
45
import type {AnchorPoint} from 'nice-ui/lib/utils/popup';

src/json-crdt-peritext-ui/plugins/toolbar/block/LeafBlockFrame/ExpandableToolbar.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import * as React from 'react';
22
import {ToolbarMenu} from 'nice-ui/lib/4-card/Toolbar/ToolbarMenu';
3-
import {ContextMenu} from 'nice-ui/lib/4-card/ContextMenu';
3+
import {ContextMenu, type ContextMenuProps} from 'nice-ui/lib/4-card/ContextMenu';
44
import {PositionAtPoint} from 'nice-ui/lib/utils/popup/PositionAtPoint';
55
import {context as popupContext} from 'nice-ui/lib/4-card/Popup/context';
66
import {ClickAway} from 'nice-ui/lib/utils/ClickAway';
77
import {ToolbarMenuProvider} from 'nice-ui/lib/4-card/Toolbar/ToolbarMenu/ToolbarMenuProvider';
8+
import {MoveToViewport} from 'nice-ui/lib/utils/popup/MoveToViewport';
89
import type {ToolbarMenuProps} from 'nice-ui/lib/4-card/Toolbar/ToolbarMenu/types';
910
import type {AnchorPoint} from 'nice-ui/lib/utils/popup';
1011

@@ -14,10 +15,11 @@ export interface ExpandableToolbarProps extends ToolbarMenuProps {
1415
expandPoint?: AnchorPoint | (() => AnchorPoint);
1516
disabled?: boolean;
1617
more?: Omit<ToolbarMenuProps['more'], 'onClick'>;
18+
context?: ContextMenuProps;
1719
}
1820

1921
export const ExpandableToolbar: React.FC<ExpandableToolbarProps> = (props) => {
20-
const {expandPoint, more, ...rest} = props;
22+
const {expandPoint, more, context, ...rest} = props;
2123
const [view, setView] = React.useState<InlineMenuView>('toolbar');
2224
const popupContextValue = React.useMemo(
2325
() => ({
@@ -38,7 +40,9 @@ export const ExpandableToolbar: React.FC<ExpandableToolbarProps> = (props) => {
3840
<ClickAway onClickAway={handleContextMenuClickAway}>
3941
<popupContext.Provider value={popupContextValue}>
4042
<ToolbarMenuProvider {...rest}>
41-
<ContextMenu {...rest} inset showSearch onEsc={() => setView('toolbar')} />
43+
<MoveToViewport>
44+
<ContextMenu inset showSearch {...context} menu={rest.menu} onEsc={() => setView('toolbar')} />
45+
</MoveToViewport>
4246
</ToolbarMenuProvider>
4347
</popupContext.Provider>
4448
</ClickAway>
@@ -52,7 +56,7 @@ export const ExpandableToolbar: React.FC<ExpandableToolbarProps> = (props) => {
5256
more={{
5357
...more,
5458
onClick: expandPoint
55-
? (e) => {
59+
? () => {
5660
setView('context');
5761
}
5862
: undefined,

src/json-crdt-peritext-ui/plugins/toolbar/block/LeafBlockFrame/index.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ const topLeftOverlay = rule({
1818
l: '-72px',
1919
});
2020

21-
const topRightOverlay = rule({
22-
d: 'block',
23-
pos: 'absolute',
24-
t: '-36px',
25-
r: '-16px',
26-
});
21+
// const topRightOverlay = rule({
22+
// d: 'block',
23+
// pos: 'absolute',
24+
// t: '-36px',
25+
// r: '-16px',
26+
// });
2727

2828
export interface LeafBlockFrameProps extends RenderBlockProps {}
2929

0 commit comments

Comments
 (0)