Formatting: Expand exceptions to multi-newline ops, multiple ops

This commit is contained in:
Scott Nonnenberg 2023-06-08 14:50:44 -07:00 committed by GitHub
parent 3ac2fb4ce4
commit 6e1916030d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 8 deletions

View file

@ -3,11 +3,13 @@
import type Quill from 'quill';
import type { KeyboardContext } from 'quill';
import type Op from 'quill-delta/dist/Op';
import React from 'react';
import classNames from 'classnames';
import { Popper } from 'react-popper';
import { createPortal } from 'react-dom';
import type { VirtualElement } from '@popperjs/core';
import { isString } from 'lodash';
import * as log from '../../logging/log';
import * as Errors from '../../types/errors';
@ -54,6 +56,14 @@ function getMetaKey(platform: string, i18n: LocalizerType) {
return i18n('icu:Keyboard--Key--ctrl');
}
function isAllNewlines(ops: Array<Op>): boolean {
return ops.every(isNewlineOnlyOp);
}
export function isNewlineOnlyOp(op: Op): boolean {
return isString(op.insert) && /^\n+$/gm.test(op.insert);
}
export class FormattingMenu {
// Cache the results of our virtual elements's last rect calculation
lastRect: DOMRect | undefined;
@ -181,12 +191,12 @@ export class FormattingMenu {
return;
}
// Note: we special-case single \n ops because Quill doesn't apply formatting to them
// Note: we special-case all-newline ops because Quill doesn't apply styles to them
const contents = this.quill.getContents(
quillSelection.index,
quillSelection.length
);
if (contents.length() === 1 && contents.ops[0].insert === '\n') {
if (isAllNewlines(contents.ops)) {
this.scheduleRemoval();
return;
}
@ -261,13 +271,12 @@ export class FormattingMenu {
const contents = this.quill.getContents(selection.index, selection.length);
// Note: we special-case single \n ops because Quill doesn't apply formatting to them
if (contents.length() === 1 && contents.ops[0].insert === '\n') {
this.scheduleRemoval();
if (isAllNewlines(contents.ops)) {
return false;
}
return contents.ops.every(
op => op.attributes?.[style] || op.insert === '\n'
op => op.attributes?.[style] || isNewlineOnlyOp(op)
);
}