-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ImgSize): added ResizableImage (#338)
- Loading branch information
1 parent
20b6e31
commit 66ca04d
Showing
7 changed files
with
373 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
body :has(.g-md-resizable_resizing) { | ||
cursor: col-resize; | ||
} | ||
|
||
.g-md-resizable { | ||
position: relative; | ||
|
||
&_resizing &__resizer-wrapper, | ||
&_hover &__resizer-wrapper { | ||
position: absolute; | ||
z-index: 1; | ||
top: 0; | ||
|
||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
|
||
width: 20px; | ||
height: 100%; | ||
|
||
cursor: col-resize; | ||
pointer-events: auto; | ||
|
||
&_left { | ||
left: 0; | ||
} | ||
|
||
&_right { | ||
right: 0; | ||
} | ||
} | ||
|
||
&__resizer { | ||
opacity: 0; | ||
} | ||
|
||
&_resizing &__resizer, | ||
&_hover &__resizer { | ||
box-sizing: content-box; | ||
width: 4px; | ||
height: 50px; | ||
max-height: 50%; | ||
|
||
opacity: 1; | ||
border-radius: 6px; | ||
background: rgba(127, 127, 127, 0.8); | ||
|
||
transition: opacity 300ms ease-in 0s; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from 'react'; | ||
|
||
import {cn} from '../../../classname'; | ||
|
||
import './Resizable.scss'; | ||
|
||
const b = cn('resizable'); | ||
|
||
interface ResizerProps { | ||
onMouseDown: (event: React.MouseEvent<HTMLElement>) => void; | ||
direction: 'left' | 'right'; | ||
} | ||
const Resizer: React.FC<ResizerProps> = ({onMouseDown, direction}) => ( | ||
<div | ||
className={b('resizer-wrapper', {[direction]: true})} | ||
role="button" | ||
tabIndex={0} | ||
onMouseDown={onMouseDown} | ||
> | ||
<div className={b('resizer')} /> | ||
</div> | ||
); | ||
|
||
export interface ResizableProps { | ||
children: React.ReactNode; | ||
onResizeLeft: (event: React.MouseEvent<HTMLElement>) => void; | ||
onResizeRight: (event: React.MouseEvent<HTMLElement>) => void; | ||
hover?: boolean; | ||
resizing?: boolean; | ||
} | ||
|
||
export const Resizable: React.FC<ResizableProps> = ({ | ||
hover, | ||
resizing, | ||
children, | ||
onResizeLeft, | ||
onResizeRight, | ||
}) => ( | ||
<div className={b({hover, resizing})}> | ||
{children} | ||
<Resizer onMouseDown={onResizeLeft} direction="left" /> | ||
<Resizer onMouseDown={onResizeRight} direction="right" /> | ||
</div> | ||
); |
6 changes: 6 additions & 0 deletions
6
src/extensions/yfm/ImgSize/plugins/ImgSizeNodeView/ImgSettingsButton.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.g-md-img-settings-button { | ||
position: absolute; | ||
z-index: 2; | ||
top: 3px; | ||
right: 3px; | ||
} |
123 changes: 61 additions & 62 deletions
123
src/extensions/yfm/ImgSize/plugins/ImgSizeNodeView/ImgSettingsButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,95 @@ | ||
import React, {RefObject, useEffect, useRef} from 'react'; | ||
import React, {RefObject, useRef} from 'react'; | ||
|
||
import {Ellipsis} from '@gravity-ui/icons'; | ||
import {Button, Icon, Menu, Popup, PopupPlacement} from '@gravity-ui/uikit'; | ||
import {Node} from 'prosemirror-model'; | ||
import {EditorView} from 'prosemirror-view'; | ||
|
||
import {cn} from '../../../../../classname'; | ||
import {i18n as i18nCommon} from '../../../../../i18n/common'; | ||
import {useBooleanState} from '../../../../../react-utils/hooks'; | ||
import {useNodeEditing} from '../../../../../react-utils/useNodeEditing'; | ||
import {useNodeHovered} from '../../../../../react-utils/useNodeHovered'; | ||
import {removeNode} from '../../../../../utils/remove-node'; | ||
import {imageRendererKey} from '../../const'; | ||
|
||
import {ImageForm} from './ImageForm'; | ||
|
||
import './ImgSettingsButton.scss'; | ||
|
||
const b = cn('img-settings-button'); | ||
|
||
export const ImgSettingsButton: React.FC<{ | ||
node: Node; | ||
view: EditorView; | ||
getPos: () => number | undefined; | ||
nodeRef: RefObject<HTMLElement>; | ||
updateAttributes: (o: object) => void; | ||
}> = function ({node, view, getPos, nodeRef, updateAttributes}) { | ||
nodeRef: RefObject<HTMLDivElement>; | ||
visible: boolean; | ||
toggleEdit: () => void; | ||
edit: boolean; | ||
unsetEdit: () => void; | ||
onDelete: () => void; | ||
}> = function ({ | ||
node, | ||
view, | ||
updateAttributes, | ||
visible, | ||
edit, | ||
toggleEdit, | ||
nodeRef, | ||
unsetEdit, | ||
onDelete, | ||
}) { | ||
const [popupOpen, setPopupOpen, unsetPopupOpen] = useBooleanState(false); | ||
const placement: PopupPlacement = ['bottom-end', 'bottom-start']; | ||
const buttonRef = useRef<HTMLDivElement>(null); | ||
|
||
const isNodeHovered = useNodeHovered(nodeRef); | ||
const isButtonHovered = useNodeHovered(buttonRef); | ||
|
||
const [edit, setEditing, unsetEdit, toggleEdit] = useNodeEditing({nodeRef, view}); | ||
const visible = (isNodeHovered || isButtonHovered || popupOpen) && !edit; | ||
const handleEdit = () => { | ||
toggleEdit(); | ||
unsetPopupOpen(); | ||
}; | ||
|
||
useEffect(() => { | ||
if (imageRendererKey.getState(view.state)?.linkAdded) { | ||
setEditing(); | ||
} | ||
}, [view, setEditing]); | ||
const isVisibleImageForm = edit; | ||
const isVisibleEditButton = !edit && (visible || popupOpen); | ||
const isVisiblePopup = !edit && popupOpen; | ||
|
||
if (edit) | ||
return ( | ||
<ImageForm | ||
node={node} | ||
view={view} | ||
updateAttributes={updateAttributes} | ||
dom={nodeRef} | ||
unsetEdit={unsetEdit} | ||
/> | ||
); | ||
const handleEditButtonClick = (event: React.MouseEvent<HTMLElement>) => { | ||
event.preventDefault(); | ||
setPopupOpen(); | ||
}; | ||
|
||
return visible ? ( | ||
return ( | ||
<> | ||
<Button | ||
onClick={setPopupOpen} | ||
ref={buttonRef} | ||
size="s" | ||
view={'raised'} | ||
style={{position: 'absolute', right: '3px', top: '3px'}} | ||
> | ||
<Icon data={Ellipsis} /> | ||
</Button> | ||
{isVisibleImageForm && ( | ||
<ImageForm | ||
node={node} | ||
view={view} | ||
updateAttributes={updateAttributes} | ||
dom={nodeRef} | ||
unsetEdit={unsetEdit} | ||
/> | ||
)} | ||
|
||
{isVisibleEditButton && ( | ||
<Button | ||
onClick={handleEditButtonClick} | ||
ref={buttonRef} | ||
size="s" | ||
view={'raised'} | ||
className={b()} | ||
> | ||
<Icon data={Ellipsis} /> | ||
</Button> | ||
)} | ||
|
||
<Popup | ||
open={popupOpen} | ||
open={isVisiblePopup} | ||
anchorRef={buttonRef} | ||
onClose={unsetPopupOpen} | ||
placement={placement} | ||
> | ||
<Menu> | ||
<Menu.Item | ||
onClick={() => { | ||
toggleEdit(); | ||
unsetPopupOpen(); | ||
}} | ||
> | ||
{i18nCommon('edit')} | ||
</Menu.Item> | ||
<Menu.Item | ||
onClick={() => { | ||
const pos = getPos(); | ||
if (pos === undefined) return; | ||
removeNode({ | ||
node, | ||
pos, | ||
tr: view.state.tr, | ||
dispatch: view.dispatch, | ||
}); | ||
view.focus(); | ||
}} | ||
> | ||
{i18nCommon('delete')} | ||
</Menu.Item> | ||
<Menu.Item onClick={handleEdit}>{i18nCommon('edit')}</Menu.Item> | ||
<Menu.Item onClick={onDelete}>{i18nCommon('delete')}</Menu.Item> | ||
</Menu> | ||
</Popup> | ||
</> | ||
) : null; | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.