Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lexical-playground] Reuse guid method #7289

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions packages/lexical-playground/src/commenting/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import type {LexicalEditor} from 'lexical';

import {createUID} from '@lexical/utils';
import {Provider, TOGGLE_CONNECT_COMMAND} from '@lexical/yjs';
import {COMMAND_PRIORITY_LOW} from 'lexical';
import {useEffect, useState} from 'react';
Expand Down Expand Up @@ -37,13 +38,6 @@ export type Thread = {

export type Comments = Array<Thread | Comment>;

function createUID(): string {
return Math.random()
.toString(36)
.replace(/[^a-z]+/g, '')
.substring(0, 5);
}

export function createComment(
content: string,
author: string,
Expand Down
9 changes: 1 addition & 8 deletions packages/lexical-playground/src/nodes/PollNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import type {JSX} from 'react';

import {makeStateWrapper} from '@lexical/utils';
import {createUID, makeStateWrapper} from '@lexical/utils';
import {
createState,
DecoratorNode,
Expand All @@ -31,13 +31,6 @@ export type Option = Readonly<{

const PollComponent = React.lazy(() => import('./PollComponent'));

function createUID(): string {
return Math.random()
.toString(36)
.replace(/[^a-z]+/g, '')
.substring(0, 5);
}

export function createPollOption(text = ''): Option {
return {
text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {JSX} from 'react';

import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {$isAtNodeEnd} from '@lexical/selection';
import {mergeRegister} from '@lexical/utils';
import {createUID, mergeRegister} from '@lexical/utils';
import {
$addUpdateTag,
$createTextNode,
Expand Down Expand Up @@ -48,10 +48,7 @@ type SearchPromise = {
promise: Promise<null | string>;
};

export const uuid = Math.random()
.toString(36)
.replace(/[^a-z]+/g, '')
.substring(0, 5);
export const uuid = createUID();

// TODO lookup should be custom
function $search(selection: null | BaseSelection): [boolean, string] {
Expand Down
1 change: 1 addition & 0 deletions packages/lexical-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export {default as positionNodeOnRange} from './positionNodeOnRange';
export {default as selectionAlwaysOnDisplay} from './selectionAlwaysOnDisplay';
export {
$splitNode,
createUID,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to export this from both lexical and @lexical/utils? It's already in lexical so maybe just import it from there rather than include its implementation twice?

isBlockDomNode,
isHTMLAnchorElement,
isHTMLElement,
Expand Down
1 change: 1 addition & 0 deletions packages/lexical/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ export {
$setCompositionKey,
$setSelection,
$splitNode,
createUID,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add some API docs to this function if we're going to export it. I wouldn't say it's a good UID implementation, just a quick hack and only has a key space of <12M with up to 5 letters (there's no guarantee that there are any characters because it's not a good algorithm, if the representation happened to be all digits it could even return an empty string). For things that don't have to support super old browsers it might make sense to just use randomUUID when possible (in secure contexts) - otherwise I would recommend that we fix the implementation so it's at least guaranteed to produce a 5 letter string, or adopt something like the nanoid algorithm.

getDOMOwnerDocument,
getDOMSelection,
getDOMSelectionFromTarget,
Expand Down
Loading