forked from microsoft/roosterjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port demo site step 2 (microsoft#2465)
- Loading branch information
1 parent
2d9d5f0
commit 3f4928b
Showing
10 changed files
with
252 additions
and
5 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
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
112 changes: 112 additions & 0 deletions
112
packages/roosterjs-content-model-plugins/lib/contextMenuBase/ContextMenuPluginBase.ts
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,112 @@ | ||
import type { EditorPlugin, IEditor, PluginEvent } from 'roosterjs-content-model-types'; | ||
|
||
/** | ||
* Context Menu options for ContextMenu plugin | ||
*/ | ||
export interface ContextMenuOptions<T> { | ||
/** | ||
* Render function for the context menu | ||
* @param container The container HTML element, it will be located at the mouse click position, | ||
* so the callback just need to render menu content into this container | ||
* @param onDismiss The onDismiss callback, some menu render need to know this callback so that | ||
* it can handle the dismiss event | ||
*/ | ||
render: (container: HTMLElement, items: (T | null)[], onDismiss: () => void) => void; | ||
|
||
/** | ||
* Dismiss function for the context menu, it will be called when user wants to dismiss this context menu | ||
* e.g. user click away so the menu should be dismissed | ||
* @param container The container HTML element | ||
*/ | ||
dismiss?: (container: HTMLElement) => void; | ||
|
||
/** | ||
* Whether the default context menu is allowed. @default false | ||
*/ | ||
allowDefaultMenu?: boolean; | ||
} | ||
|
||
/** | ||
* An editor plugin that support showing a context menu using render() function from options parameter | ||
*/ | ||
export class ContextMenuPluginBase<T> implements EditorPlugin { | ||
private container: HTMLElement | null = null; | ||
private editor: IEditor | null = null; | ||
private isMenuShowing: boolean = false; | ||
|
||
/** | ||
* Create a new instance of ContextMenu class | ||
* @param options An options object to determine how to show/hide the context menu | ||
*/ | ||
constructor(private options: ContextMenuOptions<T>) {} | ||
|
||
/** | ||
* Get a friendly name of this plugin | ||
*/ | ||
getName() { | ||
return 'ContextMenu'; | ||
} | ||
|
||
/** | ||
* Initialize this plugin | ||
* @param editor The editor instance | ||
*/ | ||
initialize(editor: IEditor) { | ||
this.editor = editor; | ||
} | ||
|
||
/** | ||
* Dispose this plugin | ||
*/ | ||
dispose() { | ||
this.onDismiss(); | ||
|
||
if (this.container?.parentNode) { | ||
this.container.parentNode.removeChild(this.container); | ||
this.container = null; | ||
} | ||
this.editor = null; | ||
} | ||
|
||
/** | ||
* Handle events triggered from editor | ||
* @param event PluginEvent object | ||
*/ | ||
onPluginEvent(event: PluginEvent) { | ||
if (event.eventType == 'contextMenu' && event.items.length > 0) { | ||
const { rawEvent, items } = event; | ||
|
||
this.onDismiss(); | ||
|
||
if (!this.options.allowDefaultMenu) { | ||
rawEvent.preventDefault(); | ||
} | ||
|
||
if (this.initContainer(rawEvent.pageX, rawEvent.pageY)) { | ||
this.options.render(this.container!, items as T[], this.onDismiss); | ||
this.isMenuShowing = true; | ||
} | ||
} | ||
} | ||
|
||
private initContainer(x: number, y: number) { | ||
if (!this.container && this.editor) { | ||
this.container = this.editor.getDocument().createElement('div'); | ||
|
||
this.container.style.position = 'fixed'; | ||
this.container.style.width = '0'; | ||
this.container.style.height = '0'; | ||
this.editor.getDocument().body.appendChild(this.container); | ||
} | ||
this.container?.style.setProperty('left', x + 'px'); | ||
this.container?.style.setProperty('top', y + 'px'); | ||
return !!this.container; | ||
} | ||
|
||
private onDismiss = () => { | ||
if (this.container && this.isMenuShowing) { | ||
this.options.dismiss?.(this.container); | ||
this.isMenuShowing = false; | ||
} | ||
}; | ||
} |
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
54 changes: 54 additions & 0 deletions
54
packages/roosterjs-content-model-types/lib/parameter/ImageEditor.ts
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,54 @@ | ||
/** | ||
* Type of image editing operations | ||
*/ | ||
export type ImageEditOperation = | ||
/** | ||
* Resize an image | ||
*/ | ||
| 'resize' | ||
|
||
/** | ||
* Rotate an image | ||
*/ | ||
| 'rotate' | ||
|
||
/** | ||
* Crop an image | ||
*/ | ||
| 'crop'; | ||
|
||
/** | ||
* Define the common operation of an image editor | ||
*/ | ||
export interface ImageEditor { | ||
/** | ||
* Check if the given editing operation is allowed on current selected image | ||
* @param operation The operation to check | ||
* @returns True if the operation is allowed, otherwise false | ||
*/ | ||
isOperationAllowed(operation: ImageEditOperation): boolean; | ||
|
||
/** | ||
* Check if the given image can be regenerated by this image editor | ||
* @param image The image to check | ||
* @returns True if the image can be regenerated, otherwise false | ||
*/ | ||
canRegenerateImage(image: HTMLImageElement): boolean; | ||
|
||
/** | ||
* Rotate selected image to the given angle (in rad) | ||
* @param angleRad The angle to rotate to | ||
*/ | ||
rotateImage(angleRad: number): void; | ||
|
||
/** | ||
* Flip the image. | ||
* @param direction Direction of flip, can be vertical or horizontal | ||
*/ | ||
flipImage(direction: 'vertical' | 'horizontal'): void; | ||
|
||
/** | ||
* Start to crop selected image | ||
*/ | ||
cropImage(): void; | ||
} |
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