<commit_before>import Vue from 'vue'
import paper from 'paper'

export default {
  resetAnnotationState: state => {
    // Vuex state
    Vue.set(state, 'project', {
      name: 'An AIDA project',
      layers: []
    })

    // Remove paperJS project instance
    paper.project.remove()
  },

  // Refresh the Vuex store to incorporate all of the paperJS annotation items
  refreshAnnotationState: (state, payload) => {
    // Construct the layer structure fresh in the Vuex state
    paper.project.layers.forEach(layer => {
      if (layer.name !== 'guide') {
        Vue.set(state.project.layers, [layer.index], {
          name: state.project.layers[layer.index] ? state.project.layers[layer.index].name : layer.name,
          opacity: layer.opacity,
          items: []
        })
      }
    })

    // Gather all the Path items in the paperJS environment and store them in
    // the state appropriately. Convention follows AIDA annotation schema:
    // https://aida.gitbook.io/docs/annotation-schema
    paper.project
      .getItems({
        className: 'Path'
      })
      .forEach(item => {
        if (item.data.type === 'circle') {
          state.project.layers[item.layer.index].items.push({
            class: item.data.class,
            type: 'circle',
            center: {
              x: item.position.x,
              y: item.position.y
            },
            radius: item.bounds.width / 2
          })
        } else if (item.data.type === 'rectangle') {
          state.project.layers[item.layer.index].items.push({
            class: item.data.class,
            type: 'rectangle',
            from: {
              x: item.bounds.topLeft.x,
              y: item.bounds.topRight.y
            },
            to: {
              x: item.bounds.bottomRight.x,
              y: item.bounds.bottomRight.y
            }
          })
        } else if (item.data.type === 'path') {
          state.project.layers[item.layer.index].items.push({
            class: item.data.class,
            type: 'path',
            segments: getSegments(item),
            closed: item.closed
          })
        }
      })

    // Function that, given a paperJS path item, return the segments in the
    // format specified by the AIDA annotation schema
    function getSegments(item) {
      let segments = []
      item.segments.forEach(segment => {
        segments.push({
          point: {
            x: segment.point.x,
            y: segment.point.y
          },
          handleIn: {
            x: segment.handleIn.x,
            y: segment.handleIn.y
          },
          handleOut: {
            x: segment.handleOut.x,
            y: segment.handleOut.y
          }
        })
      })

      return segments
    }
  },

  // Setup the PaperJs instance on the canvas DOM element.
  setupAnnotation: (state, canvas) => {
    paper.setup(canvas)
  },

  // Load annotation data into both the state and the paperJS environment.
  // For legacy purposes handle both a string of annotation output as created by
  // PaperJS but also the new AIDA annotation schema.
  loadAidaAnnotation: (state, payload) => {
    // Save the loaded annotation data to the Vuex state
    state.project = payload.annotation

    // Cycle through the layers
    state.project.layers.forEach(layer => {
      // Create a new layer
      let newPaperLayer = new paper.Layer()
      newPaperLayer.opacity = layer.opacity

      // Draw each item
      if (layer.items) {
        layer.items.forEach(item => {
          let newPaperItem
          if (item.type === 'circle') {
            newPaperItem = new paper.Path.Circle({
              center: item.center,
              radius: item.radius,
              data: {
                type: 'circle',
                countable: true,
                class: item.class
              }
            })
          } else if (item.type === 'rectangle') {
            newPaperItem = new paper.Path.Rectangle({
              from: item.from,
              to: item.to,
              data: {
                type: 'rectangle',
                class: item.class
              }
            })
          } else {
            newPaperItem = new paper.Path({
              segments: item.segments,
              closed: item.closed,
              data: {
                type: 'path',
                class: item.class
              }
            })
          }

          // Set the path colors to the default for their layer.
          if (newPaperItem.closed) {
            newPaperItem.fillColor =
              state.defaultColors[newPaperItem.layer.index].fill
          }
          newPaperItem.strokeColor =
            state.defaultColors[newPaperItem.layer.index].stroke<commit_msg>Save and load specific item colors<commit_after>import Vue from 'vue'
import paper from 'paper'

import helpers from './helpers.js'

export default {
  resetAnnotationState: state => {
    // Vuex state
    Vue.set(state, 'project', {
      name: 'An AIDA project',
      layers: []
    })

    // Remove paperJS project instance
    paper.project.remove()
  },

  // Refresh the Vuex store to incorporate all of the paperJS annotation items
  refreshAnnotationState: (state, payload) => {
    // Construct the layer structure fresh in the Vuex state
    paper.project.layers.forEach(layer => {
      if (layer.name !== 'guide') {
        Vue.set(state.project.layers, [layer.index], {
          name: state.project.layers[layer.index] ? state.project.layers[layer.index].name : layer.name,
          opacity: layer.opacity,
          items: []
        })
      }
    })

    // Gather all the Path items in the paperJS environment and store them in
    // the state appropriately. Convention follows AIDA annotation schema:
    // https://aida.gitbook.io/docs/annotation-schema
    paper.project
      .getItems({
        className: 'Path'
      })
      .forEach(item => {
        if (item.data.type === 'circle') {
          state.project.layers[item.layer.index].items.push({
            class: item.data.class,
            type: 'circle',
            color: helpers.getColor(item),
            center: {
              x: item.position.x,
              y: item.position.y
            },
            radius: item.bounds.width / 2
          })
        } else if (item.data.type === 'rectangle') {
          state.project.layers[item.layer.index].items.push({
            class: item.data.class,
            type: 'rectangle',
            color: helpers.getColor(item),
            from: {
              x: item.bounds.topLeft.x,
              y: item.bounds.topRight.y
            },
            to: {
              x: item.bounds.bottomRight.x,
              y: item.bounds.bottomRight.y
            }
          })
        } else if (item.data.type === 'path') {
          state.project.layers[item.layer.index].items.push({
            class: item.data.class,
            type: 'path',
            color: helpers.getColor(item),
            segments: helpers.getSegments(item),
            closed: item.closed
          })
        }
      })
  },

  // Setup the PaperJs instance on the canvas DOM element.
  setupAnnotation: (state, canvas) => {
    paper.setup(canvas)
  },

  // Load annotation data into both the state and the paperJS environment.
  // For legacy purposes handle both a string of annotation output as created by
  // PaperJS but also the new AIDA annotation schema.
  loadAidaAnnotation: (state, payload) => {
    // Save the loaded annotation data to the Vuex state
    state.project = payload.annotation

    // Cycle through the layers
    state.project.layers.forEach(layer => {
      // Create a new layer
      let newPaperLayer = new paper.Layer()
      newPaperLayer.opacity = layer.opacity

      // Draw each item
      if (layer.items) {
        layer.items.forEach(item => {
          let newPaperItem
          if (item.type === 'circle') {
            newPaperItem = new paper.Path.Circle({
              center: item.center,
              radius: item.radius,
              data: {
                type: 'circle',
                countable: true,
                class: item.class
              }
            })
          } else if (item.type === 'rectangle') {
            newPaperItem = new paper.Path.Rectangle({
              from: item.from,
              to: item.to,
              data: {
                type: 'rectangle',
                class: item.class
              }
            })
          } else {
            newPaperItem = new paper.Path({
              segments: item.segments,
              closed: item.closed,
              data: {
                type: 'path',
                class: item.class
              }
            })
          }

          // Set the path colors to the default for their layer.
          if (newPaperItem.closed) {
            if (item.color) {
              newPaperItem.fillColor = new paper.Color({
                hue: item.color.fill.hue,
                saturation: item.color.fill.saturation,
                lightness: item.color.fill.lightness,
                alpha: item.color.fill.alpha
              })
            } else {
              newPaperItem.fillColor = state.defaultColors[newPaperItem.layer.index % state.defaultColors.length]
              newPaperItem.fillColor.alpha = 0.7
            }
          }
          if (item.color) {
            newPaperItem.strokeColor = new paper.Color({
              hue: item.color.stroke.hue,
              saturation: item.color.stroke.saturation,
              lightness: item.color.stroke.lightness,
              alpha: item.color.stroke.alpha
            })
          } else {
            newPaperItem.strokeColor = state.defaultColors[newPaperItem.layer.index % state.defaultColors.length]
          }
{'fraction_non_alphanumeric': 0.10817160367722166, 'fraction_numerical': 0.00040858018386108274, 'mean_word_length': 1.5358715358715358, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, 'https://': 2, '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': '7047098', 'n_tokens_mistral': 2691, 'n_tokens_neox': 2612, 'n_words': 652}