Skip to content

Commit

Permalink
Use window of current default view (#2918)
Browse files Browse the repository at this point in the history
* Use window of current default view

* Use window of current default view

* Fix codes
  • Loading branch information
trueadm authored Aug 31, 2022
1 parent b082de9 commit 1580cbc
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 12 deletions.
13 changes: 12 additions & 1 deletion packages/lexical/src/LexicalEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ import {
triggerListeners,
updateEditor,
} from './LexicalUpdates';
import {createUID, dispatchCommand, markAllNodesAsDirty} from './LexicalUtils';
import {
createUID,
dispatchCommand,
getDefaultView,
markAllNodesAsDirty,
} from './LexicalUtils';
import {DecoratorNode} from './nodes/LexicalDecoratorNode';
import {LineBreakNode} from './nodes/LexicalLineBreakNode';
import {ParagraphNode} from './nodes/LexicalParagraphNode';
Expand Down Expand Up @@ -450,6 +455,7 @@ export class LexicalEditor {
_onError: ErrorHandler;
_htmlConversions: DOMConversionCache;
_readOnly: boolean;
_window: null | Window;

constructor(
editorState: EditorState,
Expand Down Expand Up @@ -508,6 +514,7 @@ export class LexicalEditor {
this._htmlConversions = htmlConversions;
this._readOnly = false;
this._headless = false;
this._window = null;
}

isComposing(): boolean {
Expand Down Expand Up @@ -695,11 +702,13 @@ export class LexicalEditor {
}

if (nextRootElement !== null) {
const windowObj = getDefaultView(nextRootElement);
const style = nextRootElement.style;
style.userSelect = 'text';
style.whiteSpace = 'pre-wrap';
style.wordBreak = 'break-word';
nextRootElement.setAttribute('data-lexical-editor', 'true');
this._window = windowObj;
this._dirtyType = FULL_RECONCILE;
initMutationObserver(this);

Expand All @@ -711,6 +720,8 @@ export class LexicalEditor {
if (!this._config.disableEvents) {
addRootElementEvents(nextRootElement, this);
}
} else {
this._window = null;
}

triggerListeners('root', this, false, nextRootElement, prevRootElement);
Expand Down
5 changes: 3 additions & 2 deletions packages/lexical/src/LexicalEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import {
getDOMTextNode,
getEditorsToPropagate,
getNearestEditorFromDOMNode,
getWindow,
isBackspace,
isBold,
isCopy,
Expand Down Expand Up @@ -237,7 +238,7 @@ function onSelectionChange(
// If we have marked a collapsed selection format, and we're
// within the given time range – then attempt to use that format
// instead of getting the format from the anchor node.
const windowEvent = window.event;
const windowEvent = getWindow(editor).event;
const currentTimeStamp = windowEvent
? windowEvent.timeStamp
: performance.now();
Expand Down Expand Up @@ -372,7 +373,7 @@ function onBeforeInput(event: InputEvent, editor: LexicalEditor): void {
// user has dom.event.clipboardevents.enabled disabled in
// about:config. In that case, we need to process the
// pasted content in the DOM mutation phase.
(IS_FIREFOX && isFirefoxClipboardEvents())
(IS_FIREFOX && isFirefoxClipboardEvents(editor))
) {
return;
} else if (inputType === 'insertCompositionText') {
Expand Down
9 changes: 5 additions & 4 deletions packages/lexical/src/LexicalMutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
$getNearestNodeFromDOMNode,
$updateTextNodeFromDOMContent,
getNodeFromDOMNode,
getWindow,
internalGetRoot,
isFirefoxClipboardEvents,
} from './LexicalUtils';
Expand All @@ -48,9 +49,9 @@ function updateTimeStamp(event: Event) {
lastTextEntryTimeStamp = event.timeStamp;
}

function initTextEntryListener(): void {
function initTextEntryListener(editor: LexicalEditor): void {
if (lastTextEntryTimeStamp === 0) {
window.addEventListener('textInput', updateTimeStamp, true);
getWindow(editor).addEventListener('textInput', updateTimeStamp, true);
}
}

Expand Down Expand Up @@ -292,7 +293,7 @@ export function $flushMutations(
$setSelection(selection);
}

if (IS_FIREFOX && isFirefoxClipboardEvents()) {
if (IS_FIREFOX && isFirefoxClipboardEvents(editor)) {
selection.insertRawText(possibleTextForFirefoxPaste);
}
}
Expand All @@ -312,7 +313,7 @@ export function flushRootMutations(editor: LexicalEditor): void {
}

export function initMutationObserver(editor: LexicalEditor): void {
initTextEntryListener();
initTextEntryListener(editor);
editor._observer = new MutationObserver(
(mutations: Array<MutationRecord>, observer: MutationObserver) => {
$flushMutations(editor, mutations, observer);
Expand Down
6 changes: 5 additions & 1 deletion packages/lexical/src/LexicalSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2281,6 +2281,10 @@ export function internalCreateRangeSelection(
domSelection: Selection | null,
editor: LexicalEditor,
): null | RangeSelection {
const windowObj = editor._window;
if (windowObj === null) {
return null;
}
// When we create a selection, we try to use the previous
// selection where possible, unless an actual user selection
// change has occurred. When we do need to create a new selection
Expand All @@ -2295,7 +2299,7 @@ export function internalCreateRangeSelection(
// reconciliation unless there are dirty nodes that need
// reconciling.

const windowEvent = window.event;
const windowEvent = windowObj.event;
const eventType = windowEvent ? windowEvent.type : undefined;
const isSelectionChange = eventType === 'selectionchange';
const useDOMSelection =
Expand Down
19 changes: 16 additions & 3 deletions packages/lexical/src/LexicalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1091,8 +1091,8 @@ export function $getDecoratorNode(
return null;
}

export function isFirefoxClipboardEvents(): boolean {
const event = window.event;
export function isFirefoxClipboardEvents(editor: LexicalEditor): boolean {
const event = getWindow(editor).event;
const inputType = event && (event as InputEvent).inputType;
return (
inputType === 'insertFromPaste' ||
Expand Down Expand Up @@ -1148,7 +1148,7 @@ export function scrollIntoViewIfNeeded(
if (element !== null) {
const rect = element.getBoundingClientRect();

if (rect.bottom > window.innerHeight) {
if (rect.bottom > getWindow(editor).innerHeight) {
element.scrollIntoView(false);
} else if (rect.top < 0) {
element.scrollIntoView();
Expand Down Expand Up @@ -1212,3 +1212,16 @@ function $hasAncestor(child: LexicalNode, targetNode: LexicalNode): boolean {
}
return false;
}

export function getDefaultView(domElem: HTMLElement): Window | null {
const ownerDoc = domElem.ownerDocument;
return (ownerDoc && ownerDoc.defaultView) || null;
}

export function getWindow(editor: LexicalEditor): Window {
const windowObj = editor._window;
if (windowObj === null) {
invariant(false, 'window object not found');
}
return windowObj;
}
4 changes: 3 additions & 1 deletion scripts/error-codes/codes.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,7 @@
"74": "Create node: Type %s in node %s does not match registered node %s with the same type",
"75": "Reconciliation: could not find DOM element for node key %s",
"76": "append: attempting to append self",
"77": "LexicalAutoLinkPlugin: AutoLinkNode not registered on editor"
"77": "LexicalAutoLinkPlugin: AutoLinkNode not registered on editor",
"78": "window object not found",
"79": "MarkdownShortcuts: missing dependency for transformer. Ensure node dependency is included in editor initial config."
}

2 comments on commit 1580cbc

@vercel
Copy link

@vercel vercel bot commented on 1580cbc Aug 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical – ./packages/lexical-website

lexical-fbopensource.vercel.app
lexicaljs.org
lexical.dev
lexicaljs.com
www.lexical.dev
lexical-git-main-fbopensource.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 1580cbc Aug 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical-playground – ./packages/lexical-playground

playground.lexical.dev
lexical-playground-git-main-fbopensource.vercel.app
lexical-playground.vercel.app
lexical-playground-fbopensource.vercel.app

Please sign in to comment.