mirror of
https://github.com/outline/outline.git
synced 2026-11-29 16:40:27 +03:00
recent issue sources
This commit is contained in:
1 parent
f00bec87d7
commit
3219cf7dbe
8 files changed
+242
-63
No files matched your search
@@ -1,6 +1,5 @@
|
||||
import { IssueSource } from "@shared/schema";
|
||||
import { ellipsis } from "@shared/styles";
|
||||
import { UnfurlResourceType, UnfurlResponse } from "@shared/types";
|
||||
import { observer } from "mobx-react";
|
||||
import React from "react";
|
||||
import { Trans, useTranslation } from "react-i18next";
|
||||
@@ -19,7 +18,7 @@ import { client } from "~/utils/ApiClient";
|
||||
type Props = {
|
||||
issueTitle: string;
|
||||
open: boolean;
|
||||
onCreate: (issue: UnfurlResponse[UnfurlResourceType.Issue]) => void;
|
||||
onCreate: (source: IssueSource) => Promise<void>;
|
||||
onClose: () => void;
|
||||
};
|
||||
|
||||
@@ -32,32 +31,24 @@ export const CreateIssueDialog = observer(
|
||||
const {
|
||||
data: sources,
|
||||
loading,
|
||||
error,
|
||||
request,
|
||||
} = useRequest<IssueSource[]>(
|
||||
React.useCallback(async () => {
|
||||
const res = await client.post("/issues.list_sources");
|
||||
return res.data;
|
||||
}, [])
|
||||
try {
|
||||
const res = await client.post("/issues.list_sources");
|
||||
return res.data;
|
||||
} catch (err) {
|
||||
toast.error(t("Couldn't load issue sources, try again?"));
|
||||
throw err;
|
||||
}
|
||||
}, [t])
|
||||
);
|
||||
|
||||
const handleCreateIssue = React.useCallback(async () => {
|
||||
try {
|
||||
setCreating();
|
||||
|
||||
const res = await client.post("/issues.create", {
|
||||
title: issueTitle,
|
||||
source: selectedSource,
|
||||
});
|
||||
|
||||
onCreate(res.data);
|
||||
|
||||
toast.success(t("Issue created"));
|
||||
} catch (err) {
|
||||
unsetCreating();
|
||||
toast.error(t("Couldn’t create the issue, try again?"));
|
||||
}
|
||||
}, [t, issueTitle, setCreating, unsetCreating, selectedSource, onCreate]);
|
||||
setCreating();
|
||||
await onCreate(selectedSource!);
|
||||
unsetCreating();
|
||||
}, [selectedSource, onCreate, setCreating, unsetCreating]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (open) {
|
||||
@@ -106,7 +97,7 @@ export const CreateIssueDialog = observer(
|
||||
}}
|
||||
/>
|
||||
) : (
|
||||
t("Select a location to move")
|
||||
t("Select a source to create issue")
|
||||
)}
|
||||
</StyledText>
|
||||
<Button
|
||||
|
||||
@@ -9,6 +9,7 @@ import { isMarkActive } from "@shared/editor/queries/isMarkActive";
|
||||
import { isNodeActive } from "@shared/editor/queries/isNodeActive";
|
||||
import { getColumnIndex, getRowIndex } from "@shared/editor/queries/table";
|
||||
import { MenuItem } from "@shared/editor/types";
|
||||
import { useRecentIssueSources } from "~/editor/hooks/useRecentIssueSources";
|
||||
import useBoolean from "~/hooks/useBoolean";
|
||||
import useDictionary from "~/hooks/useDictionary";
|
||||
import useEventListener from "~/hooks/useEventListener";
|
||||
@@ -106,6 +107,7 @@ export default function SelectionToolbar(props: Props) {
|
||||
const isActive = useIsActive(view.state) || isMobile;
|
||||
const isDragging = useIsDragging();
|
||||
const previousIsActive = usePrevious(isActive);
|
||||
const { issueSources: recentIssueSources } = useRecentIssueSources();
|
||||
|
||||
React.useEffect(() => {
|
||||
// Trigger callbacks when the toolbar is opened or closed
|
||||
@@ -213,7 +215,13 @@ export default function SelectionToolbar(props: Props) {
|
||||
} else if (isNoticeSelection && selection.empty) {
|
||||
items = getNoticeMenuItems(state, readOnly, dictionary);
|
||||
} else {
|
||||
items = getFormattingMenuItems(state, isTemplate, isMobile, dictionary);
|
||||
items = getFormattingMenuItems(
|
||||
state,
|
||||
isTemplate,
|
||||
isMobile,
|
||||
recentIssueSources,
|
||||
dictionary
|
||||
);
|
||||
}
|
||||
|
||||
// Some extensions may be disabled, remove corresponding items
|
||||
|
||||
@@ -2,7 +2,14 @@ import Extension from "@shared/editor/lib/Extension";
|
||||
import { isRemoteTransaction } from "@shared/editor/lib/multiplayer";
|
||||
import { recreateTransform } from "@shared/editor/lib/prosemirror-recreate-transform";
|
||||
import { isInCode } from "@shared/editor/queries/isInCode";
|
||||
import { MentionType, UnfurlResourceType, UnfurlResponse } from "@shared/types";
|
||||
import { IssueSource } from "@shared/schema";
|
||||
import {
|
||||
MentionPlaceholder,
|
||||
MentionType,
|
||||
UnfurlResourceType,
|
||||
UnfurlResponse,
|
||||
} from "@shared/types";
|
||||
import { t } from "i18next";
|
||||
import { action, observable } from "mobx";
|
||||
import {
|
||||
Command,
|
||||
@@ -13,10 +20,13 @@ import {
|
||||
} from "prosemirror-state";
|
||||
import { Decoration, DecorationSet } from "prosemirror-view";
|
||||
import React from "react";
|
||||
import { toast } from "sonner";
|
||||
import { Primitive } from "utility-types";
|
||||
import { v4 } from "uuid";
|
||||
import stores from "~/stores";
|
||||
import { client } from "~/utils/ApiClient";
|
||||
import { CreateIssueDialog } from "../components/CreateIssueDialog";
|
||||
import { addRecentIssueSource } from "../hooks/useRecentIssueSources";
|
||||
|
||||
export default class CreateIssue extends Extension {
|
||||
private state: {
|
||||
@@ -40,11 +50,8 @@ export default class CreateIssue extends Extension {
|
||||
state: {
|
||||
init: () => DecorationSet.empty,
|
||||
apply: (tr, set) => {
|
||||
let mapping = tr.mapping;
|
||||
|
||||
// See if the transaction adds or removes any placeholders
|
||||
// See if the transaction adds, replaces, or removes any placeholders.
|
||||
const meta = tr.getMeta(this.key);
|
||||
const hasDecorations = set.find().length;
|
||||
|
||||
// We only want a single paste placeholder at a time, so if we're adding a new
|
||||
// placeholder we can just return a new DecorationSet and avoid mapping logic.
|
||||
@@ -54,9 +61,7 @@ export default class CreateIssue extends Extension {
|
||||
Decoration.inline(
|
||||
from,
|
||||
to,
|
||||
{
|
||||
class: "create-issue-placeholder",
|
||||
},
|
||||
{},
|
||||
{
|
||||
id,
|
||||
}
|
||||
@@ -65,6 +70,9 @@ export default class CreateIssue extends Extension {
|
||||
return DecorationSet.create(tr.doc, decorations);
|
||||
}
|
||||
|
||||
let mapping = tr.mapping;
|
||||
const hasDecorations = set.find().length;
|
||||
|
||||
if (hasDecorations && (isRemoteTransaction(tr) || meta)) {
|
||||
try {
|
||||
mapping = recreateTransform(tr.before, tr.doc, {
|
||||
@@ -80,6 +88,16 @@ export default class CreateIssue extends Extension {
|
||||
|
||||
set = set.map(mapping, tr.doc);
|
||||
|
||||
if (meta?.replace) {
|
||||
const { id } = meta.replace;
|
||||
const decorations = set.find(
|
||||
undefined,
|
||||
undefined,
|
||||
(spec) => spec.id === id
|
||||
);
|
||||
return DecorationSet.create(tr.doc, decorations);
|
||||
}
|
||||
|
||||
if (meta?.remove) {
|
||||
const { id } = meta.remove;
|
||||
const decorations = set.find(
|
||||
@@ -128,20 +146,48 @@ export default class CreateIssue extends Extension {
|
||||
|
||||
commands() {
|
||||
return (attrs: Record<string, Primitive>): Command =>
|
||||
(state, dispatch) => {
|
||||
action((state, dispatch) => {
|
||||
const title = attrs.title as string;
|
||||
const source = attrs.source
|
||||
? (JSON.parse(attrs.source as string) as IssueSource)
|
||||
: undefined;
|
||||
|
||||
this.state.title = title;
|
||||
|
||||
const { from } = state.selection;
|
||||
const to = from + title.length;
|
||||
|
||||
const tr = state.tr
|
||||
.setSelection(TextSelection.near(state.doc.resolve(from)))
|
||||
.setSelection(TextSelection.near(state.doc.resolve(to)))
|
||||
.setMeta(this.key, { add: { from, to, id: title } });
|
||||
|
||||
dispatch?.(tr);
|
||||
|
||||
this.openDialog(title);
|
||||
if (source) {
|
||||
tr.replaceWith(
|
||||
from,
|
||||
to,
|
||||
state.schema.nodes.mention.create({
|
||||
id: v4(),
|
||||
type: MentionPlaceholder,
|
||||
label: title,
|
||||
href: title,
|
||||
modelId: v4(),
|
||||
actorId: stores.auth.currentUserId,
|
||||
})
|
||||
).setMeta(this.key, { replace: { id: title } });
|
||||
}
|
||||
|
||||
dispatch?.(tr);
|
||||
|
||||
if (source) {
|
||||
void this.createIssue(source);
|
||||
} else {
|
||||
this.openDialog(title);
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
widget = () => (
|
||||
@@ -153,30 +199,74 @@ export default class CreateIssue extends Extension {
|
||||
/>
|
||||
);
|
||||
|
||||
private createIssue = action(
|
||||
(issue: UnfurlResponse[UnfurlResourceType.Issue]) => {
|
||||
const { view } = this.editor;
|
||||
const { state } = view;
|
||||
|
||||
const result = this.findPlaceholder(state, this.state.title);
|
||||
|
||||
if (result) {
|
||||
const tr = state.tr.deleteRange(result[0], result[1]);
|
||||
view.dispatch(
|
||||
tr.setSelection(TextSelection.near(tr.doc.resolve(result[0])))
|
||||
);
|
||||
}
|
||||
|
||||
this.editor.commands.mention({
|
||||
id: v4(),
|
||||
type: MentionType.Issue,
|
||||
label: this.state.title,
|
||||
href: issue.url,
|
||||
modelId: v4(),
|
||||
actorId: stores.auth.currentUserId,
|
||||
private createIssue = async (source: IssueSource) => {
|
||||
try {
|
||||
addRecentIssueSource(source);
|
||||
const res = await client.post("/issues.create", {
|
||||
title: this.state.title,
|
||||
source,
|
||||
});
|
||||
this.addMentionNode(res.data);
|
||||
toast.success(t("Issue created"));
|
||||
} catch (err) {
|
||||
this.removeDecorations(source);
|
||||
toast.error(t("Couldn’t create the issue, try again?"));
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
private addMentionNode = (
|
||||
issue: UnfurlResponse[UnfurlResourceType.Issue]
|
||||
) => {
|
||||
const { view } = this.editor;
|
||||
const { state } = view;
|
||||
|
||||
const result = this.findPlaceholder(state, this.state.title);
|
||||
|
||||
if (result) {
|
||||
const tr = state.tr.deleteRange(result[0], result[1]);
|
||||
view.dispatch(
|
||||
tr
|
||||
.setSelection(TextSelection.near(tr.doc.resolve(result[0])))
|
||||
.setMeta(this.key, {
|
||||
remove: { id: this.state.title },
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
this.editor.commands.mention({
|
||||
id: v4(),
|
||||
type: MentionType.Issue,
|
||||
label: this.state.title,
|
||||
href: issue.url,
|
||||
modelId: v4(),
|
||||
actorId: stores.auth.currentUserId,
|
||||
});
|
||||
};
|
||||
|
||||
private removeDecorations = action((source: IssueSource) => {
|
||||
const { view } = this.editor;
|
||||
const { state } = view;
|
||||
|
||||
const tr = state.tr.setMeta(this.key, {
|
||||
remove: { id: this.state.title },
|
||||
});
|
||||
|
||||
const result = this.findPlaceholder(state, this.state.title);
|
||||
|
||||
// Placeholder node would have been inserted in recent issue menu flow only.
|
||||
// We want to reset it with the selected text.
|
||||
if (source && result) {
|
||||
tr.replaceWith(
|
||||
result[0],
|
||||
result[1],
|
||||
state.schema.nodeFromJSON({ type: "text", text: this.state.title })
|
||||
);
|
||||
}
|
||||
|
||||
view.dispatch(tr);
|
||||
|
||||
this.state.title = "";
|
||||
});
|
||||
|
||||
private openDialog = action((title: string) => {
|
||||
this.state.title = title;
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import { IssueSource } from "@shared/schema";
|
||||
import Storage from "@shared/utils/Storage";
|
||||
import React from "react";
|
||||
import usePersistedState, {
|
||||
setPersistedState,
|
||||
} from "~/hooks/usePersistedState";
|
||||
|
||||
const StorageKey = "recent-issue-sources";
|
||||
const MaxCount = 5;
|
||||
|
||||
export function useRecentIssueSources() {
|
||||
const [issueSources, setIssueSources] = usePersistedState<IssueSource[]>(
|
||||
StorageKey,
|
||||
[] as IssueSource[]
|
||||
);
|
||||
|
||||
const addIssueSource = React.useCallback(
|
||||
(source: IssueSource) => {
|
||||
const newIssueSources = insertAndTrim(issueSources, source);
|
||||
setIssueSources(newIssueSources);
|
||||
},
|
||||
[issueSources, setIssueSources]
|
||||
);
|
||||
|
||||
return { issueSources, addIssueSource };
|
||||
}
|
||||
|
||||
export function addRecentIssueSource(source: IssueSource) {
|
||||
const issueSources: IssueSource[] = Storage.get(StorageKey) ?? [];
|
||||
const newIssueSources = insertAndTrim(issueSources, source);
|
||||
setPersistedState(StorageKey, newIssueSources);
|
||||
}
|
||||
|
||||
function insertAndTrim(issueSources: IssueSource[], source: IssueSource) {
|
||||
const newIssueSources = issueSources.filter((s) => s.id !== source.id);
|
||||
newIssueSources.unshift(source);
|
||||
|
||||
if (newIssueSources.length > MaxCount) {
|
||||
newIssueSources.pop();
|
||||
}
|
||||
|
||||
return newIssueSources;
|
||||
}
|
||||
|
||||
export type RecentIssueSourcesResponse = ReturnType<
|
||||
typeof useRecentIssueSources
|
||||
>;
|
||||
@@ -1,3 +1,4 @@
|
||||
import { t } from "i18next";
|
||||
import {
|
||||
BoldIcon,
|
||||
CodeIcon,
|
||||
@@ -19,7 +20,7 @@ import {
|
||||
Heading3Icon,
|
||||
PlusIcon,
|
||||
} from "outline-icons";
|
||||
import { EditorState } from "prosemirror-state";
|
||||
import { EditorState, TextSelection } from "prosemirror-state";
|
||||
import * as React from "react";
|
||||
import styled from "styled-components";
|
||||
import Highlight from "@shared/editor/marks/Highlight";
|
||||
@@ -29,14 +30,17 @@ import { isInList } from "@shared/editor/queries/isInList";
|
||||
import { isMarkActive } from "@shared/editor/queries/isMarkActive";
|
||||
import { isNodeActive } from "@shared/editor/queries/isNodeActive";
|
||||
import { MenuItem } from "@shared/editor/types";
|
||||
import { IssueSource } from "@shared/schema";
|
||||
import { metaDisplay } from "@shared/utils/keyboard";
|
||||
import CircleIcon from "~/components/Icons/CircleIcon";
|
||||
import PluginIcon from "~/components/PluginIcon";
|
||||
import { Dictionary } from "~/hooks/useDictionary";
|
||||
|
||||
export default function formattingMenuItems(
|
||||
state: EditorState,
|
||||
isTemplate: boolean,
|
||||
isMobile: boolean,
|
||||
recentIssueSources: IssueSource[],
|
||||
dictionary: Dictionary
|
||||
): MenuItem[] {
|
||||
const { schema } = state;
|
||||
@@ -44,12 +48,39 @@ export default function formattingMenuItems(
|
||||
const isCodeBlock = isInCode(state, { onlyBlock: true });
|
||||
const isEmpty = state.selection.empty;
|
||||
|
||||
const selectedText =
|
||||
!isEmpty && state.selection instanceof TextSelection
|
||||
? state.doc.cut(state.selection.from, state.selection.to).textContent
|
||||
: undefined;
|
||||
|
||||
const highlight = getMarksBetween(
|
||||
state.selection.from,
|
||||
state.selection.to,
|
||||
state
|
||||
).find(({ mark }) => mark.type.name === "highlight");
|
||||
|
||||
const issueSourcesChildren = recentIssueSources.length
|
||||
? recentIssueSources.map<MenuItem>((source) => ({
|
||||
name: "issue",
|
||||
label: `${source.account.name}/${source.name}`,
|
||||
icon: <PluginIcon id={source.service} />,
|
||||
attrs: {
|
||||
title: selectedText,
|
||||
source: JSON.stringify(source),
|
||||
},
|
||||
}))
|
||||
: undefined;
|
||||
|
||||
if (issueSourcesChildren) {
|
||||
issueSourcesChildren.push({
|
||||
name: "issue",
|
||||
label: `${t("Other")}…`,
|
||||
attrs: {
|
||||
title: selectedText,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return [
|
||||
{
|
||||
name: "placeholder",
|
||||
@@ -270,10 +301,10 @@ export default function formattingMenuItems(
|
||||
shortcut: `${metaDisplay}+⌥+I`,
|
||||
icon: <PlusIcon />,
|
||||
attrs: {
|
||||
title: state.doc.cut(state.selection.from, state.selection.to)
|
||||
.textContent,
|
||||
title: selectedText,
|
||||
},
|
||||
visible: !isCode && !isEmpty,
|
||||
visible: !isCode && !!selectedText,
|
||||
children: issueSourcesChildren,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
@@ -287,6 +287,8 @@ export const MentionPullRequest = observer((props: IssuePrProps) => {
|
||||
);
|
||||
});
|
||||
|
||||
export const MentionPlaceholder = () => <MentionLoading className="mention" />;
|
||||
|
||||
const MentionLoading = ({ className }: { className: string }) => {
|
||||
const { t } = useTranslation();
|
||||
|
||||
|
||||
@@ -16,11 +16,17 @@ import * as React from "react";
|
||||
import { Primitive } from "utility-types";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import env from "../../env";
|
||||
import { MentionType, UnfurlResourceType, UnfurlResponse } from "../../types";
|
||||
import {
|
||||
MentionPlaceholder,
|
||||
MentionType,
|
||||
UnfurlResourceType,
|
||||
UnfurlResponse,
|
||||
} from "../../types";
|
||||
import {
|
||||
MentionCollection,
|
||||
MentionDocument,
|
||||
MentionIssue,
|
||||
MentionPlaceholder as MentionPlaceholderComp,
|
||||
MentionPullRequest,
|
||||
MentionUser,
|
||||
} from "../components/Mentions";
|
||||
@@ -141,6 +147,8 @@ export default class Mention extends Node {
|
||||
onChangeUnfurl={this.handleChangeUnfurl(props)}
|
||||
/>
|
||||
);
|
||||
case MentionPlaceholder:
|
||||
return <MentionPlaceholderComp />;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -79,6 +79,8 @@ export enum MentionType {
|
||||
PullRequest = "pull_request",
|
||||
}
|
||||
|
||||
export const MentionPlaceholder = "mention_placeholder";
|
||||
|
||||
export type PublicEnv = {
|
||||
ROOT_SHARE_ID?: string;
|
||||
analytics: {
|
||||
|
||||
Reference in new issue
Block a user