<commit_before>import Vue from 'vue'
import Sortable from 'sortablejs'
import { Storage, sleep } from './modules/utils'
Vue.config.productionTip = false
Vue.config.devtools = false

var app = new Vue({
  el: '#app',
  data: {
    apiData: null,
    currentId: null,
    currentItem: null,
		showModal: false,
  },
  methods: {
    getApiData() {
      chrome.storage.sync.get({
        apiData: null, // 设置默认值，不需要也可用数组
      }, (items) => {
        if (items.apiData) {
          // console.log('get', items.apiData)
          this.apiData = items.apiData
        }
      });
    },
    clearStorage() {
      this.apiData = null
      chrome.storage.sync.set({
        apiData: null, // 设置默认值，不需要也可用数组
      }, () => {
        alert('清除成功')
      });
    },
    saveItemForm() {
      this.apiData[this.currentId] = this.currentItem
      chrome.storage.sync.set({ apiData: this.apiData })
      this.showModal = false
    },
    addItem() {
      this.currentId = this.apiData.length
      this.currentItem = {
        "type": "html",
        "name": null,
        "icon": null,
        "url": null,
        "selectors": {
          "url": null
        },
        "isShow": true
      }
      this.showModal = true
    },
    deleteItem(index) {
      if (confirm(`确认删除${this.apiData[index].name}？`)) {
        delete this.apiData[index]
        chrome.storage.sync.set({ apiData: this.apiData })
      }
    },
    showItem(item, index) {
      this.showModal = true
      this.currentId = index
      this.currentItem = item
    }
  },
  computed: {
  },
  mounted () {
    this.getApiData()
    const self = this
		const el = document.getElementById('items')
    const sortable = Sortable.create(el, {
      store: {
        get() {
          return []
        },
        set(sortable) {
          const order = sortable.toArray();
          let newApiData = []
          order.forEach(key => newApiData.push(self.apiData[key]))
          chrome.storage.sync.set({ apiData: newApiData })
          console.log(newApiData)
        }
      },
      onEnd(evt) {
        console.log(evt, self.apiData)
        this.save()
      }
    })
  },
  render (h) { // <-- h must be in scope
    return (
      <div class="container">
        <div class="navigation">
          <h1>V-Read</h1>
          <ul class="menu">
            <li class="selected">我的订阅</li>
          </ul>
        </div>
        <div class="mainView">
          <h1>我的订阅</h1>
          <div class="controls">
            <button class="add" onClick={this.addItem.bind(this)}>添加订阅</button>
            <button>导入</button>
            <button>导出</button>
            <button onClick={this.clearStorage.bind(this)}>清空存储</button>
          </div>
          <div class="content">
            <ul id="items" class="items">
              {
                this.apiData && this.apiData.map((item, index) => {
                  return (<commit_msg>fix 删除<commit_after>import Vue from 'vue'
import Sortable from 'sortablejs'
import { Storage, sleep } from './modules/utils'
Vue.config.productionTip = false
Vue.config.devtools = false

let sortable
var app = new Vue({
  el: '#app',
  data: {
    apiData: null,
    currentId: null,
    currentItem: null,
		showModal: false,
  },
  methods: {
    getApiData() {
      chrome.storage.sync.get({
        apiData: null, // 设置默认值，不需要也可用数组
      }, (items) => {
        if (items.apiData) {
          // console.log('get', items.apiData)
          this.apiData = items.apiData
        }
      });
    },
    clearStorage() {
      if (!confirm('确认清除')) return
      this.apiData = null
      chrome.storage.sync.set({ apiData: null })
    },
    saveItemForm() {
      this.apiData[this.currentId] = this.currentItem
      chrome.storage.sync.set({ apiData: this.apiData })
      this.showModal = false
    },
    addItem() {
      this.currentId = this.apiData.length
      this.currentItem = {
        "type": "html",
        "name": null,
        "icon": null,
        "url": null,
        "selectors": {
          "url": null
        },
        "isShow": true
      }
      this.showModal = true
    },
    deleteItem(index) {
      if (!confirm(`确认删除${this.apiData[index].name}？`)) return
      const child = document.querySelector(`[data-id='${index}']`)
      child.parentElement.removeChild(child)
      sortable.save()
    },
    showItem(item, index) {
      this.showModal = true
      this.currentId = index
      this.currentItem = item
    }
  },
  computed: {
  },
  mounted () {
    this.getApiData()
    const self = this
		const el = document.getElementById('items')
    sortable = Sortable.create(el, {
      store: {
        get() {
          return []
        },
        set(sortable) {
          const order = sortable.toArray();
          let newApiData = []
          order.forEach(key => newApiData.push(self.apiData[key]))
          chrome.storage.sync.set({ apiData: newApiData })
          // console.log(newApiData)
        }
      },
      onSort(evt) {
        // console.log(evt, self.apiData)
        this.save()
      }
    })
  },
  render (h) { // <-- h must be in scope
    return (
      <div class="container">
        <div class="navigation">
          <h1>V-Read</h1>
          <ul class="menu">
            <li class="selected">我的订阅</li>
          </ul>
        </div>
        <div class="mainView">
          <h1>我的订阅</h1>
          <div class="controls">
            <button class="add" onClick={this.addItem.bind(this)}>添加订阅</button>
            <button>导入</button>
            <button>导出</button>
            <button onClick={this.clearStorage.bind(this)}>清空存储</button>
          </div>
          <div class="content">
            <ul id="items" class="items">
              {
                this.apiData && this.apiData.map((item, index) => {
                  return (
{'fraction_non_alphanumeric': 0.15117677375021474, 'fraction_numerical': 0.001374334306820134, 'mean_word_length': 1.8044315992292872, 'pattern_counts': {'":': 14, '<': 53, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1631159', 'n_tokens_mistral': 1903, 'n_tokens_neox': 1928, 'n_words': 340}