-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Standalone Editor: CreateStandaloneEditorCore (#2218)
- Loading branch information
1 parent
700e8e8
commit faae180
Showing
27 changed files
with
902 additions
and
159 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
69 changes: 69 additions & 0 deletions
69
packages-content-model/roosterjs-content-model-core/lib/coreApi/getVisibleViewport.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,69 @@ | ||
import type { Rect } from 'roosterjs-editor-types'; | ||
import type { GetVisibleViewport } from 'roosterjs-content-model-types'; | ||
|
||
/** | ||
* @internal | ||
* Retrieves the rect of the visible viewport of the editor. | ||
* @param core The StandaloneEditorCore object | ||
*/ | ||
export const getVisibleViewport: GetVisibleViewport = core => { | ||
const scrollContainer = core.domEvent.scrollContainer; | ||
|
||
return getIntersectedRect( | ||
scrollContainer == core.contentDiv ? [scrollContainer] : [scrollContainer, core.contentDiv] | ||
); | ||
}; | ||
|
||
/** | ||
* Get the intersected Rect of elements provided | ||
* | ||
* @example | ||
* The result of the following Elements Rects would be: | ||
{ | ||
top: Element2.top, | ||
bottom: Element1.bottom, | ||
left: Element2.left, | ||
right: Element2.right | ||
} | ||
+-------------------------+ | ||
| Element 1 | | ||
| +-----------------+ | | ||
| | Element2 | | | ||
| | | | | ||
| | | | | ||
+-------------------------+ | ||
| | | ||
+-----------------+ | ||
* @param elements Elements to use. | ||
* @param additionalRects additional rects to use | ||
* @returns If the Rect is valid return the rect, if not, return null. | ||
*/ | ||
function getIntersectedRect(elements: HTMLElement[], additionalRects: Rect[] = []): Rect | null { | ||
const rects = elements | ||
.map(element => normalizeRect(element.getBoundingClientRect())) | ||
.concat(additionalRects) | ||
.filter((rect: Rect | null): rect is Rect => !!rect); | ||
|
||
const result: Rect = { | ||
top: Math.max(...rects.map(r => r.top)), | ||
bottom: Math.min(...rects.map(r => r.bottom)), | ||
left: Math.max(...rects.map(r => r.left)), | ||
right: Math.min(...rects.map(r => r.right)), | ||
}; | ||
|
||
return result.top < result.bottom && result.left < result.right ? result : null; | ||
} | ||
|
||
function normalizeRect(clientRect: DOMRect): Rect | null { | ||
const { left, right, top, bottom } = | ||
clientRect || <DOMRect>{ left: 0, right: 0, top: 0, bottom: 0 }; | ||
return left === 0 && right === 0 && top === 0 && bottom === 0 | ||
? null | ||
: { | ||
left: Math.round(left), | ||
right: Math.round(right), | ||
top: Math.round(top), | ||
bottom: Math.round(bottom), | ||
}; | ||
} |
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
76 changes: 76 additions & 0 deletions
76
packages-content-model/roosterjs-content-model-core/lib/editor/createStandaloneEditorCore.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,76 @@ | ||
import { createStandaloneEditorCorePlugins } from '../corePlugin/createStandaloneEditorCorePlugins'; | ||
import { createStandaloneEditorDefaultSettings } from './createStandaloneEditorDefaultSettings'; | ||
import { DarkColorHandlerImpl } from './DarkColorHandlerImpl'; | ||
import { standaloneCoreApiMap } from './standaloneCoreApiMap'; | ||
import type { | ||
EditorEnvironment, | ||
StandaloneEditorCore, | ||
StandaloneEditorCorePlugins, | ||
StandaloneEditorOptions, | ||
UnportedCoreApiMap, | ||
UnportedCorePluginState, | ||
} from 'roosterjs-content-model-types'; | ||
|
||
/** | ||
* A temporary function to create Standalone Editor core | ||
* @param contentDiv Editor content DIV | ||
* @param options Editor options | ||
*/ | ||
export function createStandaloneEditorCore( | ||
contentDiv: HTMLDivElement, | ||
options: StandaloneEditorOptions, | ||
unportedCoreApiMap: UnportedCoreApiMap, | ||
unportedCorePluginState: UnportedCorePluginState | ||
): StandaloneEditorCore { | ||
const corePlugins = createStandaloneEditorCorePlugins(options, contentDiv); | ||
|
||
return { | ||
contentDiv, | ||
api: { ...standaloneCoreApiMap, ...unportedCoreApiMap, ...options.coreApiOverride }, | ||
originalApi: { ...standaloneCoreApiMap, ...unportedCoreApiMap }, | ||
plugins: [ | ||
corePlugins.cache, | ||
corePlugins.format, | ||
corePlugins.copyPaste, | ||
corePlugins.domEvent, | ||
// TODO: Add additional plugins here | ||
], | ||
environment: createEditorEnvironment(), | ||
darkColorHandler: new DarkColorHandlerImpl(contentDiv, options.baseDarkColor), | ||
imageSelectionBorderColor: options.imageSelectionBorderColor, // TODO: Move to Selection core plugin | ||
trustedHTMLHandler: options.trustedHTMLHandler || defaultTrustHtmlHandler, | ||
...createStandaloneEditorDefaultSettings(options), | ||
...getPluginState(corePlugins), | ||
...unportedCorePluginState, | ||
}; | ||
} | ||
|
||
function createEditorEnvironment(): EditorEnvironment { | ||
// It is ok to use global window here since the environment should always be the same for all windows in one session | ||
const userAgent = window.navigator.userAgent; | ||
|
||
return { | ||
isMac: window.navigator.appVersion.indexOf('Mac') != -1, | ||
isAndroid: /android/i.test(userAgent), | ||
isSafari: | ||
userAgent.indexOf('Safari') >= 0 && | ||
userAgent.indexOf('Chrome') < 0 && | ||
userAgent.indexOf('Android') < 0, | ||
}; | ||
} | ||
|
||
/** | ||
* @internal export for test only | ||
*/ | ||
export function defaultTrustHtmlHandler(html: string) { | ||
return html; | ||
} | ||
|
||
function getPluginState(corePlugins: StandaloneEditorCorePlugins) { | ||
return { | ||
domEvent: corePlugins.domEvent.getState(), | ||
copyPaste: corePlugins.copyPaste.getState(), | ||
cache: corePlugins.cache.getState(), | ||
format: corePlugins.format.getState(), | ||
}; | ||
} |
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
38 changes: 38 additions & 0 deletions
38
packages-content-model/roosterjs-content-model-core/test/coreApi/getVisibleViewportTest.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,38 @@ | ||
import { getVisibleViewport } from '../../lib/coreApi/getVisibleViewport'; | ||
|
||
describe('getVisibleViewport', () => { | ||
it('scrollContainer is same with contentDiv', () => { | ||
const div = { | ||
getBoundingClientRect: () => ({ left: 100, right: 200, top: 300, bottom: 400 }), | ||
}; | ||
const core = { | ||
contentDiv: div, | ||
domEvent: { | ||
scrollContainer: div, | ||
}, | ||
} as any; | ||
|
||
const result = getVisibleViewport(core); | ||
|
||
expect(result).toEqual({ left: 100, right: 200, top: 300, bottom: 400 }); | ||
}); | ||
|
||
it('scrollContainer is different than contentDiv', () => { | ||
const div1 = { | ||
getBoundingClientRect: () => ({ left: 100, right: 200, top: 300, bottom: 400 }), | ||
}; | ||
const div2 = { | ||
getBoundingClientRect: () => ({ left: 150, right: 250, top: 350, bottom: 450 }), | ||
}; | ||
const core = { | ||
contentDiv: div1, | ||
domEvent: { | ||
scrollContainer: div2, | ||
}, | ||
} as any; | ||
|
||
const result = getVisibleViewport(core); | ||
|
||
expect(result).toEqual({ left: 150, right: 200, top: 350, bottom: 400 }); | ||
}); | ||
}); |
Oops, something went wrong.