瀏覽代碼

feat: show the first changed line when tracking

tophf 4 年之前
父節點
當前提交
ce8f618e44
共有 2 個文件被更改,包括 29 次插入1 次删除
  1. 3 0
      src/common/ui/externals.vue
  2. 26 1
      src/confirm/views/app.vue

+ 3 - 0
src/common/ui/externals.vue

@@ -106,6 +106,9 @@ export default {
       this.code = code;
       this.$set(this.deps, depsUrl, code);
     },
+    'install.code'(val) {
+      this.code = val;
+    },
     value() {
       this.$nextTick(() => {
         if (this.index >= this.all.length) this.index = 0;

+ 26 - 1
src/confirm/views/app.vue

@@ -72,6 +72,7 @@
     </div>
     <div class="frame-block flex-1 pos-rel">
       <vm-externals
+        ref="externals"
         v-if="script"
         v-model="script"
         class="abs-full"
@@ -114,6 +115,7 @@ let filePortResolve;
 /** @type {boolean} */
 let filePortNeeded;
 let basicTitle;
+let cachedCodePromise;
 
 export default {
   components: {
@@ -177,6 +179,7 @@ export default {
     /* sendCmdDirectly makes the page load so fast that ua.isFirefox is still a boolean,
        so we'll detect FF68 that stopped allowing file: scheme in fetch() via a CSS feature */
     filePortNeeded = url.startsWith('file:') && ua.isFirefox && CSS.supports('counter-set', 'none');
+    cachedCodePromise = sendCmdDirectly('CachePop', url);
     this.guard = setInterval(sendCmdDirectly, KEEP_INFO_DELAY, 'CacheHit', { key });
     await this.loadData();
     await this.parseMeta();
@@ -212,7 +215,26 @@ export default {
         ? await new Promise(this.pingFilePort)
         : await this.getScript(this.info.url);
       if (code == null || changedOnly && this.code === code) throw 0;
+      this.setCode(code);
+    },
+    setCode(code) {
+      const lines = code.split(/\r?\n/);
+      const cm = this.$refs.externals?.$refs.code?.cm;
+      let i = -1;
+      let isDiff;
+      if (cm) {
+        cm.eachLine(({ text }) => {
+          isDiff = text !== lines[i += 1];
+          return isDiff;
+        });
+      }
       this.code = code;
+      if (isDiff || cm && i < lines.length - 1) {
+        this.$nextTick(() => {
+          cm.setCursor(i);
+          cm.scrollIntoView(null, cm.display.lastWrapHeight / 3);
+        });
+      }
     },
     async parseMeta() {
       /** @type {VMScriptMeta} */
@@ -319,10 +341,12 @@ export default {
     },
     async getScript(url) {
       try {
-        return await sendCmdDirectly('CacheLoad', url) || await this.getFile(url);
+        return cachedCodePromise && await cachedCodePromise || await this.getFile(url);
       } catch (e) {
         this.message = this.i18n('msgErrorLoadingData');
         throw url;
+      } finally {
+        cachedCodePromise = null;
       }
     },
     async installScript() {
@@ -353,6 +377,7 @@ export default {
       if (this.tracking || !this.isLocal || !this.installed) {
         return;
       }
+      cachedCodePromise = null; // always re-read because the file may have changed since then
       this.tracking = true;
       while (options.get('trackLocalFile') && this.tracking !== 'stop') {
         await makePause(500);