Merge branch 'dev'

This commit is contained in:
Mikael Svenson 2020-04-02 16:16:59 +02:00
commit f5306be31a
3 changed files with 25 additions and 10 deletions

View File

@ -161,6 +161,7 @@ Version|Date|Comments
1.0.0.13|July 1th, 2019|Downgrade to SPFx v1.4.1 to support SP2019
1.0.0.14|Oct 13th, 2019|Added resolve to fix pnpm issue. Updated author info.
1.0.0.15|Mar 16th, 2020|Upgrade to SPFx v1.10.0. Add support for Teams tab. Renamed package file.
1.0.0.16|April 1st, 2010|Improved how script tags are handled and cleaned up on smart page navigation.
## 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.**

View File

@ -3,7 +3,7 @@
"solution": {
"name": "Modern Script Editor web part by mikaelsvenson",
"id": "1425175f-3ed8-44d2-8fc4-dd1497191294",
"version": "1.0.0.15",
"version": "1.0.0.16",
"includeClientSideAssets": true,
"skipFeatureDeployment": false,
"isDomainIsolated": false

View File

@ -10,6 +10,7 @@ import PropertyPaneLogo from './PropertyPaneLogo';
export default class ScriptEditorWebPart extends BaseClientSideWebPart<IScriptEditorWebPartProps> {
public _propertyPaneHelper;
private _unqiueId;
constructor() {
super();
@ -22,6 +23,7 @@ export default class ScriptEditorWebPart extends BaseClientSideWebPart<IScriptEd
}
public render(): void {
this._unqiueId = this.context.instanceId;
if (this.displayMode == DisplayMode.Read) {
if (this.properties.removePadding) {
let element = this.domElement.parentElement;
@ -139,14 +141,18 @@ export default class ScriptEditorWebPart extends BaseClientSideWebPart<IScriptEd
const headTag = document.getElementsByTagName("head")[0] || document.documentElement;
const scriptTag = document.createElement("script");
scriptTag.type = "text/javascript";
if (elem.src && elem.src.length > 0) {
return;
}
if (elem.onload && elem.onload.length > 0) {
scriptTag.onload = elem.onload;
for (let i = 0; i < elem.attributes.length; i++) {
const attr = elem.attributes[i];
// Copies all attributes in case of loaded script relies on the tag attributes
if(attr.name.toLowerCase() === "onload" ) continue; // onload handled after loading with SPComponentLoader
scriptTag.setAttribute(attr.name, attr.value);
}
// set a bogus type to avoid browser loading the script, as it's loaded with SPComponentLoader
scriptTag.type = (scriptTag.src && scriptTag.src.length) > 0 ? "pnp" : "text/javascript";
// Ensure proper setting and adding id used in cleanup on reload
scriptTag.setAttribute("pnpname", this._unqiueId);
try {
// doesn't work on ie...
scriptTag.appendChild(document.createTextNode(data));
@ -156,7 +162,6 @@ export default class ScriptEditorWebPart extends BaseClientSideWebPart<IScriptEd
}
headTag.insertBefore(scriptTag, headTag.firstChild);
headTag.removeChild(scriptTag);
}
private nodeName(elem, name) {
@ -168,7 +173,15 @@ export default class ScriptEditorWebPart extends BaseClientSideWebPart<IScriptEd
//
// Argument element is an element in the dom.
private async executeScript(element: HTMLElement) {
// Define global name to tack scripts on in case script to be loaded is not AMD/UMD
// clean up added script tags in case of smart re-load
const headTag = document.getElementsByTagName("head")[0] || document.documentElement;
let scriptTags = headTag.getElementsByTagName("script");
for (let i = 0; i < scriptTags.length; i++) {
const scriptTag = scriptTags[i];
if(scriptTag.hasAttribute("pnpname") && scriptTag.attributes["pnpname"].value == this._unqiueId ) {
headTag.removeChild(scriptTag);
}
}
if (this.properties.spPageContextInfo && !window["_spPageContextInfo"]) {
window["_spPageContextInfo"] = this.context.pageContext.legacyPageContext;
@ -178,6 +191,7 @@ export default class ScriptEditorWebPart extends BaseClientSideWebPart<IScriptEd
window["_teamsContexInfo"] = this.context.sdks.microsoftTeams.context;
}
// Define global name to tack scripts on in case script to be loaded is not AMD/UMD
(<any>window).ScriptGlobal = {};
// main section of function
@ -210,10 +224,10 @@ export default class ScriptEditorWebPart extends BaseClientSideWebPart<IScriptEd
window["define"].amd = null;
}
for (let i = 0; i < urls.length; i++) {
try {
let scriptUrl = urls[i];
// Add unique param to force load on each run to overcome smart navigation in the browser as needed
const prefix = scriptUrl.indexOf('?') === -1 ? '?' : '&';
scriptUrl += prefix + 'pnp=' + new Date().getTime();
await SPComponentLoader.loadScript(scriptUrl, { globalExportsName: "ScriptGlobal" });