Skip to content

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
tortmayr committed Nov 27, 2023
1 parent 7d8601a commit c3a4bf6
Showing 1 changed file with 55 additions and 55 deletions.
110 changes: 55 additions & 55 deletions packages/plugin-ext/src/main/browser/webview/pre/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,63 +146,63 @@
*/
function getDefaultScript(state) {
return `
const acquireVsCodeApi = (function() {
const originalPostMessage = window.parent.postMessage.bind(window.parent);
const originalConsole = {...console};
const targetOrigin = '*';
let acquired = false;
let state = ${state ? `JSON.parse(${JSON.stringify(state)})` : undefined};
const forwardConsoleLog= (level,msg,args)=> {
let message, optionalParams;
try{
if (msg) {
message= JSON.stringify(msg) ?? null;
}
if (args){
optionalParams= JSON.stringify(args) ?? null;
}
} catch(e){
// Log non serializable objects inside of view
originalConsole[level](msg,args);
return;
}
originalPostMessage({ command: 'onconsole', data: { level, message,optionalParams} }, targetOrigin);
};
const acquireVsCodeApi = (function() {
const originalPostMessage = window.parent.postMessage.bind(window.parent);
const originalConsole = {...console};
const targetOrigin = '*';
let acquired = false;
let state = ${state ? `JSON.parse(${JSON.stringify(state)})` : undefined};
const forwardConsoleLog = (level, msg, args) => {
let message, optionalParams;
try {
if (msg) {
message = JSON.stringify(msg) ?? null;
}
if (args) {
optionalParams = JSON.stringify(args) ?? null;
}
} catch (e) {
// Log non serializable objects inside of view
originalConsole[level](msg, args);
return;
}
originalPostMessage({ command: 'onconsole', data: { level, message, optionalParams } }, targetOrigin);
};
console.log= (message,args)=> forwardConsoleLog('log',message,args);
console.info= (message,args)=> forwardConsoleLog('info',message,args);
console.warn= (message,args)=> forwardConsoleLog('warn',message,args);
console.error= (message,args)=> forwardConsoleLog('error',message,args);
console.debug= (message,args)=> forwardConsoleLog('debug',message,args);
console.trace= (message,args)=> forwardConsoleLog('trace',message,args);
console.log = (message, args) => forwardConsoleLog('log', message, args);
console.info = (message, args) => forwardConsoleLog('info', message, args);
console.warn = (message, args) => forwardConsoleLog('warn', message, args);
console.error = (message, args) => forwardConsoleLog('error', message, args);
console.debug = (message, args) => forwardConsoleLog('debug', message, args);
console.trace = (message, args) => forwardConsoleLog('trace', message, args);
return () => {
if (acquired) {
throw new Error('An instance of the VS Code API has already been acquired');
}
acquired = true;
return Object.freeze({
postMessage: function(msg) {
return originalPostMessage({ command: 'onmessage', data: msg }, targetOrigin);
},
setState: function(newState) {
state = newState;
originalPostMessage({ command: 'do-update-state', data: JSON.stringify(newState) }, targetOrigin);
return newState;
},
getState: function() {
return state;
}
});
};
})();
const acquireTheiaApi = acquireVsCodeApi;
delete window.parent;
delete window.top;
delete window.frameElement;
`;
return () => {
if (acquired) {
throw new Error('An instance of the VS Code API has already been acquired');
}
acquired = true;
return Object.freeze({
postMessage: function (msg) {
return originalPostMessage({ command: 'onmessage', data: msg }, targetOrigin);
},
setState: function (newState) {
state = newState;
originalPostMessage({ command: 'do-update-state', data: JSON.stringify(newState) }, targetOrigin);
return newState;
},
getState: function () {
return state;
}
});
};
})();
const acquireTheiaApi = acquireVsCodeApi;
delete window.parent;
delete window.top;
delete window.frameElement;
`;
}

/**
Expand Down

0 comments on commit c3a4bf6

Please sign in to comment.