-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: language selector for code block
- Loading branch information
Showing
12 changed files
with
219 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import { $createCodeNode } from '@lexical/code' | ||
import { | ||
$isListNode, | ||
INSERT_ORDERED_LIST_COMMAND, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from 'react' | ||
import * as Select from '@radix-ui/react-select' | ||
// import { useEmitterValues, usePublisher } from '../../../system' | ||
import { SelectItem, SelectTrigger, SelectContent } from './SelectPieces' | ||
import { useEmitterValues } from '..' | ||
import { $getNodeByKey } from 'lexical' | ||
import { CodeBlockEditorType } from '../../types/ActiveEditorType' | ||
import { CodeBlockNode } from '../../nodes/CodeBlock' | ||
|
||
export function CodeBlockLanguageSelect() { | ||
const [activeEditorType, activeEditor] = useEmitterValues('activeEditorType', 'activeEditor') | ||
|
||
console.log(activeEditorType) | ||
const nodeLanguage = React.useMemo(() => { | ||
let language!: string | ||
activeEditor!.getEditorState().read(() => { | ||
const node = $getNodeByKey((activeEditorType as CodeBlockEditorType).nodeKey) as CodeBlockNode | ||
language = node.getLanguage() | ||
}) | ||
return language | ||
}, [activeEditor, activeEditorType]) | ||
|
||
return ( | ||
<Select.Root | ||
value={nodeLanguage} | ||
onValueChange={(language) => { | ||
activeEditor!.update(() => { | ||
const node = $getNodeByKey((activeEditorType as CodeBlockEditorType).nodeKey) as CodeBlockNode | ||
node.setLanguage(language) | ||
setTimeout(() => { | ||
node.select() | ||
}, 80) | ||
}) | ||
}} | ||
> | ||
<SelectTrigger placeholder="Language" /> | ||
<SelectContent> | ||
<SelectItem value="js">JavaScript</SelectItem> | ||
<SelectItem value="ts">TypeScript</SelectItem> | ||
<SelectItem value="css">CSS</SelectItem> | ||
</SelectContent> | ||
</Select.Root> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from 'react' | ||
import * as Select from '@radix-ui/react-select' | ||
import { ReactComponent as DropDownIcon } from './icons/arrow_drop_down.svg' | ||
import { ChevronDownIcon, ChevronUpIcon } from '@radix-ui/react-icons' | ||
import classnames from 'classnames' | ||
|
||
export const SelectItem = React.forwardRef<HTMLDivElement | null, { className?: string; children: React.ReactNode; value: string }>( | ||
({ children, className, ...props }, forwardedRef) => { | ||
return ( | ||
<Select.Item | ||
{...props} | ||
ref={forwardedRef} | ||
className={classnames( | ||
className, | ||
`cursor-default px-4 py-2 data-[highlighted]:bg-primary-200 data-[state=checked]:bg-primary-300 data-[highlighted]:outline-0 flex` | ||
)} | ||
> | ||
<Select.ItemText>{children}</Select.ItemText> | ||
</Select.Item> | ||
) | ||
} | ||
) | ||
export function SelectTrigger({ placeholder }: { placeholder: string }) { | ||
return ( | ||
<Select.Trigger | ||
aria-label={placeholder} | ||
className="group flex w-36 flex-row items-center rounded-md border-0 bg-transparent p-2 pl-4 pr-2 font-sans text-base hover:bg-primary-100 data-[state=open]:rounded-b-none data-[state=open]:bg-primary-100" | ||
> | ||
<Select.Value placeholder={placeholder} /> | ||
<Select.Icon className="ml-auto group-data-[state=open]:text-primary-900 [&_svg]:block"> | ||
<DropDownIcon /> | ||
</Select.Icon> | ||
</Select.Trigger> | ||
) | ||
} | ||
|
||
export function SelectContent({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<Select.Portal className="font-sans"> | ||
<Select.Content className="w-36 rounded-b-md bg-primary-100" onCloseAutoFocus={(e) => e.preventDefault()} position="popper"> | ||
<Select.ScrollUpButton className=""> | ||
<ChevronUpIcon /> | ||
</Select.ScrollUpButton> | ||
<Select.Viewport className="">{children}</Select.Viewport> | ||
<Select.ScrollDownButton className=""> | ||
<ChevronDownIcon /> | ||
</Select.ScrollDownButton> | ||
</Select.Content> | ||
</Select.Portal> | ||
) | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.