fix: Disabling editor embeds should work with collaborative editing (#2968)

* fix: Disabling editor embeds should work with collaborative editing

* Design tweaks, fixed dragging
This commit is contained in:
Tom Moor
2022-01-28 18:27:27 -08:00
committed by GitHub
parent e7867e52e0
commit 1cd770e38d
11 changed files with 116 additions and 27 deletions

View File

@@ -104,7 +104,7 @@ function CollectionDescription({ collection }: Props) {
autoFocus={isEditing} autoFocus={isEditing}
onBlur={handleStopEditing} onBlur={handleStopEditing}
maxLength={1000} maxLength={1000}
disableEmbeds embedsDisabled
readOnlyWriteCheckboxes readOnlyWriteCheckboxes
grow grow
/> />

View File

@@ -1,7 +1,6 @@
import * as React from "react"; import * as React from "react";
import { Optional } from "utility-types"; import { Optional } from "utility-types";
import embeds from "@shared/editor/embeds"; import embeds from "@shared/editor/embeds";
import { EmbedDescriptor } from "@shared/editor/types";
import ErrorBoundary from "~/components/ErrorBoundary"; import ErrorBoundary from "~/components/ErrorBoundary";
import { Props as EditorProps } from "~/editor"; import { Props as EditorProps } from "~/editor";
import useDictionary from "~/hooks/useDictionary"; import useDictionary from "~/hooks/useDictionary";
@@ -19,14 +18,12 @@ const SharedEditor = React.lazy(
) )
); );
const EMPTY_ARRAY: EmbedDescriptor[] = [];
export type Props = Optional< export type Props = Optional<
EditorProps, EditorProps,
"placeholder" | "defaultValue" | "onClickLink" | "embeds" | "dictionary" "placeholder" | "defaultValue" | "onClickLink" | "embeds" | "dictionary"
> & { > & {
shareId?: string | undefined; shareId?: string | undefined;
disableEmbeds?: boolean; embedsDisabled?: boolean;
grow?: boolean; grow?: boolean;
onSynced?: () => Promise<void>; onSynced?: () => Promise<void>;
onPublish?: (event: React.MouseEvent) => any; onPublish?: (event: React.MouseEvent) => any;
@@ -94,7 +91,7 @@ function Editor(props: Props, ref: React.Ref<any>) {
ref={ref} ref={ref}
uploadImage={onUploadImage} uploadImage={onUploadImage}
onShowToast={onShowToast} onShowToast={onShowToast}
embeds={props.disableEmbeds ? EMPTY_ARRAY : embeds} embeds={embeds}
dictionary={dictionary} dictionary={dictionary}
{...props} {...props}
onClickLink={onClickLink} onClickLink={onClickLink}

View File

@@ -37,7 +37,7 @@ function HoverPreviewDocument({ url, children }: Props) {
<Editor <Editor
key={document.id} key={document.id}
defaultValue={document.getSummary()} defaultValue={document.getSummary()}
disableEmbeds embedsDisabled
readOnly readOnly
/> />
</React.Suspense> </React.Suspense>

View File

@@ -99,8 +99,8 @@ export default class ComponentView {
} }
} }
stopEvent() { stopEvent(event: Event) {
return true; return event.type !== "mousedown" && !event.type.startsWith("drag");
} }
destroy() { destroy() {

View File

@@ -139,6 +139,8 @@ export type Props = {
onKeyDown?: (event: React.KeyboardEvent<HTMLDivElement>) => void; onKeyDown?: (event: React.KeyboardEvent<HTMLDivElement>) => void;
/** Collection of embed types to render in the document */ /** Collection of embed types to render in the document */
embeds: EmbedDescriptor[]; embeds: EmbedDescriptor[];
/** Whether embeds should be rendered without an iframe */
embedsDisabled?: boolean;
/** Callback when a toast message is triggered (eg "link copied") */ /** Callback when a toast message is triggered (eg "link copied") */
onShowToast?: (message: string, code: ToastType) => void; onShowToast?: (message: string, code: ToastType) => void;
className?: string; className?: string;

View File

@@ -410,20 +410,14 @@ function DocumentMenu({
type: "button", type: "button",
title: t("Enable embeds"), title: t("Enable embeds"),
onClick: document.enableEmbeds, onClick: document.enableEmbeds,
visible: visible: !!showToggleEmbeds && document.embedsDisabled,
!!showToggleEmbeds &&
document.embedsDisabled &&
!team.collaborativeEditing,
icon: <BuildingBlocksIcon />, icon: <BuildingBlocksIcon />,
}, },
{ {
type: "button", type: "button",
title: t("Disable embeds"), title: t("Disable embeds"),
onClick: document.disableEmbeds, onClick: document.disableEmbeds,
visible: visible: !!showToggleEmbeds && !document.embedsDisabled,
!!showToggleEmbeds &&
!document.embedsDisabled &&
!team.collaborativeEditing,
icon: <BuildingBlocksIcon />, icon: <BuildingBlocksIcon />,
}, },
{ {

View File

@@ -392,7 +392,7 @@ class DocumentScene extends React.Component<Props> {
const team = auth.team; const team = auth.team;
const isShare = !!shareId; const isShare = !!shareId;
const value = revision ? revision.text : document.text; const value = revision ? revision.text : document.text;
const disableEmbeds = const embedsDisabled =
(team && team.documentEmbeds === false) || document.embedsDisabled; (team && team.documentEmbeds === false) || document.embedsDisabled;
const headings = this.editor.current const headings = this.editor.current
? // @ts-expect-error ts-migrate(2571) FIXME: Object is of type 'unknown'. ? // @ts-expect-error ts-migrate(2571) FIXME: Object is of type 'unknown'.
@@ -570,7 +570,7 @@ class DocumentScene extends React.Component<Props> {
)} )}
<Editor <Editor
id={document.id} id={document.id}
key={disableEmbeds ? "disabled" : "enabled"} key={embedsDisabled ? "disabled" : "enabled"}
innerRef={this.editor} innerRef={this.editor}
multiplayer={collaborativeEditing} multiplayer={collaborativeEditing}
shareId={shareId} shareId={shareId}
@@ -580,7 +580,7 @@ class DocumentScene extends React.Component<Props> {
document={document} document={document}
value={readOnly ? value : undefined} value={readOnly ? value : undefined}
defaultValue={value} defaultValue={value}
disableEmbeds={disableEmbeds} embedsDisabled={embedsDisabled}
onSynced={this.onSynced} onSynced={this.onSynced}
onImageUploadStart={this.onImageUploadStart} onImageUploadStart={this.onImageUploadStart}
onImageUploadStop={this.onImageUploadStop} onImageUploadStop={this.onImageUploadStop}

View File

@@ -0,0 +1,70 @@
import { OpenIcon } from "outline-icons";
import * as React from "react";
import styled, { DefaultTheme, ThemeProps } from "styled-components";
import { EmbedProps as Props } from "../";
export default function Simple(props: Props & ThemeProps<DefaultTheme>) {
return (
<Wrapper
className={
props.isSelected
? "ProseMirror-selectednode disabled-embed"
: "disabled-embed"
}
href={props.attrs.href}
target="_blank"
rel="noreferrer nofollow"
>
{props.embed.icon(undefined)}
<Preview>
<Title>{props.embed.title}</Title>
<Subtitle>{props.attrs.href.replace(/^https?:\/\//, "")}</Subtitle>
<StyledOpenIcon color="currentColor" size={20} />
</Preview>
</Wrapper>
);
}
const StyledOpenIcon = styled(OpenIcon)`
margin-left: auto;
`;
const Title = styled.strong`
font-weight: 500;
font-size: 14px;
color: ${(props) => props.theme.text};
`;
const Preview = styled.div`
gap: 8px;
display: flex;
flex-direction: row;
flex-grow: 1;
align-items: center;
color: ${(props) => props.theme.textTertiary};
`;
const Subtitle = styled.span`
font-size: 13px;
color: ${(props) => props.theme.textTertiary} !important;
`;
const Wrapper = styled.a`
display: inline-flex;
align-items: flex-start;
gap: 4px;
color: ${(props) => props.theme.text} !important;
background: ${(props) => props.theme.secondaryBackground};
white-space: nowrap;
border-radius: 8px;
padding: 6px 8px;
max-width: 840px;
width: 100%;
text-overflow: ellipsis;
overflow: hidden;
&:hover {
outline: 2px solid ${(props) => props.theme.divider};
}
`;

View File

@@ -38,6 +38,8 @@ import Image from "./components/Image";
export type EmbedProps = { export type EmbedProps = {
isSelected: boolean; isSelected: boolean;
isEditable: boolean;
embed: EmbedDescriptor;
attrs: { attrs: {
href: string; href: string;
matches: RegExpMatchArray; matches: RegExpMatchArray;

View File

@@ -2,6 +2,7 @@ import Token from "markdown-it/lib/token";
import { NodeSpec, NodeType, Node as ProsemirrorNode } from "prosemirror-model"; import { NodeSpec, NodeType, Node as ProsemirrorNode } from "prosemirror-model";
import { EditorState, Transaction } from "prosemirror-state"; import { EditorState, Transaction } from "prosemirror-state";
import * as React from "react"; import * as React from "react";
import Simple from "../embeds/components/Simple";
import { MarkdownSerializerState } from "../lib/markdown/serializer"; import { MarkdownSerializerState } from "../lib/markdown/serializer";
import embedsRule from "../rules/embeds"; import embedsRule from "../rules/embeds";
import { ComponentProps } from "../types"; import { ComponentProps } from "../types";
@@ -24,7 +25,7 @@ export default class Embed extends Node {
}, },
parseDOM: [ parseDOM: [
{ {
tag: "iframe[class=embed]", tag: "iframe.embed",
getAttrs: (dom: HTMLIFrameElement) => { getAttrs: (dom: HTMLIFrameElement) => {
const { embeds } = this.editor.props; const { embeds } = this.editor.props;
const href = dom.getAttribute("src") || ""; const href = dom.getAttribute("src") || "";
@@ -43,6 +44,14 @@ export default class Embed extends Node {
return {}; return {};
}, },
}, },
{
tag: "a.disabled-embed",
getAttrs: (dom: HTMLAnchorElement) => {
return {
href: dom.getAttribute("href") || "",
};
},
},
], ],
toDOM: (node) => [ toDOM: (node) => [
"iframe", "iframe",
@@ -57,22 +66,24 @@ export default class Embed extends Node {
} }
component({ isEditable, isSelected, theme, node }: ComponentProps) { component({ isEditable, isSelected, theme, node }: ComponentProps) {
const { embeds } = this.editor.props; const { embeds, embedsDisabled } = this.editor.props;
// matches are cached in module state to avoid re running loops and regex // matches are cached in module state to avoid re running loops and regex
// here. Unfortuantely this function is not compatible with React.memo or // here. Unfortunately this function is not compatible with React.memo or
// we would use that instead. // we would use that instead.
const hit = cache[node.attrs.href]; const hit = cache[node.attrs.href];
let Component = hit ? hit.Component : undefined; let Component = hit ? hit.Component : undefined;
let matches = hit ? hit.matches : undefined; let matches = hit ? hit.matches : undefined;
let embed = hit ? hit.embed : undefined;
if (!Component) { if (!Component) {
for (const embed of embeds) { for (const e of embeds) {
const m = embed.matcher(node.attrs.href); const m = e.matcher(node.attrs.href);
if (m) { if (m) {
Component = embed.component; Component = e.component;
matches = m; matches = m;
cache[node.attrs.href] = { Component, matches }; embed = e;
cache[node.attrs.href] = { Component, embed, matches };
} }
} }
} }
@@ -81,6 +92,18 @@ export default class Embed extends Node {
return null; return null;
} }
if (embedsDisabled) {
return (
<Simple
attrs={{ href: node.attrs.href, matches }}
embed={embed}
isEditable={isEditable}
isSelected={isSelected}
theme={theme}
/>
);
}
return ( return (
<Component <Component
attrs={{ ...node.attrs, matches }} attrs={{ ...node.attrs, matches }}

View File

@@ -22,6 +22,7 @@ export type MenuItem = {
}; };
export type EmbedDescriptor = MenuItem & { export type EmbedDescriptor = MenuItem & {
icon: React.FC<any>;
matcher: (url: string) => boolean | [] | RegExpMatchArray; matcher: (url: string) => boolean | [] | RegExpMatchArray;
component: typeof React.Component | React.FC<any>; component: typeof React.Component | React.FC<any>;
}; };