From a6180ba120a044b42618af7952d39d1dba432a5f Mon Sep 17 00:00:00 2001 From: David Carley Date: Tue, 2 Mar 2021 15:29:22 -0800 Subject: [PATCH] Better support for large gcode files --- src/js/gcode-viewer.js | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/js/gcode-viewer.js b/src/js/gcode-viewer.js index 658382c..633a8d5 100644 --- a/src/js/gcode-viewer.js +++ b/src/js/gcode-viewer.js @@ -80,33 +80,29 @@ module.exports = { methods: { - load: function (file) { + load: async function(file) { if (file == this.file) return; this.clear(); this.file = file; if (!file) return; - var xhr = new XMLHttpRequest(); - xhr.open('GET', '/api/file/' + file + '?' + Math.random(), true); - xhr.responseType = 'text'; + const response = await fetch(`/api/file/${file}?${Math.random()}`); + const text = await response.text(); - xhr.onload = function (e) { - if (this.file != file) return; - var lines = escapeHTML(xhr.response.trimRight()).split(/\r?\n/); - - for (var i = 0; i < lines.length; i++) { - lines[i] = '
  • ' + - '' + (i + 1) + '' + lines[i] + '
  • '; - } + if (text.length > 20e6) { + this.clusterize.update(['File is large - gcode view disabled']); + } else { + const lines = escapeHTML(text.trimRight()) + .split(/[\r\n]/) + .map((line, i) => `
  • ${i + 1}${line}
  • `); this.clusterize.update(lines); - this.empty = false; + } - Vue.nextTick(this.update_line); - }.bind(this) + this.empty = false; - xhr.send(); + Vue.nextTick(this.update_line); },