feat(content-manager): revert prismjs logic

This commit is contained in:
Simone Taeggi 2024-12-02 16:23:56 +01:00
parent b9b399cfa3
commit fee8edab8d
4 changed files with 99 additions and 3 deletions

View File

@ -22,6 +22,7 @@ import { getTranslation } from '../../../../../utils/translations';
import { type BlocksStore, useBlocksEditorContext } from './BlocksEditor'; import { type BlocksStore, useBlocksEditorContext } from './BlocksEditor';
import { useConversionModal } from './BlocksToolbar'; import { useConversionModal } from './BlocksToolbar';
import { type ModifiersStore } from './Modifiers'; import { type ModifiersStore } from './Modifiers';
import { decorateCode } from './utils/decorateCode';
import { getEntries, isLinkNode, isListNode } from './utils/types'; import { getEntries, isLinkNode, isListNode } from './utils/types';
const StyledEditable = styled(Editable)<{ isExpandedMode: boolean }>` const StyledEditable = styled(Editable)<{ isExpandedMode: boolean }>`
@ -299,7 +300,11 @@ const baseRenderLeaf = (props: RenderLeafProps, modifiers: ModifiersStore) => {
return currentChildren; return currentChildren;
}, props.children); }, props.children);
return <span {...props.attributes}>{wrappedChildren}</span>; return (
<span {...props.attributes} className={props.leaf.className}>
{wrappedChildren}
</span>
);
}; };
type BaseRenderElementProps = Direction & { type BaseRenderElementProps = Direction & {
@ -591,6 +596,7 @@ const BlocksContent = ({ placeholder, ariaLabelId }: BlocksContentProps) => {
readOnly={disabled} readOnly={disabled}
placeholder={placeholder} placeholder={placeholder}
isExpandedMode={isExpandedMode} isExpandedMode={isExpandedMode}
decorate={decorateCode}
renderElement={renderElement} renderElement={renderElement}
renderLeaf={renderLeaf} renderLeaf={renderLeaf}
onKeyDown={handleKeyDown} onKeyDown={handleKeyDown}

View File

@ -1,214 +1,267 @@
export const codeLanguages: { value: string; label: string }[] = [ export const codeLanguages: { value: string; label: string; decorate?: string }[] = [
{ {
value: 'asm', value: 'asm',
label: 'Assembly', label: 'Assembly',
decorate: 'asmatmel',
}, },
{ {
value: 'bash', value: 'bash',
label: 'Bash', label: 'Bash',
decorate: 'bash',
}, },
{ {
value: 'c', value: 'c',
label: 'C', label: 'C',
decorate: 'c',
}, },
{ {
value: 'clojure', value: 'clojure',
label: 'Clojure', label: 'Clojure',
decorate: 'clojure',
}, },
{ {
value: 'cobol', value: 'cobol',
label: 'COBOL', label: 'COBOL',
decorate: 'cobol',
}, },
{ {
value: 'cpp', value: 'cpp',
label: 'C++', label: 'C++',
decorate: 'cpp',
}, },
{ {
value: 'csharp', value: 'csharp',
label: 'C#', label: 'C#',
decorate: 'csharp',
}, },
{ {
value: 'css', value: 'css',
label: 'CSS', label: 'CSS',
decorate: 'css',
}, },
{ {
value: 'dart', value: 'dart',
label: 'Dart', label: 'Dart',
decorate: 'dart',
}, },
{ {
value: 'dockerfile', value: 'dockerfile',
label: 'Dockerfile', label: 'Dockerfile',
decorate: 'docker',
}, },
{ {
value: 'elixir', value: 'elixir',
label: 'Elixir', label: 'Elixir',
decorate: 'elixir',
}, },
{ {
value: 'erlang', value: 'erlang',
label: 'Erlang', label: 'Erlang',
decorate: 'erlang',
}, },
{ {
value: 'fortran', value: 'fortran',
label: 'Fortran', label: 'Fortran',
decorate: 'fortran',
}, },
{ {
value: 'fsharp', value: 'fsharp',
label: 'F#', label: 'F#',
decorate: 'fsharp',
}, },
{ {
value: 'go', value: 'go',
label: 'Go', label: 'Go',
decorate: 'go',
}, },
{ {
value: 'graphql', value: 'graphql',
label: 'GraphQL', label: 'GraphQL',
decorate: 'graphql',
}, },
{ {
value: 'groovy', value: 'groovy',
label: 'Groovy', label: 'Groovy',
decorate: 'groovy',
}, },
{ {
value: 'haskell', value: 'haskell',
label: 'Haskell', label: 'Haskell',
decorate: 'haskell',
}, },
{ {
value: 'haxe', value: 'haxe',
label: 'Haxe', label: 'Haxe',
decorate: 'haxe',
}, },
{ {
value: 'html', value: 'html',
label: 'HTML', label: 'HTML',
decorate: 'html',
}, },
{ {
value: 'ini', value: 'ini',
label: 'INI', label: 'INI',
decorate: 'ini',
}, },
{ {
value: 'java', value: 'java',
label: 'Java', label: 'Java',
decorate: 'java',
}, },
{ {
value: 'javascript', value: 'javascript',
label: 'JavaScript', label: 'JavaScript',
decorate: 'javascript',
}, },
{ {
value: 'jsx', value: 'jsx',
label: 'JavaScript (React)', label: 'JavaScript (React)',
decorate: 'jsx',
}, },
{ {
value: 'json', value: 'json',
label: 'JSON', label: 'JSON',
decorate: 'json',
}, },
{ {
value: 'julia', value: 'julia',
label: 'Julia', label: 'Julia',
decorate: 'julia',
}, },
{ {
value: 'kotlin', value: 'kotlin',
label: 'Kotlin', label: 'Kotlin',
decorate: 'kotlin',
}, },
{ {
value: 'latex', value: 'latex',
label: 'LaTeX', label: 'LaTeX',
decorate: 'latex',
}, },
{ {
value: 'lua', value: 'lua',
label: 'Lua', label: 'Lua',
decorate: 'lua',
}, },
{ {
value: 'markdown', value: 'markdown',
label: 'Markdown', label: 'Markdown',
decorate: 'markdown',
}, },
{ {
value: 'matlab', value: 'matlab',
label: 'MATLAB', label: 'MATLAB',
decorate: 'matlab',
}, },
{ {
value: 'makefile', value: 'makefile',
label: 'Makefile', label: 'Makefile',
decorate: 'makefile',
}, },
{ {
value: 'objectivec', value: 'objectivec',
label: 'Objective-C', label: 'Objective-C',
decorate: 'objectivec',
}, },
{ {
value: 'perl', value: 'perl',
label: 'Perl', label: 'Perl',
decorate: 'perl',
}, },
{ {
value: 'php', value: 'php',
label: 'PHP', label: 'PHP',
decorate: 'php',
}, },
{ {
value: 'plaintext', value: 'plaintext',
label: 'Plain text', label: 'Plain text',
decorate: 'plaintext',
}, },
{ {
value: 'powershell', value: 'powershell',
label: 'PowerShell', label: 'PowerShell',
decorate: 'powershell',
}, },
{ {
value: 'python', value: 'python',
label: 'Python', label: 'Python',
decorate: 'python',
}, },
{ {
value: 'r', value: 'r',
label: 'R', label: 'R',
decorate: 'r',
}, },
{ {
value: 'ruby', value: 'ruby',
label: 'Ruby', label: 'Ruby',
decorate: 'ruby',
}, },
{ {
value: 'rust', value: 'rust',
label: 'Rust', label: 'Rust',
decorate: 'rust',
}, },
{ {
value: 'sas', value: 'sas',
label: 'SAS', label: 'SAS',
decorate: 'sas',
}, },
{ {
value: 'scala', value: 'scala',
label: 'Scala', label: 'Scala',
decorate: 'scala',
}, },
{ {
value: 'scheme', value: 'scheme',
label: 'Scheme', label: 'Scheme',
decorate: 'scheme',
}, },
{ {
value: 'shell', value: 'shell',
label: 'Shell', label: 'Shell',
decorate: 'shell',
}, },
{ {
value: 'sql', value: 'sql',
label: 'SQL', label: 'SQL',
decorate: 'sql',
}, },
{ {
value: 'stata', value: 'stata',
label: 'Stata', label: 'Stata',
decorate: 'stata',
}, },
{ {
value: 'swift', value: 'swift',
label: 'Swift', label: 'Swift',
decorate: 'swift',
}, },
{ {
value: 'typescript', value: 'typescript',
label: 'TypeScript', label: 'TypeScript',
decorate: 'ts',
}, },
{ {
value: 'tsx', value: 'tsx',
label: 'TypeScript (React)', label: 'TypeScript (React)',
decorate: 'tsx',
}, },
{ {
value: 'vbnet', value: 'vbnet',
label: 'VB.NET', label: 'VB.NET',
decorate: 'vbnet',
}, },
{ {
value: 'xml', value: 'xml',
label: 'XML', label: 'XML',
decorate: 'xml',
}, },
{ {
value: 'yaml', value: 'yaml',
label: 'YAML', label: 'YAML',
decorate: 'yml',
}, },
]; ];

View File

@ -0,0 +1,37 @@
import { BaseRange, Element, Node, NodeEntry } from 'slate';
import { codeLanguages } from './constants';
import { Prism } from './prism';
type BaseRangeCustom = BaseRange & { className: string };
export const decorateCode = ([node, path]: NodeEntry) => {
const ranges: BaseRangeCustom[] = [];
// make sure it is an Slate Element
if (!Element.isElement(node) || node.type !== 'code') return ranges;
// transform the Element into a string
const text = Node.string(node);
const decorateKey = codeLanguages.find((lang) => lang.value === node.language)?.decorate;
const selectedLanguage = Prism.languages[decorateKey || 'plaintext'];
// create "tokens" with "prismjs" and put them in "ranges"
const tokens = Prism.tokenize(text, selectedLanguage);
let start = 0;
for (const token of tokens) {
const length = token.length;
const end = start + length;
if (typeof token !== 'string') {
ranges.push({
anchor: { path, offset: start },
focus: { path, offset: end },
className: `token ${token.type}`,
});
}
start = end;
}
// these will be found in "renderLeaf" in "leaf" and their "className" will be applied
return ranges;
};

View File

@ -19,7 +19,7 @@ const _self =
* @namespace * @namespace
* @public * @public
*/ */
const Prism = (function (_self) { export const Prism = (function (_self) {
// Private helper vars // Private helper vars
var lang = /(?:^|\s)lang(?:uage)?-([\w-]+)(?=\s|$)/i; var lang = /(?:^|\s)lang(?:uage)?-([\w-]+)(?=\s|$)/i;
var uniqueId = 0; var uniqueId = 0;