Skip to content

Commit

Permalink
HtmlEditor: Markdown Support demo
Browse files Browse the repository at this point in the history
  • Loading branch information
marker-dao authored Nov 22, 2024
1 parent 41477d6 commit 568d46d
Show file tree
Hide file tree
Showing 70 changed files with 1,389 additions and 574 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
::ng-deep .dx-htmleditor-content img {
vertical-align: middle;
padding-right: 10px;
margin-bottom: 22px;
}

::ng-deep .value-title {
font-size: var(--dx-font-size-sm);
font-weight: 500;
}

::ng-deep .value-content {
margin-top: 20px;
overflow: auto;
height: 110px;
width: 100%;
height: 210px;
white-space: pre-wrap;
border: 1px solid var(--dx-color-border);
padding: 16px;
background-color: var(--dx-color-main-bg);
}

.options {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<dx-html-editor [height]="300" [converter]="converter" [(value)]="valueContent">
<dxo-toolbar>
<dxi-item name="undo"></dxi-item>
<dxi-item name="redo"></dxi-item>
<dxi-item name="separator"></dxi-item>
<dxi-item name="bold"></dxi-item>
<dxi-item name="italic"></dxi-item>
<dxi-item name="separator"></dxi-item>
<dxi-item
name="header"
[acceptedValues]="[false, 1, 2, 3, 4, 5]"
[options]="{ inputAttr: { 'aria-label': 'Header' } }"
></dxi-item>
<dxi-item name="separator"></dxi-item>
<dxi-item name="orderedList"></dxi-item>
<dxi-item name="bulletList"></dxi-item>
</dxo-toolbar>
</dx-html-editor>

<div class="options">
<div class="value-title"> Markdown Preview </div>
<div class="value-content" tabindex="0">{{ valueContent }}</div>
</div>
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { NgModule, Component, enableProdMode } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import prettier from 'prettier/standalone';
import parserHtml from 'prettier/parser-html';
import { DxHtmlEditorModule, DxHtmlEditorTypes } from 'devextreme-angular/ui/html-editor';
import { DxButtonGroupModule, DxButtonGroupTypes } from 'devextreme-angular/ui/button-group';
import { DxHtmlEditorModule } from 'devextreme-angular/ui/html-editor';
import { Service } from './app.service';

interface Converter {
toHtml: (value: string) => string;
fromHtml: (value: string) => string;
}

if (!/localhost/.test(document.location.host)) {
enableProdMode();
}
Expand All @@ -27,32 +29,48 @@ if (window && window.config.packageConfigPaths) {
export class AppComponent {
valueContent: string;

editorValueType: DxHtmlEditorTypes.MarkupType = 'html';
converter: Converter;

constructor(service: Service) {
this.valueContent = service.getMarkup();
}

onValueTypeChanged({ addedItems }: DxButtonGroupTypes.SelectionChangedEvent) {
this.editorValueType = addedItems[0].text.toLowerCase();
}

prettierFormat(markup: string) {
if (this.editorValueType === 'html') {
return prettier.format(markup, {
parser: 'html',
plugins: [parserHtml],
});
this.converter = {
toHtml(value) {
// @ts-expect-error
const result = unified()
// @ts-expect-error
.use(remarkParse)
// @ts-expect-error
.use(remarkRehype)
// @ts-expect-error
.use(rehypeStringify)
.processSync(value)
.toString();

return result;
},
fromHtml(value) {
// @ts-expect-error
const result = unified()
// @ts-expect-error
.use(rehypeParse)
// @ts-expect-error
.use(rehypeRemark)
// @ts-expect-error
.use(remarkStringify)
.processSync(value)
.toString();

return result;
},
}
return markup;
}
}

@NgModule({
imports: [
BrowserModule,
DxHtmlEditorModule,
DxButtonGroupModule,
],
declarations: [AppComponent],
bootstrap: [AppComponent],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Injectable } from '@angular/core';

const markup = `## ![HtmlEditor](../../../../images/widgets/HtmlEditor.svg) Formatted Text Editor (HTML Editor)
Lists:
1. Use numbers followed by a period for an ordered list.
2. Use a single asterisk for a bullet list.
Formats:
* Enclose a word in single asterisks for *italic*.
* Enclose a word in double asterisks for **bold**.
`;

@Injectable()
export class Service {
getMarkup(): string {
return markup;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0" />
<link rel="stylesheet" type="text/css" href="../../../../node_modules/devextreme-dist/css/dx.light.css" />

<script type="module">
import { unified } from "https://esm.sh/unified@11?bundle";
import remarkParse from "https://esm.sh/remark-parse@11?bundle";
import remarkRehype from "https://esm.sh/remark-rehype@11?bundle";
import rehypeStringify from "https://esm.sh/rehype-stringify@10?bundle";
import rehypeParse from "https://esm.sh/rehype-parse@9?bundle";
import rehypeRemark from "https://esm.sh/rehype-remark@10?bundle";
import remarkStringify from "https://esm.sh/remark-stringify@11?bundle";

window.unified = unified;
window.remarkParse = remarkParse;
window.remarkRehype = remarkRehype;
window.rehypeStringify = rehypeStringify;
window.rehypeParse = rehypeParse;
window.rehypeRemark = rehypeRemark;
window.remarkStringify = remarkStringify;
</script>

<script src="../../../../node_modules/core-js/client/shim.min.js"></script>
<script src="../../../../node_modules/zone.js/bundles/zone.umd.js"></script>
<script src="../../../../node_modules/reflect-metadata/Reflect.js"></script>
Expand Down
81 changes: 81 additions & 0 deletions apps/demos/Demos/HtmlEditor/MarkdownSupport/React/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React, { useCallback, useState } from 'react';
import HtmlEditor, { Toolbar, Item } from 'devextreme-react/html-editor';
import { markup } from './data.ts';

const headerValues = [false, 1, 2, 3, 4, 5];
const headerOptions = {
inputAttr: {
'aria-label': 'Header',
},
};
const converter = {
toHtml(value) {
// @ts-expect-error
const result = unified()
// @ts-expect-error
.use(remarkParse)
// @ts-expect-error
.use(remarkRehype)
// @ts-expect-error
.use(rehypeStringify)
.processSync(value)
.toString();

return result;
},
fromHtml(value) {
// @ts-expect-error
const result = unified()
// @ts-expect-error
.use(rehypeParse)
// @ts-expect-error
.use(rehypeRemark)
// @ts-expect-error
.use(remarkStringify)
.processSync(value)
.toString();

return result;
},
};

export default function App() {
const [valueContent, setValueContent] = useState(markup);

const valueChanged = useCallback((e: { value?: string; }) => {
setValueContent(e.value);
}, [setValueContent]);

return (
<div className="widget-container">
<HtmlEditor
converter={converter}
height={300}
defaultValue={valueContent}
onValueChanged={valueChanged}
>
<Toolbar>
<Item name="undo" />
<Item name="redo" />
<Item name="separator" />
<Item name="bold" />
<Item name="italic" />
<Item name="separator" />
<Item name="header" acceptedValues={headerValues} options={headerOptions} />
<Item name="separator" />
<Item name="orderedList" />
<Item name="bulletList" />
</Toolbar>
</HtmlEditor>

<div className="options">
<div className="value-title">
Markdown Preview
</div>
<div className="value-content" tabIndex={0}>
{valueContent}
</div>
</div>
</div>
);
}
12 changes: 12 additions & 0 deletions apps/demos/Demos/HtmlEditor/MarkdownSupport/React/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const markup = `## ![HtmlEditor](../../../../images/widgets/HtmlEditor.svg) Formatted Text Editor (HTML Editor)
Lists:
1. Use numbers followed by a period for an ordered list.
2. Use a single asterisk for a bullet list.
Formats:
* Enclose a word in single asterisks for *italic*.
* Enclose a word in double asterisks for **bold**.
`;
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@
<link rel="stylesheet" type="text/css" href="../../../../node_modules/devextreme-dist/css/dx.light.css" />
<link rel="stylesheet" type="text/css" href="styles.css" />

<script type="module">
import { unified } from "https://esm.sh/unified@11?bundle";
import remarkParse from "https://esm.sh/remark-parse@11?bundle";
import remarkRehype from "https://esm.sh/remark-rehype@11?bundle";
import rehypeStringify from "https://esm.sh/rehype-stringify@10?bundle";
import rehypeParse from "https://esm.sh/rehype-parse@9?bundle";
import rehypeRemark from "https://esm.sh/rehype-remark@10?bundle";
import remarkStringify from "https://esm.sh/remark-stringify@11?bundle";

window.unified = unified;
window.remarkParse = remarkParse;
window.remarkRehype = remarkRehype;
window.rehypeStringify = rehypeStringify;
window.rehypeParse = rehypeParse;
window.rehypeRemark = rehypeRemark;
window.remarkStringify = remarkStringify;
</script>

<script src="../../../../node_modules/core-js/client/shim.min.js"></script>
<script src="../../../../node_modules/systemjs/dist/system.js"></script>
<script type="text/javascript" src="config.js"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
.dx-htmleditor-content img {
vertical-align: middle;
padding-right: 10px;
margin-bottom: 22px;
}

.value-title {
font-size: var(--dx-font-size-sm);
font-weight: 500;
}

.value-content {
margin-top: 20px;
overflow: auto;
height: 110px;
width: 100%;
height: 210px;
white-space: pre-wrap;
border: 1px solid var(--dx-color-border);
padding: 16px;
background-color: var(--dx-color-main-bg);
}

.options {
Expand Down
Loading

0 comments on commit 568d46d

Please sign in to comment.