Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disabled Data functionality #457

Closed
konki-vienna opened this issue Apr 10, 2019 · 8 comments · Fixed by #459
Closed

Disabled Data functionality #457

konki-vienna opened this issue Apr 10, 2019 · 8 comments · Fixed by #459
Assignees
Labels
api Issues that are impacting the API itself bug Confirmed bugs or reports that are very likely to be bugs

Comments

@konki-vienna
Copy link

When adding require('sketch/dom').getSelectedDocument() to the code of a Data Plugin, the Data Plugin part of that plugin gets disabled (through Sketch) without the possibility to enable it again (through the user).

Environment: Sketch Version 54, Mac OS Mojave (v. 10.14.3)
How to reproduce:

  1. Create a Sketch Data Plugin
  2. Add the code let document = require('sketch/dom').getSelectedDocument() to the data related JS file.
  3. Build the plugin
  4. Open Sketch
  5. Go to Sketch > Preferences > Data and scroll to the specific Data Plugin line - the plugin is disbaled (see screenshot attached).
    Bildschirmfoto 2019-04-10 um 10 42 51
@mathieudutour
Copy link
Contributor

Where exactly are you adding it?

@konki-vienna
Copy link
Author

konki-vienna commented Apr 10, 2019

I was adding it outside onStartup():

const sketch = require('sketch')
const { DataSupplier } = sketch
const util = require('util')

let document = require('sketch/dom').getSelectedDocument()

export function onStartup () {
  // To register the plugin, uncomment the relevant type:
  DataSupplier.registerDataSupplier('public.text', 'data_test', 'SupplyData')
  // DataSupplier.registerDataSupplier('public.image', 'data_test', 'SupplyData')
}

export function onShutdown () {
  // Deregister the plugin
  DataSupplier.deregisterDataSuppliers()
}

export function onSupplyData (context) {
  let dataKey = context.data.key
  const items = util.toArray(context.data.items).map(sketch.fromNative)
  items.forEach((item, index) => {
    let data = Math.random().toString()
    DataSupplier.supplyDataAtIndex(dataKey, data, index)
  })
}

but when I add it to/inside onStartup() everything seems to be fine. 🤦‍♂️
Probably this is the right place to put it, right?

const sketch = require('sketch')
const { DataSupplier } = sketch
const util = require('util')

export function onStartup () {
  let document = require('sketch/dom').getSelectedDocument()
  // To register the plugin, uncomment the relevant type:
  DataSupplier.registerDataSupplier('public.text', 'data_test', 'SupplyData')
  // DataSupplier.registerDataSupplier('public.image', 'data_test', 'SupplyData')
}

export function onShutdown () {
  // Deregister the plugin
  DataSupplier.deregisterDataSuppliers()
}

export function onSupplyData (context) {
  let dataKey = context.data.key
  const items = util.toArray(context.data.items).map(sketch.fromNative)
  items.forEach((item, index) => {
    let data = Math.random().toString()
    DataSupplier.supplyDataAtIndex(dataKey, data, index)
  })
}

Or do I have to add it to onSupplyData?

@mathieudutour
Copy link
Contributor

mathieudutour commented Apr 10, 2019

Yeah it might do something funky outside onStartup because you don’t know when it will be run.
Even onStartup might not have a document.

@konki-vienna
Copy link
Author

So better put it inside onSupplyData?

@mathieudutour
Copy link
Contributor

Yeah, better put it in the function where you need it :)

@konki-vienna
Copy link
Author

Yes, thank you!
PS.: I guess you (and not me) close the issue then - I do not know what the protocol here is...

@konki-vienna
Copy link
Author

Ah, one last question: Would there have been any way to get some debug-info here (regarding the disabled Data-plugin part). I was looking into the MacOs Console (filtered for Sketch), but there is so much going on when Sketch starts that I did not see anything which looked connected...

@mathieudutour
Copy link
Contributor

It's actually a bug that getSelectedDocument throws an error when there is no document so I'll fix that!

@mathieudutour mathieudutour added api Issues that are impacting the API itself bug Confirmed bugs or reports that are very likely to be bugs and removed status: work in progress labels Apr 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Issues that are impacting the API itself bug Confirmed bugs or reports that are very likely to be bugs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants