mirror of
https://github.com/pnp/sp-dev-fx-webparts.git
synced 2025-02-18 19:07:12 +00:00
Merge pull request #1998 from fabiofranzini/react-htm-templating
Initial release of the 'react-htm-templating' sample
This commit is contained in:
commit
e04cdc4056
33
samples/react-htm-templating/.gitignore
vendored
Normal file
33
samples/react-htm-templating/.gitignore
vendored
Normal file
@ -0,0 +1,33 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
|
||||
# Dependency directories
|
||||
node_modules
|
||||
|
||||
# Build generated files
|
||||
dist
|
||||
lib
|
||||
release
|
||||
solution
|
||||
temp
|
||||
*.sppkg
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
|
||||
# OSX
|
||||
.DS_Store
|
||||
|
||||
# Visual Studio files
|
||||
.ntvs_analysis.dat
|
||||
.vs
|
||||
bin
|
||||
obj
|
||||
|
||||
# Resx Generated Code
|
||||
*.resx.ts
|
||||
|
||||
# Styles Generated Code
|
||||
*.scss.ts
|
12
samples/react-htm-templating/.yo-rc.json
Normal file
12
samples/react-htm-templating/.yo-rc.json
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
"@microsoft/generator-sharepoint": {
|
||||
"isCreatingSolution": true,
|
||||
"environment": "spo",
|
||||
"version": "1.12.1",
|
||||
"libraryName": "react-htm",
|
||||
"libraryId": "302cd564-03ac-4ed5-8fb8-d2655ccbd7f4",
|
||||
"packageManager": "npm",
|
||||
"isDomainIsolated": false,
|
||||
"componentType": "webpart"
|
||||
}
|
||||
}
|
87
samples/react-htm-templating/README.md
Normal file
87
samples/react-htm-templating/README.md
Normal file
@ -0,0 +1,87 @@
|
||||
# React HTM (Hyperscript Tagged Markup) Templating
|
||||
|
||||
## Summary
|
||||
This Web Part provides an example of how to create a dynamic template system (configurable via Web Part properties) that can not only render HTML but also React controls, such as Fluent UI, without the need to use Handlebars.js + Web Components.
|
||||
|
||||
![picture of the web part in action](assets/preview.gif)
|
||||
|
||||
## Compatibility
|
||||
|
||||
![SPFx 1.12.1](https://img.shields.io/badge/SPFx-1.12.1-green.svg)
|
||||
![Node.js LTS v14 | LTS v12 | LTS v10](https://img.shields.io/badge/Node.js-LTS%20v14%20%7C%20LTS%20v12%20%7C%20LTS%20v10-green.svg)
|
||||
![SharePoint Online](https://img.shields.io/badge/SharePoint-Online-yellow.svg)
|
||||
![Teams N/A: Untested with Microsoft Teams](https://img.shields.io/badge/Teams-N%2FA-lightgrey.svg "Untested with Microsoft Teams")
|
||||
![Workbench Local | Hosted](https://img.shields.io/badge/Workbench-Local%20%7C%20Hosted-green.svg)
|
||||
|
||||
## Applies to
|
||||
|
||||
* [SharePoint Framework](https://docs.microsoft.com/sharepoint/dev/spfx/sharepoint-framework-overview)
|
||||
* [Microsoft 365 tenant](https://docs.microsoft.com/sharepoint/dev/spfx/set-up-your-development-environment)
|
||||
|
||||
## Solution
|
||||
|
||||
Solution|Author(s)
|
||||
--------|---------
|
||||
react-htm-templating | [Fabio Franzini](https://www.linkedin.com/in/fabiofranzini/) ([@franzinifabio](https://twitter.com/franzinifabio)), fabiofranzini.com
|
||||
|
||||
## Version history
|
||||
|
||||
Version|Date|Comments
|
||||
-------|----|--------
|
||||
1.0|August 4, 2021|Initial release
|
||||
|
||||
## Minimal Path to Awesome
|
||||
|
||||
* Clone this repository
|
||||
* in the command line run:
|
||||
* `npm install`
|
||||
* `gulp serve`
|
||||
|
||||
## Features
|
||||
|
||||
This example born from the need to have a template system that can directly use the "Fluent UI" components (or other components) written in React, without the need to use any wrapper such as, for example, encapsulating the rendering of components via Web Components.
|
||||
|
||||
The web part uses a template system called [HTM (Hyperscript Tagged Markup)](https://github.com/developit/htm), whose meaning is: "htm is JSX-like syntax in plain JavaScript - no transpiler necessary".
|
||||
|
||||
Basically, the template is nothing more than the JS code, which is evaluated through the Function constructor.
|
||||
|
||||
```
|
||||
...
|
||||
const evalFunction = new Function(this.properties.template);
|
||||
...
|
||||
const executeEvalFunction = () => {
|
||||
return evalFunction.call(evalFunctionContext);
|
||||
};
|
||||
...
|
||||
let renderComponent = html`<${executeEvalFunction} />`;
|
||||
ReactDom.render(renderComponent, this.domElement);
|
||||
```
|
||||
|
||||
The template must return the object generated by htm which is the component instance that you will have to render.
|
||||
|
||||
For HTM syntax please refer to the official [GitHub repo)](https://github.com/developit/htm).
|
||||
|
||||
The Web Part is configured to pass a context object to the template, with the following objects:
|
||||
- **this.html**: Reference to the HTM engine.
|
||||
- **this.react**: Reference to React.
|
||||
- **this.theme**: Reference to the 'Theme' object of SharePoint Page.
|
||||
- **this.themeVariant**: Reference to the Theme Variant object.
|
||||
- **this.fui**: Reference to the 'Fluent UI' namespace.
|
||||
- **this.fuiHooks**: Reference to the 'Fluent UI Hooks' namespace.
|
||||
- **this.fuiSampleData**: Reference to the 'Fluent UI Sample Data' namespace.
|
||||
|
||||
## Disclaimer
|
||||
|
||||
**THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.**
|
||||
|
||||
## Help
|
||||
|
||||
We do not support samples, but we this community is always willing to help, and we want to improve these samples. We use GitHub to track issues, which makes it easy for community members to volunteer their time and help resolve issues.
|
||||
|
||||
If you encounter any issues while using this sample, [create a new issue](https://github.com/pnp/sp-dev-fx-webparts/issues/new?assignees=&labels=Needs%3A+Triage+%3Amag%3A%2Ctype%3Abug-suspected&template=bug-report.yml&sample=react-htm-templating&authors=@fabiofranzini&title=react-htm-templating%20-%20).
|
||||
|
||||
For questions regarding this sample, [create a new question](https://github.com/pnp/sp-dev-fx-webparts/issues/new?assignees=&labels=Needs%3A+Triage+%3Amag%3A%2Ctype%3Abug-suspected&template=question.yml&sample=react-htm-templating&authors=@fabiofranzini&title=react-htm-templating%20-%20).
|
||||
|
||||
Finally, if you have an idea for improvement, [make a suggestion](https://github.com/pnp/sp-dev-fx-webparts/issues/new?assignees=&labels=Needs%3A+Triage+%3Amag%3A%2Ctype%3Abug-suspected&template=suggestion.yml&sample=react-htm-templating&authors=@fabiofranzini&title=react-htm-templating%20-%20).
|
||||
|
||||
<img src="https://telemetry.sharepointpnp.com/sp-dev-fx-webparts/samples/react-htm-templating" />
|
BIN
samples/react-htm-templating/assets/preview.gif
Normal file
BIN
samples/react-htm-templating/assets/preview.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 26 MiB |
50
samples/react-htm-templating/assets/sample.json
Normal file
50
samples/react-htm-templating/assets/sample.json
Normal file
@ -0,0 +1,50 @@
|
||||
[
|
||||
{
|
||||
"name": "pnp-sp-dev-spfx-web-parts-react-htm-templating",
|
||||
"source": "pnp",
|
||||
"title": "HTM (Hyperscript Tagged Markup) Templating",
|
||||
"shortDescription": "Provides an example of how to create a dynamic template system (configurable via Web Part properties) that can not only render HTML but also React controls, such as Fluent UI, without the need to use Handlebars.js + Web Components",
|
||||
"url": "https://github.com/pnp/sp-dev-fx-webparts/tree/main/samples/react-htm-templating",
|
||||
"longDescription": [
|
||||
"Provides an example of how to create a dynamic template system (configurable via Web Part properties) that can not only render HTML but also React controls, such as Fluent UI, without the need to use Handlebars.js + Web Components"
|
||||
],
|
||||
"creationDateTime": "2021-08-04",
|
||||
"updateDateTime": "2021-08-04",
|
||||
"products": [
|
||||
"SharePoint",
|
||||
"Office"
|
||||
],
|
||||
"metadata": [
|
||||
{
|
||||
"key": "CLIENT-SIDE-DEV",
|
||||
"value": "React"
|
||||
},
|
||||
{
|
||||
"key": "SPFX-VERSION",
|
||||
"value": "1.12.1"
|
||||
}
|
||||
],
|
||||
"thumbnails": [
|
||||
{
|
||||
"type": "image",
|
||||
"order": 100,
|
||||
"url": "https://github.com/pnp/sp-dev-fx-webparts/raw/main/samples/react-htm-templating/assets/preview.gif",
|
||||
"alt": "Preview"
|
||||
}
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"gitHubAccount": "fabiofranzini",
|
||||
"pictureUrl": "https://github.com/fabiofranzini.png",
|
||||
"name": "Fabio Franzini"
|
||||
}
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"name": "Build your first SharePoint client-side web part",
|
||||
"description": "Client-side web parts are client-side components that run in the context of a SharePoint page. Client-side web parts can be deployed to SharePoint environments that support the SharePoint Framework. You can also use modern JavaScript web frameworks, tools, and libraries to build them.",
|
||||
"url": "https://docs.microsoft.com/en-us/sharepoint/dev/spfx/web-parts/get-started/build-a-hello-world-web-part"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
20
samples/react-htm-templating/config/config.json
Normal file
20
samples/react-htm-templating/config/config.json
Normal file
@ -0,0 +1,20 @@
|
||||
{
|
||||
"$schema": "https://developer.microsoft.com/json-schemas/spfx-build/config.2.0.schema.json",
|
||||
"version": "2.0",
|
||||
"bundles": {
|
||||
"react-htm-web-part": {
|
||||
"components": [
|
||||
{
|
||||
"entrypoint": "./lib/webparts/reactHtm/ReactHtmWebPart.js",
|
||||
"manifest": "./src/webparts/reactHtm/ReactHtmWebPart.manifest.json"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"externals": {},
|
||||
"localizedResources": {
|
||||
"ReactHtmWebPartStrings": "lib/webparts/reactHtm/loc/{locale}.js",
|
||||
"PropertyControlStrings": "node_modules/@pnp/spfx-property-controls/lib/loc/{locale}.js",
|
||||
"ControlStrings": "node_modules/@pnp/spfx-controls-react/lib/loc/{locale}.js"
|
||||
}
|
||||
}
|
4
samples/react-htm-templating/config/copy-assets.json
Normal file
4
samples/react-htm-templating/config/copy-assets.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"$schema": "https://developer.microsoft.com/json-schemas/spfx-build/copy-assets.schema.json",
|
||||
"deployCdnPath": "./release/assets/"
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
{
|
||||
"$schema": "https://developer.microsoft.com/json-schemas/spfx-build/deploy-azure-storage.schema.json",
|
||||
"workingDir": "./release/assets/",
|
||||
"account": "<!-- STORAGE ACCOUNT NAME -->",
|
||||
"container": "react-htm",
|
||||
"accessKey": "<!-- ACCESS KEY -->"
|
||||
}
|
20
samples/react-htm-templating/config/package-solution.json
Normal file
20
samples/react-htm-templating/config/package-solution.json
Normal file
@ -0,0 +1,20 @@
|
||||
{
|
||||
"$schema": "https://developer.microsoft.com/json-schemas/spfx-build/package-solution.schema.json",
|
||||
"solution": {
|
||||
"name": "react-htm-client-side-solution",
|
||||
"id": "302cd564-03ac-4ed5-8fb8-d2655ccbd7f4",
|
||||
"version": "1.0.0.0",
|
||||
"includeClientSideAssets": true,
|
||||
"isDomainIsolated": false,
|
||||
"developer": {
|
||||
"name": "Fabio Franzini",
|
||||
"websiteUrl": "https://fabiofranzini.com/",
|
||||
"privacyUrl": "",
|
||||
"termsOfUseUrl": "",
|
||||
"mpnId": ""
|
||||
}
|
||||
},
|
||||
"paths": {
|
||||
"zippedPackage": "solution/react-htm.sppkg"
|
||||
}
|
||||
}
|
10
samples/react-htm-templating/config/serve.json
Normal file
10
samples/react-htm-templating/config/serve.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"$schema": "https://developer.microsoft.com/json-schemas/core-build/serve.schema.json",
|
||||
"port": 4321,
|
||||
"https": true,
|
||||
"initialPage": "https://localhost:5432/workbench",
|
||||
"api": {
|
||||
"port": 5432,
|
||||
"entryPath": "node_modules/@microsoft/sp-webpart-workbench/lib/api/"
|
||||
}
|
||||
}
|
4
samples/react-htm-templating/config/write-manifests.json
Normal file
4
samples/react-htm-templating/config/write-manifests.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"$schema": "https://developer.microsoft.com/json-schemas/spfx-build/write-manifests.schema.json",
|
||||
"cdnBasePath": "<!-- PATH TO CDN -->"
|
||||
}
|
16
samples/react-htm-templating/gulpfile.js
vendored
Normal file
16
samples/react-htm-templating/gulpfile.js
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
const build = require('@microsoft/sp-build-web');
|
||||
|
||||
build.addSuppression(`Warning - [sass] The local CSS class 'ms-Grid' is not camelCase and will not be type-safe.`);
|
||||
|
||||
var getTasks = build.rig.getTasks;
|
||||
build.rig.getTasks = function () {
|
||||
var result = getTasks.call(build.rig);
|
||||
|
||||
result.set('serve', result.get('serve-deprecated'));
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
build.initialize(require('gulp'));
|
22464
samples/react-htm-templating/package-lock.json
generated
Normal file
22464
samples/react-htm-templating/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
39
samples/react-htm-templating/package.json
Normal file
39
samples/react-htm-templating/package.json
Normal file
@ -0,0 +1,39 @@
|
||||
{
|
||||
"name": "react-htm",
|
||||
"version": "0.0.1",
|
||||
"private": true,
|
||||
"main": "lib/index.js",
|
||||
"scripts": {
|
||||
"build": "gulp bundle",
|
||||
"clean": "gulp clean",
|
||||
"test": "gulp test"
|
||||
},
|
||||
"dependencies": {
|
||||
"@fluentui/example-data": "^8.2.5",
|
||||
"@fluentui/react": "^8.26.2",
|
||||
"@fluentui/react-hooks": "^8.2.6",
|
||||
"@microsoft/sp-core-library": "1.12.1",
|
||||
"@microsoft/sp-lodash-subset": "1.12.1",
|
||||
"@microsoft/sp-office-ui-fabric-core": "1.12.1",
|
||||
"@microsoft/sp-property-pane": "1.12.1",
|
||||
"@microsoft/sp-webpart-base": "1.12.1",
|
||||
"@pnp/spfx-controls-react": "3.2.1",
|
||||
"@pnp/spfx-property-controls": "3.2.0",
|
||||
"htm": "^3.1.0",
|
||||
"office-ui-fabric-react": "7.156.0",
|
||||
"react": "16.9.0",
|
||||
"react-dom": "16.9.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "16.9.36",
|
||||
"@types/react-dom": "16.9.8",
|
||||
"@microsoft/sp-build-web": "1.12.1",
|
||||
"@microsoft/sp-tslint-rules": "1.12.1",
|
||||
"@microsoft/sp-module-interfaces": "1.12.1",
|
||||
"@microsoft/sp-webpart-workbench": "1.12.1",
|
||||
"@microsoft/rush-stack-compiler-3.7": "0.2.3",
|
||||
"gulp": "~4.0.2",
|
||||
"ajv": "~5.2.2",
|
||||
"@types/webpack-env": "1.13.1"
|
||||
}
|
||||
}
|
1
samples/react-htm-templating/src/index.ts
Normal file
1
samples/react-htm-templating/src/index.ts
Normal file
@ -0,0 +1 @@
|
||||
// A file is required to be in the root of the /src directory by the TypeScript compiler
|
@ -0,0 +1,24 @@
|
||||
{
|
||||
"$schema": "https://developer.microsoft.com/json-schemas/spfx/client-side-web-part-manifest.schema.json",
|
||||
"id": "3c0443ba-8202-4d11-817e-482586feeb6f",
|
||||
"alias": "ReactHtmWebPart",
|
||||
"componentType": "WebPart",
|
||||
"version": "*",
|
||||
"manifestVersion": 2,
|
||||
"requiresCustomScript": false,
|
||||
"supportedHosts": ["SharePointWebPart", "SharePointFullPage"],
|
||||
"supportsThemeVariants": true,
|
||||
"supportsFullBleed": true,
|
||||
"preconfiguredEntries": [{
|
||||
"groupId": "5c03119e-3074-46fd-976b-c60198311f70",
|
||||
"group": { "default": "Other" },
|
||||
"title": { "default": "React HTM (Hyperscript Tagged Markup)" },
|
||||
"description": { "default": "Use HTM (Hyperscript Tagged Markup) as Template Engine for React components like 'Fluent UI React'" },
|
||||
"officeFabricIconFontName": "CodeEdit",
|
||||
"properties": {
|
||||
"template": "let [count, setCount] = this.react.useState(0);\r\nconst originalItems = this.fuiHooks.useConst(() => this.fuiSampleData.createListItems(10));\r\nconst classNames = this.fui.mergeStyleSets({\r\n itemCell: [\r\n this.fui.getFocusStyle(this.theme, { inset: -1 }),\r\n {\r\n minHeight: 54,\r\n padding: 10,\r\n boxSizing: 'border-box',\r\n borderBottom: `1px solid ${this.themeVariant.semanticColors.bodyDivider}`,\r\n display: 'flex',\r\n selectors: {\r\n '&:hover': { background: this.themeVariant.palette.neutralLight },\r\n }\r\n },\r\n ],\r\n itemImage: {\r\n flexShrink: 0,\r\n },\r\n itemContent: {\r\n marginLeft: 10,\r\n overflow: 'hidden',\r\n flexGrow: 1,\r\n },\r\n itemName: [\r\n this.themeVariant.fonts.xLarge,\r\n {\r\n whiteSpace: 'nowrap',\r\n overflow: 'hidden',\r\n textOverflow: 'ellipsis',\r\n },\r\n ],\r\n itemIndex: {\r\n fontSize: this.themeVariant.fonts.small.fontSize,\r\n color: this.themeVariant.palette.neutralTertiary,\r\n marginBottom: 10,\r\n },\r\n chevron: {\r\n alignSelf: 'center',\r\n marginLeft: 10,\r\n color: this.themeVariant.palette.neutralTertiary,\r\n fontSize: this.themeVariant.fonts.large.fontSize,\r\n flexShrink: 0,\r\n },\r\n});\r\n\r\nconst onRenderCell = (item, index) => {\r\n return this.html`\r\n <div class=${classNames.itemCell} data-is-focusable>\r\n <${this.fui.Image} className=${classNames.itemImage} src=${item.thumbnail} width=${50} height=${50} imageFit=${this.fui.ImageFit.cover} />\r\n <div class=${classNames.itemContent}>\r\n <div class=${classNames.itemName}>${item.name}</div>\r\n <div class=${classNames.itemIndex}>${`Item ${index}`}</div>\r\n <div>${item.description}</div>\r\n </div>\r\n <${this.fui.Icon} className=${classNames.chevron} iconName=${this.fui.getRTL() ? 'ChevronLeft' : 'ChevronRight'} />\r\n </div>`;\r\n};\r\n\r\nreturn this.html`\r\n <${this.fui.ThemeProvider} theme=${this.themeVariant}>\r\n <${this.fui.Stack} tokens=${{ childrenGap: 10 }}>\r\n <${this.fui.Label}>Clicked ${count} times<//>\r\n <${this.fui.PrimaryButton} text=\"Click here!\" onClick=${() => setCount(count + 1)} />\r\n <${this.fui.List} items=${originalItems} onRenderCell=${onRenderCell} />\r\n <//>\r\n <//>`;",
|
||||
"loadFluentUI": true,
|
||||
"loadFluentUISampleData": true
|
||||
}
|
||||
}]
|
||||
}
|
@ -0,0 +1,236 @@
|
||||
import { getTheme } from '@fluentui/react';
|
||||
import { IReadonlyTheme, ThemeChangedEventArgs, ThemeProvider } from '@microsoft/sp-component-base';
|
||||
import { Version } from '@microsoft/sp-core-library';
|
||||
import { isEqual } from '@microsoft/sp-lodash-subset';
|
||||
import {
|
||||
IPropertyPaneConfiguration,
|
||||
PropertyPaneToggle
|
||||
} from '@microsoft/sp-property-pane';
|
||||
import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';
|
||||
import * as htm from 'htm';
|
||||
import * as React from 'react';
|
||||
import * as ReactDom from 'react-dom';
|
||||
import * as strings from 'ReactHtmWebPartStrings';
|
||||
import ShowError, { IShowErrorProps } from './components/showError';
|
||||
import ShowPlaceholder, { IShowPlaceholderProps } from './components/showPlaceholder';
|
||||
|
||||
export interface IReactHtmWebPartProps {
|
||||
template: string;
|
||||
loadFluentUI: boolean;
|
||||
loadFluentUISampleData: boolean;
|
||||
}
|
||||
|
||||
export default class ReactHtmWebPart extends BaseClientSideWebPart<IReactHtmWebPartProps> {
|
||||
|
||||
protected themeProvider: ThemeProvider;
|
||||
protected themeVariant: IReadonlyTheme;
|
||||
|
||||
protected propertyFieldCodeEditor;
|
||||
protected propertyFieldCodeEditorLanguages;
|
||||
protected propertyPaneWebPartInformation;
|
||||
|
||||
protected fluentUIReact;
|
||||
protected fluentUIHooks;
|
||||
protected fluentUISampleData;
|
||||
|
||||
protected get isRenderAsync(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected async onInit(): Promise<void> {
|
||||
this.initThemeVariant();
|
||||
|
||||
return super.onInit();
|
||||
}
|
||||
|
||||
public async render(): Promise<void> {
|
||||
this.showLoadingIndicator();
|
||||
|
||||
if (this.properties.loadFluentUI) {
|
||||
this.fluentUIReact = await import(
|
||||
/* webpackChunkName: 'react-htm-fluentui-react' */
|
||||
'@fluentui/react'
|
||||
);
|
||||
|
||||
this.fluentUIHooks = await import(
|
||||
/* webpackChunkName: 'react-htm-fluentui-hooks' */
|
||||
'@fluentui/react-hooks'
|
||||
);
|
||||
}
|
||||
|
||||
if (this.properties.loadFluentUISampleData) {
|
||||
this.fluentUISampleData = await import(
|
||||
/* webpackChunkName: 'react-htm-fluentui-example-data' */
|
||||
'@fluentui/example-data'
|
||||
);
|
||||
}
|
||||
|
||||
this.renderCompleted();
|
||||
}
|
||||
|
||||
protected renderCompleted(): void {
|
||||
|
||||
if (this.properties.template) {
|
||||
const html = htm.default.bind(React.createElement);
|
||||
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function
|
||||
// https://remarkablemark.org/blog/2018/05/15/javascript-eval-vs-function/
|
||||
// https://docs.microsoft.com/en-us/javascript/api/sp-loader/spcomponentloader
|
||||
const evalFunction = new Function(this.properties.template);
|
||||
const evalFunctionContext = {
|
||||
html: html,
|
||||
react: React,
|
||||
theme: getTheme(),
|
||||
themeVariant: this.themeVariant,
|
||||
fui: (this.properties.loadFluentUI) ? this.fluentUIReact : undefined,
|
||||
fuiHooks: (this.properties.loadFluentUI) ? this.fluentUIHooks : undefined,
|
||||
fuiSampleData: (this.properties.loadFluentUISampleData) ? this.fluentUISampleData : undefined
|
||||
};
|
||||
|
||||
const executeEvalFunction = () => {
|
||||
return evalFunction.call(evalFunctionContext);
|
||||
};
|
||||
|
||||
let evalError = null;
|
||||
|
||||
try {
|
||||
let renderComponent = html`<${executeEvalFunction} />`;
|
||||
|
||||
this.hideLoadingIndicator();
|
||||
|
||||
//@ts-ignore
|
||||
ReactDom.render(renderComponent, this.domElement);
|
||||
} catch (error) {
|
||||
evalError = error.toString();
|
||||
}
|
||||
|
||||
if (evalError) {
|
||||
this.hideLoadingIndicator();
|
||||
const element: React.ReactElement<IShowErrorProps> = React.createElement(
|
||||
ShowError,
|
||||
{
|
||||
message: evalError,
|
||||
themeVariant: this.themeVariant
|
||||
}
|
||||
);
|
||||
ReactDom.render(element, this.domElement);
|
||||
}
|
||||
}
|
||||
else {
|
||||
this.hideLoadingIndicator();
|
||||
const element: React.ReactElement<IShowPlaceholderProps> = React.createElement(
|
||||
ShowPlaceholder,
|
||||
{
|
||||
context: this.context,
|
||||
themeVariant: this.themeVariant
|
||||
}
|
||||
);
|
||||
ReactDom.render(element, this.domElement);
|
||||
}
|
||||
|
||||
super.renderCompleted();
|
||||
}
|
||||
|
||||
protected onDispose(): void {
|
||||
ReactDom.unmountComponentAtNode(this.domElement);
|
||||
}
|
||||
|
||||
protected get dataVersion(): Version {
|
||||
return Version.parse('1.0');
|
||||
}
|
||||
|
||||
protected get disableReactivePropertyChanges(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected async loadPropertyPaneResources(): Promise<void> {
|
||||
const { PropertyFieldCodeEditor, PropertyFieldCodeEditorLanguages } = await import(
|
||||
/* webpackChunkName: 'pnp-controls-property-field-code-editor' */
|
||||
'@pnp/spfx-property-controls/lib/PropertyFieldCodeEditor'
|
||||
);
|
||||
this.propertyFieldCodeEditor = PropertyFieldCodeEditor;
|
||||
this.propertyFieldCodeEditorLanguages = PropertyFieldCodeEditorLanguages;
|
||||
|
||||
const { PropertyPaneWebPartInformation } = await import(
|
||||
/* webpackChunkName: 'pnp-controls-property-field-information' */
|
||||
'@pnp/spfx-property-controls/lib/PropertyPaneWebPartInformation'
|
||||
);
|
||||
this.propertyPaneWebPartInformation = PropertyPaneWebPartInformation;
|
||||
}
|
||||
|
||||
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
|
||||
let htmlDescription = `
|
||||
<p>The <a href="https://github.com/developit/htm" target="_blank">HTM (Hyperscript Tagged Markup)</a> is used as the template language and the following objects are made available inside your js script:</p>
|
||||
<ul>
|
||||
<li><b>this.html</b>: Reference to the HTM engine</li>
|
||||
<li><b>this.react</b>: Reference to React</li>
|
||||
<li><b>this.theme</b>: Reference to the 'Theme' object of SharePoint Page</li>
|
||||
<li><b>this.themeVariant</b>: Reference to the Theme Variant object</li>
|
||||
<li><b>this.fui</b>: Reference to the 'Fluent UI' namespace</li>
|
||||
<li><b>this.fuiHooks</b>: Reference to the 'Fluent UI Hooks' namespace</li>
|
||||
<li><b>this.fuiSampleData</b>: Reference to the 'Fluent UI Sample Data' namespace</li>
|
||||
</ul>
|
||||
<p>Author: <a href="https://twitter.com/franzinifabio" target="_blank">Fabio Franzini</a><p>
|
||||
`;
|
||||
|
||||
return {
|
||||
pages: [
|
||||
{
|
||||
header: {
|
||||
description: strings.PropertyPaneDescription
|
||||
},
|
||||
groups: [
|
||||
{
|
||||
groupName: strings.BasicGroupName,
|
||||
groupFields: [
|
||||
this.propertyFieldCodeEditor('template', {
|
||||
label: strings.TemplateFieldLabel,
|
||||
panelTitle: strings.TemplateFieldLabel,
|
||||
initialValue: this.properties.template,
|
||||
onPropertyChange: this.onPropertyPaneFieldChanged,
|
||||
properties: this.properties,
|
||||
disabled: false,
|
||||
key: 'codeEditorFieldId',
|
||||
language: this.propertyFieldCodeEditorLanguages.JavaScript
|
||||
}),
|
||||
PropertyPaneToggle('loadFluentUI', {
|
||||
label: strings.LoadFluentUIFieldLabel,
|
||||
onText: strings.FieldLabelOn,
|
||||
offText: strings.FieldLabelOff
|
||||
}),
|
||||
PropertyPaneToggle('loadFluentUISampleData', {
|
||||
label: strings.LoadFluentUISampleDataFieldLabel,
|
||||
onText: strings.FieldLabelOn,
|
||||
offText: strings.FieldLabelOff
|
||||
}),
|
||||
this.propertyPaneWebPartInformation({
|
||||
description: htmlDescription,
|
||||
key: 'webPartInfoId'
|
||||
})
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
protected showLoadingIndicator(): void {
|
||||
this.context.statusRenderer.displayLoadingIndicator(this.domElement, "");
|
||||
}
|
||||
|
||||
protected hideLoadingIndicator(): void {
|
||||
this.context.statusRenderer.clearLoadingIndicator(this.domElement);
|
||||
}
|
||||
|
||||
private initThemeVariant(): void {
|
||||
this.themeProvider = this.context.serviceScope.consume(ThemeProvider.serviceKey);
|
||||
this.themeVariant = this.themeProvider.tryGetTheme();
|
||||
this.themeProvider.themeChangedEvent.add(this, (args: ThemeChangedEventArgs): void => {
|
||||
if (!isEqual(this.themeVariant, args.theme)) {
|
||||
this.themeVariant = args.theme;
|
||||
this.render();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
import { ThemeProvider } from '@fluentui/react/lib/Theme';
|
||||
import { IReadonlyTheme } from '@microsoft/sp-component-base';
|
||||
import { MessageBar, MessageBarType } from 'office-ui-fabric-react';
|
||||
import * as React from 'react';
|
||||
|
||||
export interface IShowErrorProps {
|
||||
message: string;
|
||||
themeVariant: IReadonlyTheme;
|
||||
}
|
||||
|
||||
function ShowError(props: IShowErrorProps) {
|
||||
return (
|
||||
<ThemeProvider theme={props.themeVariant}>
|
||||
<MessageBar messageBarType={MessageBarType.error}>
|
||||
{props.message}
|
||||
</MessageBar>
|
||||
</ThemeProvider>
|
||||
);
|
||||
}
|
||||
|
||||
export default ShowError;
|
@ -0,0 +1,27 @@
|
||||
import { ThemeProvider } from '@fluentui/react';
|
||||
import { IReadonlyTheme } from '@microsoft/sp-component-base';
|
||||
import { WebPartContext } from "@microsoft/sp-webpart-base";
|
||||
import { Placeholder } from "@pnp/spfx-controls-react/lib/Placeholder";
|
||||
import * as React from 'react';
|
||||
import * as strings from 'ReactHtmWebPartStrings';
|
||||
|
||||
export interface IShowPlaceholderProps {
|
||||
context: WebPartContext;
|
||||
themeVariant: IReadonlyTheme;
|
||||
}
|
||||
|
||||
function ShowPlaceholder(props: IShowPlaceholderProps) {
|
||||
return (
|
||||
<ThemeProvider theme={props.themeVariant}>
|
||||
<Placeholder iconName='CodeEdit'
|
||||
iconText={strings.PlaceholderIconText}
|
||||
description={strings.PlaceholderDescription}
|
||||
buttonLabel={strings.PlaceholderButtonLabel}
|
||||
onConfigure={() => {
|
||||
this.props.context.propertyPane.open();
|
||||
}} />
|
||||
</ThemeProvider>
|
||||
);
|
||||
}
|
||||
|
||||
export default ShowPlaceholder;
|
14
samples/react-htm-templating/src/webparts/reactHtm/loc/en-us.js
vendored
Normal file
14
samples/react-htm-templating/src/webparts/reactHtm/loc/en-us.js
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
define([], function () {
|
||||
return {
|
||||
"PropertyPaneDescription": "Web Part Configuration",
|
||||
"BasicGroupName": "Core Settings",
|
||||
"TemplateFieldLabel": "Edit HTM Template",
|
||||
"LoadFluentUIFieldLabel": "Load 'Fluent UI' Library components",
|
||||
"LoadFluentUISampleDataFieldLabel": "Load 'Fluent UI' sample data",
|
||||
"FieldLabelOn": "Yes",
|
||||
"FieldLabelOff": "No",
|
||||
"PlaceholderIconText": "Configure the Web Part",
|
||||
"PlaceholderDescription": "Please configure the Web Part, you can edit the template by using JavaScript and the HTM (Hyperscript Tagged Markup)",
|
||||
"PlaceholderButtonLabel": "Configure"
|
||||
}
|
||||
});
|
17
samples/react-htm-templating/src/webparts/reactHtm/loc/mystrings.d.ts
vendored
Normal file
17
samples/react-htm-templating/src/webparts/reactHtm/loc/mystrings.d.ts
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
declare interface IReactHtmWebPartStrings {
|
||||
PropertyPaneDescription: string;
|
||||
BasicGroupName: string;
|
||||
TemplateFieldLabel: string;
|
||||
LoadFluentUIFieldLabel: string;
|
||||
LoadFluentUISampleDataFieldLabel: string;
|
||||
FieldLabelOn: string;
|
||||
FieldLabelOff: string;
|
||||
PlaceholderIconText: string;
|
||||
PlaceholderDescription: string;
|
||||
PlaceholderButtonLabel: string;
|
||||
}
|
||||
|
||||
declare module 'ReactHtmWebPartStrings' {
|
||||
const strings: IReactHtmWebPartStrings;
|
||||
export = strings;
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
Binary file not shown.
After Width: | Height: | Size: 383 B |
35
samples/react-htm-templating/tsconfig.json
Normal file
35
samples/react-htm-templating/tsconfig.json
Normal file
@ -0,0 +1,35 @@
|
||||
{
|
||||
"extends": "./node_modules/@microsoft/rush-stack-compiler-3.7/includes/tsconfig-web.json",
|
||||
"compilerOptions": {
|
||||
"target": "es5",
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"module": "esnext",
|
||||
"moduleResolution": "node",
|
||||
"jsx": "react",
|
||||
"declaration": true,
|
||||
"sourceMap": true,
|
||||
"experimentalDecorators": true,
|
||||
"skipLibCheck": true,
|
||||
"outDir": "lib",
|
||||
"inlineSources": false,
|
||||
"strictNullChecks": false,
|
||||
"noUnusedLocals": false,
|
||||
"typeRoots": [
|
||||
"./node_modules/@types",
|
||||
"./node_modules/@microsoft"
|
||||
],
|
||||
"types": [
|
||||
"webpack-env"
|
||||
],
|
||||
"lib": [
|
||||
"es5",
|
||||
"dom",
|
||||
"es2015.collection",
|
||||
"es2015.promise"
|
||||
]
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.ts",
|
||||
"src/**/*.tsx"
|
||||
]
|
||||
}
|
30
samples/react-htm-templating/tslint.json
Normal file
30
samples/react-htm-templating/tslint.json
Normal file
@ -0,0 +1,30 @@
|
||||
{
|
||||
"extends": "./node_modules/@microsoft/sp-tslint-rules/base-tslint.json",
|
||||
"rules": {
|
||||
"class-name": false,
|
||||
"export-name": false,
|
||||
"forin": false,
|
||||
"label-position": false,
|
||||
"member-access": true,
|
||||
"no-arg": false,
|
||||
"no-console": false,
|
||||
"no-construct": false,
|
||||
"no-duplicate-variable": true,
|
||||
"no-eval": false,
|
||||
"no-function-expression": true,
|
||||
"no-internal-module": true,
|
||||
"no-shadowed-variable": true,
|
||||
"no-switch-case-fall-through": true,
|
||||
"no-unnecessary-semicolons": true,
|
||||
"no-unused-expression": true,
|
||||
"no-use-before-declare": true,
|
||||
"no-with-statement": true,
|
||||
"semicolon": true,
|
||||
"trailing-comma": false,
|
||||
"typedef": false,
|
||||
"typedef-whitespace": false,
|
||||
"use-named-parameter": true,
|
||||
"variable-name": false,
|
||||
"whitespace": false
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user