1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

chore pkg and demo

This commit is contained in:
songjunxi
2023-10-22 10:52:46 +08:00
commit bccff40c0d
59 changed files with 12213 additions and 0 deletions

View File

View 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());
};

View 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;

View 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;
}
}