2022-08-31 06:33:37 +05:00
|
|
|
/*
|
|
|
|
|
This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
|
|
2023-01-16 13:44:52 +05:00
|
|
|
Copyright (C) 2023 Streetwriters (Private) Limited
|
2022-08-31 06:33:37 +05:00
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
|
*/
|
2022-08-30 16:13:11 +05:00
|
|
|
|
2022-08-30 11:05:10 +05:00
|
|
|
import { ToolbarGroupDefinition } from "@notesnook/editor";
|
|
|
|
|
import { Editor } from "@notesnook/editor";
|
2022-06-23 19:14:55 +05:00
|
|
|
import { Dispatch, MutableRefObject, RefObject, SetStateAction } from "react";
|
|
|
|
|
import { useEditorController } from "../hooks/useEditorController";
|
|
|
|
|
|
|
|
|
|
export type SafeAreaType = {
|
|
|
|
|
top: number;
|
|
|
|
|
left: number;
|
|
|
|
|
bottom: number;
|
|
|
|
|
right: number;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type Settings = {
|
|
|
|
|
readonly: boolean;
|
|
|
|
|
fullscreen: boolean;
|
|
|
|
|
deviceMode: "mobile" | "smallTablet" | "tablet";
|
|
|
|
|
premium: boolean;
|
2022-06-27 18:39:59 +05:00
|
|
|
tools: ToolbarGroupDefinition[];
|
2022-06-30 09:47:04 +05:00
|
|
|
noToolbar?: boolean;
|
|
|
|
|
noHeader?: boolean;
|
2022-07-18 16:27:11 +05:00
|
|
|
keyboardShown?: boolean;
|
2022-08-06 23:39:07 +05:00
|
|
|
doubleSpacedLines?: boolean;
|
2023-01-09 13:51:22 +05:00
|
|
|
corsProxy: string;
|
2022-06-23 19:14:55 +05:00
|
|
|
};
|
|
|
|
|
|
2022-08-30 11:05:10 +05:00
|
|
|
/* eslint-disable no-var */
|
2022-06-23 19:14:55 +05:00
|
|
|
declare global {
|
2022-08-30 11:05:10 +05:00
|
|
|
var statusBar: React.MutableRefObject<{
|
|
|
|
|
set: React.Dispatch<
|
|
|
|
|
React.SetStateAction<{
|
|
|
|
|
date: string;
|
|
|
|
|
saved: string;
|
|
|
|
|
}>
|
|
|
|
|
>;
|
|
|
|
|
}>;
|
2022-06-30 09:47:04 +05:00
|
|
|
var readonly: boolean;
|
|
|
|
|
var noToolbar: boolean;
|
|
|
|
|
var noHeader: boolean;
|
2022-06-23 19:14:55 +05:00
|
|
|
/**
|
|
|
|
|
* Id of current session
|
|
|
|
|
*/
|
|
|
|
|
var sessionId: string;
|
|
|
|
|
/**
|
|
|
|
|
* Current tiptap instance
|
|
|
|
|
*/
|
|
|
|
|
var editor: Editor | null;
|
|
|
|
|
/**
|
|
|
|
|
* Current editor controller
|
|
|
|
|
*/
|
|
|
|
|
var editorController: ReturnType<typeof useEditorController>;
|
|
|
|
|
|
|
|
|
|
var settingsController: {
|
|
|
|
|
update: (settings: Settings) => void;
|
|
|
|
|
previous: Settings;
|
|
|
|
|
set?: Dispatch<SetStateAction<Settings>>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var premiumController: {
|
|
|
|
|
update: (premium: boolean) => void;
|
|
|
|
|
previous: boolean;
|
|
|
|
|
set?: Dispatch<SetStateAction<boolean>>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var safeAreaController: {
|
|
|
|
|
update: (insets: SafeAreaType) => void;
|
|
|
|
|
reset: () => void;
|
|
|
|
|
previous: SafeAreaType;
|
|
|
|
|
set?: Dispatch<
|
|
|
|
|
SetStateAction<{
|
|
|
|
|
top: number;
|
|
|
|
|
bottom: number;
|
|
|
|
|
left: number;
|
|
|
|
|
right: number;
|
|
|
|
|
}>
|
|
|
|
|
>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var editorTitle: RefObject<HTMLInputElement>;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Global ref to manage tags in editor.
|
|
|
|
|
*/
|
|
|
|
|
var editorTags: MutableRefObject<{
|
|
|
|
|
setTags: React.Dispatch<
|
|
|
|
|
React.SetStateAction<{ title: string; alias: string }[]>
|
|
|
|
|
>;
|
|
|
|
|
}>;
|
|
|
|
|
|
2022-08-31 13:03:22 +05:00
|
|
|
function logger(type: "info" | "warn" | "error", ...logs: unknown[]): void;
|
2022-06-23 19:14:55 +05:00
|
|
|
/**
|
|
|
|
|
* Function to post message to react native
|
|
|
|
|
* @param type
|
|
|
|
|
* @param value
|
|
|
|
|
*/
|
|
|
|
|
|
2022-08-30 11:05:10 +05:00
|
|
|
function post<T extends keyof typeof EventTypes>(
|
|
|
|
|
type: typeof EventTypes[T],
|
|
|
|
|
value?: unknown
|
|
|
|
|
): void;
|
2022-06-23 19:14:55 +05:00
|
|
|
interface Window {
|
|
|
|
|
/**
|
|
|
|
|
* React Native WebView
|
|
|
|
|
*/
|
|
|
|
|
ReactNativeWebView: {
|
|
|
|
|
postMessage: (data: string) => void;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-30 11:05:10 +05:00
|
|
|
/* eslint-enable no-var */
|
2022-06-23 19:14:55 +05:00
|
|
|
|
|
|
|
|
export const EventTypes = {
|
|
|
|
|
selection: "editor-event:selection",
|
|
|
|
|
content: "editor-event:content",
|
|
|
|
|
title: "editor-event:title",
|
|
|
|
|
scroll: "editor-event:scroll",
|
|
|
|
|
history: "editor-event:history",
|
|
|
|
|
newtag: "editor-event:newtag",
|
|
|
|
|
tag: "editor-event:tag",
|
|
|
|
|
filepicker: "editor-event:picker",
|
|
|
|
|
download: "editor-event:download-attachment",
|
|
|
|
|
logger: "native:logger",
|
|
|
|
|
back: "editor-event:back",
|
|
|
|
|
pro: "editor-event:pro",
|
|
|
|
|
monograph: "editor-event:monograph",
|
|
|
|
|
properties: "editor-event:properties",
|
2022-09-16 12:10:06 +05:00
|
|
|
fullscreen: "editor-event:fullscreen",
|
2022-12-05 17:54:19 +05:00
|
|
|
link: "editor-event:link",
|
2022-12-16 22:28:30 +05:00
|
|
|
contentchange: "editor-event:content-change",
|
|
|
|
|
reminders: "editor-event:reminders"
|
2022-08-30 11:05:10 +05:00
|
|
|
} as const;
|
2022-06-23 19:14:55 +05:00
|
|
|
|
|
|
|
|
export function isReactNative(): boolean {
|
|
|
|
|
return !!window.ReactNativeWebView;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 17:54:19 +05:00
|
|
|
export function logger(
|
|
|
|
|
type: "info" | "warn" | "error",
|
|
|
|
|
...logs: unknown[]
|
|
|
|
|
): void {
|
2022-08-30 11:05:10 +05:00
|
|
|
const logString = logs
|
2022-06-23 19:14:55 +05:00
|
|
|
.map((log) => {
|
|
|
|
|
return typeof log !== "string" ? JSON.stringify(log) : log;
|
|
|
|
|
})
|
|
|
|
|
.join(" ");
|
|
|
|
|
|
|
|
|
|
post(EventTypes.logger, `[${type}]: ` + logString);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 11:05:10 +05:00
|
|
|
export function post<T extends keyof typeof EventTypes>(
|
|
|
|
|
type: typeof EventTypes[T],
|
|
|
|
|
value?: unknown
|
2022-12-05 17:54:19 +05:00
|
|
|
): void {
|
2022-06-23 19:14:55 +05:00
|
|
|
if (isReactNative()) {
|
|
|
|
|
window.ReactNativeWebView.postMessage(
|
|
|
|
|
JSON.stringify({
|
2022-08-30 11:05:10 +05:00
|
|
|
type,
|
2022-06-23 19:14:55 +05:00
|
|
|
value: value,
|
2022-08-30 11:05:10 +05:00
|
|
|
sessionId: globalThis.sessionId
|
2022-06-23 19:14:55 +05:00
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
console.log(type, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-30 11:05:10 +05:00
|
|
|
|
2022-06-23 19:14:55 +05:00
|
|
|
globalThis.logger = logger;
|
|
|
|
|
globalThis.post = post;
|