-
-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
155 additions
and
12 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { EditorView } from 'prosemirror-view'; | ||
import { toggleMark } from 'prosemirror-commands'; | ||
import { EditorState, NodeSelection } from 'prosemirror-state'; | ||
|
||
import MenuItem from '../views/base/MenuItem'; | ||
import { MenuItemSpec, MenuItemViewRender } from '../../types'; | ||
|
||
import Popup from '../views/base/Popup'; | ||
import FormView, { FormInputs, OnSubmitData } from '../views/base/Form'; | ||
|
||
const getFormInputs = (url = '', alt = '', title = ''): FormInputs => [ | ||
[ | ||
{ | ||
type: 'url', | ||
required: true, | ||
label: 'URL', | ||
name: 'url', | ||
defaultValue: url | ||
} | ||
], | ||
[ | ||
{ | ||
type: 'text', | ||
required: false, | ||
label: 'Alt Text', | ||
name: 'altText', | ||
defaultValue: alt | ||
} | ||
], | ||
[ | ||
{ | ||
type: 'text', | ||
required: false, | ||
label: 'Title', | ||
name: 'title', | ||
defaultValue: title | ||
} | ||
] | ||
]; | ||
|
||
const updateImage = (view: EditorView, data: OnSubmitData) => { | ||
const { dispatch, state: { schema, tr } } = view; | ||
|
||
const attrs = { | ||
src: data.url, | ||
alt: data.altText ?? '', | ||
title: data.title ?? '' | ||
}; | ||
|
||
dispatch(tr.replaceSelectionWith(schema.nodes.image.createAndFill(attrs))); | ||
}; | ||
|
||
const image = (view: EditorView, spec: MenuItemSpec): MenuItemViewRender => { | ||
const { dom, update: updateDom } = new MenuItem(spec); | ||
|
||
const onPopupOpen = () => { | ||
const { state: { selection } } = view; | ||
|
||
if (selection instanceof NodeSelection) { | ||
const { src, alt, title } = selection.node.attrs; | ||
renderForm(getFormInputs(src, alt, title)); | ||
} else { | ||
renderForm(getFormInputs()); | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
const onPopupClose = () => { | ||
return true; | ||
}; | ||
|
||
const updateActiveState = () => { | ||
setActiveState(); | ||
}; | ||
|
||
const popupOptions = { | ||
menuDOM: dom, | ||
onOpen: onPopupOpen, | ||
afterOpen: updateActiveState, | ||
onClose: onPopupClose, | ||
afterClose: updateActiveState | ||
}; | ||
|
||
const { dom: popupDOM, closePopup, isPopupOpen } = new Popup(popupOptions); | ||
|
||
const onSubmit = (data: OnSubmitData) => { | ||
updateImage(view, data); | ||
closePopup(); | ||
}; | ||
|
||
const { dom: formDom, render: renderForm } = new FormView({ inputs: getFormInputs(), onSubmit }); | ||
|
||
popupDOM.appendChild(formDom); | ||
dom.appendChild(popupDOM); | ||
|
||
const setActiveState = () => { | ||
updateDom({ | ||
active: isPopupOpen(), | ||
disabled: false | ||
}); | ||
}; | ||
|
||
const update = (state: EditorState) => { | ||
const { schema, selection } = state; | ||
|
||
const command = toggleMark(schema.nodes.image); | ||
const canExecute = command(state, null); | ||
|
||
let isActive = false; | ||
|
||
if (selection instanceof NodeSelection) { | ||
isActive = selection.node.type.name === 'image'; | ||
} | ||
|
||
updateDom({ | ||
active: isPopupOpen() || isActive, | ||
disabled: !canExecute | ||
}); | ||
}; | ||
|
||
return { | ||
dom, | ||
update | ||
}; | ||
}; | ||
|
||
export default image; |
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
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
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,4 @@ | ||
export default ` | ||
<path d="M0 0h24v24H0z" fill="none"/> | ||
<path d="M21 19V5c0-1.1-.9-2-2-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2zM8.5 13.5l2.5 3.01L14.5 12l4.5 6H5l3.5-4.5z"/> | ||
`; |
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