chore pkg and demo
This commit is contained in:
0
packages/core/src/lib/constants.ts
Normal file
0
packages/core/src/lib/constants.ts
Normal file
21
packages/core/src/lib/editor.ts
Normal file
21
packages/core/src/lib/editor.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { Editor } from "@tiptap/core";
|
||||
|
||||
export const getPrevText = (
|
||||
editor: Editor,
|
||||
{
|
||||
chars,
|
||||
offset = 0,
|
||||
}: {
|
||||
chars: number;
|
||||
offset?: number;
|
||||
}
|
||||
) => {
|
||||
// for now, we're using textBetween for now until we can figure out a way to stream markdown text
|
||||
// with proper formatting: https://github.com/yesmore/inke/discussions/7
|
||||
return editor.state.doc.textBetween(
|
||||
Math.max(0, editor.state.selection.from - chars),
|
||||
editor.state.selection.from - offset,
|
||||
"\n"
|
||||
);
|
||||
// complete(editor.storage.markdown.getMarkdown());
|
||||
};
|
||||
27
packages/core/src/lib/hooks/use-local-storage.ts
Normal file
27
packages/core/src/lib/hooks/use-local-storage.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
const useLocalStorage = <T>(
|
||||
key: string,
|
||||
initialValue: T
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
): [T, (value: T) => void] => {
|
||||
const [storedValue, setStoredValue] = useState(initialValue);
|
||||
|
||||
useEffect(() => {
|
||||
// Retrieve from localStorage
|
||||
const item = window.localStorage.getItem(key);
|
||||
if (item) {
|
||||
setStoredValue(JSON.parse(item));
|
||||
}
|
||||
}, [key]);
|
||||
|
||||
const setValue = (value: T) => {
|
||||
// Save state
|
||||
setStoredValue(value);
|
||||
// Save to localStorage
|
||||
window.localStorage.setItem(key, JSON.stringify(value));
|
||||
};
|
||||
return [storedValue, setValue];
|
||||
};
|
||||
|
||||
export default useLocalStorage;
|
||||
26
packages/core/src/lib/utils.ts
Normal file
26
packages/core/src/lib/utils.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { clsx, type ClassValue } from "clsx";
|
||||
import { twMerge } from "tailwind-merge";
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs));
|
||||
}
|
||||
|
||||
export function isValidUrl(url: string) {
|
||||
try {
|
||||
new URL(url);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function getUrlFromString(str: string) {
|
||||
if (isValidUrl(str)) return str;
|
||||
try {
|
||||
if (str.includes(".") && !str.includes(" ")) {
|
||||
return new URL(`https://${str}`).toString();
|
||||
}
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user