|
| 1 | +## How to Connect the HTML Extensions in the Editor |
| 2 | + |
| 3 | +To integrate the HTML extensions in your editor, you will use the specified versions of the necessary packages. Here’s a detailed guide: |
| 4 | + |
| 5 | +First to integrate this extension, you need to use the following versions of the packages: |
| 6 | + |
| 7 | +- @gravity-ui/markdown-editor version 13.4.0 or higher |
| 8 | +- @diplodoc/html-extension version 1.2.7 or higher |
| 9 | + |
| 10 | +## Usage |
| 11 | + |
| 12 | +### 1. Install the Packages |
| 13 | + |
| 14 | +First, ensure that you have all the necessary packages installed. You can use npm or yarn to add them to your project: |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install @gravity-ui/markdown-editor@^13.4.0 |
| 18 | +npm install @diplodoc/html-extension@^1.2.7 |
| 19 | +``` |
| 20 | + |
| 21 | + |
| 22 | +### 2. Integrate the Plugin in the Transformer |
| 23 | + |
| 24 | +You will need to import and configure the transformers in your editor setup. Below is an example of how to do this: |
| 25 | + |
| 26 | +```typescript |
| 27 | +import { transform as transformHTML } from '@diplodoc/html-extension'; |
| 28 | + |
| 29 | +// Define the runtime marker constant |
| 30 | +const HTML_RUNTIME = 'html-runtime'; |
| 31 | + |
| 32 | +// Configure the plugins in your transformer setup |
| 33 | +const plugins: PluginWithParams[] = [ |
| 34 | + // Add HTML transformer plugin |
| 35 | + transformHTML({ bundle: false, runtimeJsPath: HTML_RUNTIME }), |
| 36 | + |
| 37 | + // Add other plugins as needed |
| 38 | +]; |
| 39 | +``` |
| 40 | + |
| 41 | +### 3. Integrate into Editor |
| 42 | + |
| 43 | +Ensure that these plugins are integrated into your editor's initialization or configuration file. Below is a simplified example to illustrate how you might set them up with a markdown editor: |
| 44 | + |
| 45 | +```ts |
| 46 | +const editor = new MarkdownEditor({ |
| 47 | + // Editor configuration |
| 48 | + plugins, |
| 49 | + // Other configurations |
| 50 | +}); |
| 51 | +``` |
| 52 | + |
| 53 | +### 4. Adding a Higher-Order Component (HOC) in Static Render to Load Runtime and Apply Styling Hooks |
| 54 | + |
| 55 | +```ts |
| 56 | +import {useEffect} from 'react'; |
| 57 | + |
| 58 | +import {YfmHtml} from '@gravity-ui/markdown-editor/view/components/YfmHtml'; |
| 59 | +import type {WithYfmHtmlBlockProps} from '@gravity-ui/markdown-editor/view/hocs/withYfmHtml'; |
| 60 | +import {withYfmHtmlBlock} from '@gravity-ui/markdown-editor/view/hocs/withYfmHtml'; |
| 61 | +import {useEffect, useState} from 'react'; |
| 62 | + |
| 63 | +import {IHTMLIFrameElementConfig} from '@diplodoc/html-extension/runtime'; |
| 64 | +import {getYfmHtmlBlockCssVariables} from '@gravity-ui/markdown-editor/view/hocs/withYfmHtml/utils'; |
| 65 | +import {useThemeValue} from '@gravity-ui/uikit'; |
| 66 | + |
| 67 | +// HOC |
| 68 | +export const YFM_HTML_BLOCK_RUNTIME = 'yfm-html-block'; |
| 69 | + |
| 70 | +const YfmStaticView = withYfmHtmlBlock({runtime: YFM_HTML_BLOCK_RUNTIME})(YfmHtml); |
| 71 | + |
| 72 | +const variablesMapping = { |
| 73 | + colorBackground: '--g-color-base-background', |
| 74 | + colorTextPrimary: '--g-color-text-primary', |
| 75 | + colorTextSecondary: '--g-color-text-secondary', |
| 76 | + fontFamily: '--g-font-family-sans', |
| 77 | + fontSize: '--g-text-body-1-font-size', |
| 78 | +}; |
| 79 | + |
| 80 | +// hooks |
| 81 | +export const useYfmHtmlBlockStyles = () => { |
| 82 | + const theme = useThemeValue(); |
| 83 | + const [config, setConfig] = useState<IHTMLIFrameElementConfig | undefined>(); |
| 84 | + |
| 85 | + useEffect(() => { |
| 86 | + const bodyStyles = window.getComputedStyle(document.body); |
| 87 | + |
| 88 | + const styles = Object.entries(variablesMapping).reduce( |
| 89 | + (acc, [key, cssVariable]) => { |
| 90 | + acc[key] = bodyStyles.getPropertyValue(cssVariable); |
| 91 | + return acc; |
| 92 | + }, |
| 93 | + {} as Record<string, string>, |
| 94 | + ); |
| 95 | + |
| 96 | + setConfig({ |
| 97 | + styles: getYfmHtmlBlockCssVariables(styles), |
| 98 | + classNames: [theme], |
| 99 | + resizePadding: 50, |
| 100 | + resizeDelay: 100, |
| 101 | + }); |
| 102 | + }, [theme]); |
| 103 | + |
| 104 | + return config; |
| 105 | +}; |
| 106 | + |
| 107 | +// render |
| 108 | +const HtmlRenderer = React.forwardRef<HTMLDivElement, HtmlRendererProps>((props, ref) => { |
| 109 | + // ... |
| 110 | + const theme = useThemeType(); // your hook for get theme |
| 111 | + |
| 112 | + const yfmHtmlBlockConfig = useYfmHtmlBlockStyles(); |
| 113 | + () => ({ |
| 114 | + theme, |
| 115 | + zoom: {showMenu: true, bindKeys: true, resetOnBlur: true}, |
| 116 | + }), |
| 117 | + [theme], |
| 118 | + ); |
| 119 | + |
| 120 | + // ... |
| 121 | + return ( |
| 122 | + <div> |
| 123 | + <YfmStaticView |
| 124 | + html={html} |
| 125 | + meta={meta} |
| 126 | + ref={elementRef} |
| 127 | + yfmHtmlBlockConfig={yfmHtmlBlockConfig} |
| 128 | + /> |
| 129 | + {props.children} |
| 130 | + </div> |
| 131 | + ); |
| 132 | +}); |
| 133 | + |
| 134 | +``` |
| 135 | + |
| 136 | + |
| 137 | +### 5. Integrate the WYSiWYG Extension |
| 138 | + |
| 139 | +```ts |
| 140 | +import {YfmHtmlBlock} from '@gravity-ui/markdown-editor/extensions/yfm/YfmHtmlBlock'; |
| 141 | + |
| 142 | +// ... |
| 143 | +builder.use(YfmHtmlBlock, { useConfig: useYfmHtmlBlockStyles }); |
| 144 | + |
| 145 | +``` |
| 146 | + |
| 147 | +### 5. Add Buttons to the Toolbar |
| 148 | + |
| 149 | +```ts |
| 150 | +import { |
| 151 | + mYfmHtmlBlockButton, |
| 152 | +} from '@gravity-ui/markdown-editor/bundle/config/markup'; |
| 153 | + |
| 154 | +import { |
| 155 | + wYfmHtmlBlockItemData, |
| 156 | +} from '@gravity-ui/markdown-editor'; |
| 157 | + |
| 158 | +// add to useMarkdownEditor |
| 159 | +const mdEditor = useMarkdownEditor({ |
| 160 | + // ... |
| 161 | + extensionOptions: { |
| 162 | + commandMenu: {actions: [wYfmHtmlBlockItemData]}, |
| 163 | + }, |
| 164 | +}); |
| 165 | + |
| 166 | +``` |
| 167 | + |
0 commit comments