Files
notesnook/apps/mobile/app/components/sheets/editor-tabs/index.tsx

271 lines
7.3 KiB
TypeScript
Raw Normal View History

2023-12-21 10:14:53 +05:00
/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2024-03-14 10:43:25 +05:00
import { Note } from "@notesnook/core";
2024-09-23 15:18:44 +05:00
import { EVENTS } from "@notesnook/core";
2023-12-21 10:14:53 +05:00
import { useThemeColors } from "@notesnook/theme";
2024-03-14 10:43:25 +05:00
import React, { useEffect } from "react";
2024-04-22 15:55:10 +05:00
import { View } from "react-native";
import { FlatList } from "react-native-actions-sheet";
import Icon from "react-native-vector-icons/MaterialCommunityIcons";
2024-03-14 10:43:25 +05:00
import { db } from "../../../common/database";
2023-12-21 10:14:53 +05:00
import { useDBItem } from "../../../hooks/use-db-item";
2023-12-25 15:02:34 +05:00
import { useTabStore } from "../../../screens/editor/tiptap/use-tab-store";
import { editorController } from "../../../screens/editor/tiptap/utils";
2024-01-02 10:54:44 +05:00
import { eSendEvent, presentSheet } from "../../../services/event-manager";
import { eUnlockNote } from "../../../utils/events";
2023-12-21 10:14:53 +05:00
import { SIZE } from "../../../utils/size";
import { Button } from "../../ui/button";
import { IconButton } from "../../ui/icon-button";
2024-03-08 13:00:10 +05:00
import { Pressable } from "../../ui/pressable";
2023-12-21 10:14:53 +05:00
import Heading from "../../ui/typography/heading";
2023-12-25 15:02:34 +05:00
import Paragraph from "../../ui/typography/paragraph";
2024-07-27 10:19:43 +05:00
import { strings } from "@notesnook/intl";
2023-12-21 10:14:53 +05:00
type TabItem = {
id: number;
noteId?: string;
2023-12-29 10:47:23 +05:00
previewTab?: boolean;
2024-01-02 10:54:44 +05:00
locked?: boolean;
2024-03-14 10:43:25 +05:00
noteLocked?: boolean;
readonly?: boolean;
2024-04-22 15:55:10 +05:00
pinned?: boolean;
2023-12-21 10:14:53 +05:00
};
const TabItemComponent = (props: {
tab: TabItem;
isFocused: boolean;
close?: (ctx?: string | undefined) => void;
}) => {
const { colors } = useThemeColors();
2024-03-14 10:43:25 +05:00
const [item, update] = useDBItem(props.tab.noteId, "note");
useEffect(() => {
const syncCompletedSubscription = db.eventManager?.subscribe(
EVENTS.syncItemMerged,
(data: Note) => {
if (data.type === "note") {
const tabId = useTabStore.getState().getTabForNote(data.id);
if (tabId !== undefined && item?.title !== data.title) {
update();
}
}
}
);
return () => {
syncCompletedSubscription?.unsubscribe();
};
}, [update, item]);
2023-12-21 10:14:53 +05:00
return (
2024-03-08 13:00:10 +05:00
<Pressable
style={{
2023-12-21 10:14:53 +05:00
alignItems: "center",
justifyContent: "space-between",
flexDirection: "row",
paddingLeft: 12,
2024-03-20 06:42:17 +05:00
minHeight: 45
2023-12-21 10:14:53 +05:00
}}
type={props.isFocused ? "selected" : "transparent"}
onPress={() => {
if (!props.isFocused) {
useTabStore.getState().focusTab(props.tab.id);
2024-01-02 10:54:44 +05:00
if (props.tab.locked) {
eSendEvent(eUnlockNote);
}
if (!props.tab.noteId) {
setTimeout(() => {
editorController?.current?.commands?.focus(props.tab.id);
}, 300);
}
2023-12-21 10:14:53 +05:00
}
2024-03-26 08:57:04 +05:00
props.close?.();
2023-12-21 10:14:53 +05:00
}}
>
<View
style={{
flexDirection: "row",
alignItems: "center",
2024-01-02 10:54:44 +05:00
justifyContent: "flex-start",
2024-03-20 06:42:17 +05:00
gap: 10,
flexShrink: 1
2023-12-21 10:14:53 +05:00
}}
>
2024-03-14 10:43:25 +05:00
{props.tab.noteLocked ? (
<>
{props.tab.locked ? (
<Icon size={SIZE.md} name="lock" />
) : (
<Icon size={SIZE.md} name="lock-open-outline" />
)}
</>
) : null}
{props.tab.readonly ? <Icon size={SIZE.md} name="pencil-lock" /> : null}
2023-12-21 10:14:53 +05:00
<Paragraph
color={
props.isFocused
? colors.selected.paragraph
: colors.primary.paragraph
}
2023-12-29 10:47:23 +05:00
style={{
2024-03-26 11:05:08 +05:00
fontFamily: props.tab.previewTab
? "OpenSans-Italic"
: "OpenSans-Regular"
2023-12-29 10:47:23 +05:00
}}
2024-03-20 06:42:17 +05:00
numberOfLines={1}
2023-12-21 10:14:53 +05:00
size={SIZE.md}
>
2024-07-27 10:19:43 +05:00
{props.tab.noteId
? item?.title || strings.untitledNote()
: strings.newNote()}
2023-12-21 10:14:53 +05:00
</Paragraph>
</View>
2024-04-22 15:55:10 +05:00
<View
style={{
flexDirection: "row",
gap: 5,
paddingRight: 12
2023-12-21 10:14:53 +05:00
}}
2024-04-22 15:55:10 +05:00
>
<IconButton
name="pin"
size={SIZE.lg}
color={props.tab.pinned ? colors.primary.accent : colors.primary.icon}
onPress={() => {
useTabStore.getState().updateTab(props.tab.id, {
2024-05-14 15:35:49 +05:00
pinned: !props.tab.pinned
2024-04-22 15:55:10 +05:00
});
}}
top={0}
left={0}
right={20}
bottom={0}
/>
{!props.tab?.pinned ? (
<IconButton
name="close"
size={SIZE.lg}
color={colors.primary.icon}
onPress={() => {
const isLastTab = useTabStore.getState().tabs.length === 1;
useTabStore.getState().removeTab(props.tab.id);
// The last tab is not actually removed, it is just cleaned up.
if (isLastTab) {
editorController.current?.reset(props.tab.id, true, true);
props.close?.();
}
}}
top={0}
left={0}
right={20}
bottom={0}
/>
) : null}
</View>
2024-03-08 13:00:10 +05:00
</Pressable>
2023-12-21 10:14:53 +05:00
);
};
export default function EditorTabs({
close
}: {
close?: (ctx?: string | undefined) => void;
}) {
const [tabs, currentTab] = useTabStore((state) => [
state.tabs,
state.currentTab
]);
const renderTabItem = React.useCallback(
({ item }: { item: TabItem }) => {
return (
<TabItemComponent
key={item.id}
tab={item}
isFocused={item.id === currentTab}
close={close}
/>
);
},
[close, currentTab]
);
2023-12-21 10:14:53 +05:00
return (
<View
style={{
paddingHorizontal: 12,
gap: 12,
maxHeight: "100%"
2023-12-21 10:14:53 +05:00
}}
>
<View
style={{
flexDirection: "row",
justifyContent: "space-between",
width: "100%",
alignItems: "center"
}}
>
2024-07-27 10:19:43 +05:00
<Heading size={SIZE.lg}>{strings.tabs()}</Heading>
2023-12-21 10:14:53 +05:00
<Button
onPress={() => {
useTabStore.getState().newTab();
2023-12-25 15:02:34 +05:00
setTimeout(() => {
editorController?.current?.commands?.focus(
useTabStore.getState().currentTab
);
}, 500);
close?.();
2023-12-21 10:14:53 +05:00
}}
2024-07-27 10:19:43 +05:00
title={strings.newTab()}
2023-12-21 10:14:53 +05:00
icon="plus"
style={{
flexDirection: "row",
justifyContent: "flex-start",
borderRadius: 100,
height: 35
}}
iconSize={SIZE.lg}
/>
</View>
2024-04-22 15:55:10 +05:00
<FlatList
windowSize={3}
data={tabs.sort((t1, t2) => {
if (t1.pinned && t2.pinned) return 0;
if (t1.pinned) return -1;
if (t2.pinned) return 1;
return 0;
})}
renderItem={renderTabItem}
/>
2023-12-21 10:14:53 +05:00
</View>
);
}
EditorTabs.present = () => {
presentSheet({
component: (ref, close, update) => <EditorTabs close={close} />
});
};