{"action":"closed","issue":{"url":"https://api.github.com/repos/ianstormtaylor/slate/issues/1255","repository_url":"https://api.github.com/repos/ianstormtaylor/slate","labels_url":"https://api.github.com/repos/ianstormtaylor/slate/issues/1255/labels{/name}","comments_url":"https://api.github.com/repos/ianstormtaylor/slate/issues/1255/comments","events_url":"https://api.github.com/repos/ianstormtaylor/slate/issues/1255/events","html_url":"https://github.com/ianstormtaylor/slate/issues/1255","id":266410838,"number":1255,"title":"consider reordering event handlers to `(change, event, editor)`","user":{"login":"ianstormtaylor","id":311752,"avatar_url":"https://avatars0.githubusercontent.com/u/311752?v=4","gravatar_id":"","url":"https://api.github.com/users/ianstormtaylor","html_url":"https://github.com/ianstormtaylor","followers_url":"https://api.github.com/users/ianstormtaylor/followers","following_url":"https://api.github.com/users/ianstormtaylor/following{/other_user}","gists_url":"https://api.github.com/users/ianstormtaylor/gists{/gist_id}","starred_url":"https://api.github.com/users/ianstormtaylor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ianstormtaylor/subscriptions","organizations_url":"https://api.github.com/users/ianstormtaylor/orgs","repos_url":"https://api.github.com/users/ianstormtaylor/repos","events_url":"https://api.github.com/users/ianstormtaylor/events{/privacy}","received_events_url":"https://api.github.com/users/ianstormtaylor/received_events","type":"User","site_admin":false},"labels":[{"id":412790240,"url":"https://api.github.com/repos/ianstormtaylor/slate/labels/idea","name":"idea","color":"eeeeee","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2017-10-18T08:53:15Z","updated_at":"2017-10-18T16:27:16Z","closed_at":"2017-10-18T16:27:16Z","author_association":"OWNER","body":"This is a probably controversial idea I'm having late at night 😄  so take it with a grain of salt—I'd love to hear your feedback.\r\n\r\n---\r\n\r\nI've been noticing more and more that the `(change, ...args)` function signature is extremely powerful. For example, if you define your own custom change functions with the same pattern that core does, with `(change, ...args)`, like so:\r\n\r\n```js\r\nfunction insertImage(change, src) {\r\n  change.insertBlock({\r\n    type: 'image',\r\n    isVoid: true,\r\n    data: { src },\r\n  })\r\n}\r\n```\r\n\r\nWith a [small tweak](https://github.com/ianstormtaylor/slate/commit/d66e127e7df9607e0dd5d46d3f23df8a27e45acd#diff-0f1805ffc056f7c54e6a6f98ba0259a1R260) I just made to the `editor.change()` method, you can call them super-tersely as a one-liner now:\r\n\r\n```js\r\neditor.change(insertImage, 'https://google.com/logo')\r\n```\r\n\r\nWhich makes writing event handlers in custom components even easier.\r\n\r\nA similar thing could be done with the current Slate-specific event handlers in the plugins stack. Right now the logic to invoke them is:\r\n\r\n```js\r\n  onEvent = (handler, ...args) => {\r\n    const { stack, state } = this.state\r\n    const change = state.change()\r\n    stack.handle(handler, change, this, ...args)\r\n    this.onChange(change)\r\n  }\r\n```\r\n\r\nBut if the handlers took `(change, event, editor)` instead, it could be simplified to:\r\n\r\n```js\r\n  onEvent = (handler, event) => {\r\n    const { stack } = this.state\r\n    const fn = stack.getHandler(handler)\r\n    this.change(fn, event, this)\r\n  }\r\n```\r\n\r\nNow I'm not saying that's worth the change, because it's just a few lines in core. But I'm just writing this down to note that when function signatures line up like that, there are some potentially nice rewards to be reaped. So just a thing to keep in mind!\r\n\r\nFor example, this would mean that `onChange()` and `onKeyDown()` would be parallel now, both taking `(change, ...args)`.\r\n\r\nIt would also mean that you do could things inside a plugin like:\r\n\r\n```js\r\nreturn {\r\n  onDrop: insertImage,\r\n  onPaste: insertImage,\r\n}\r\n```\r\n\r\nThat said, it would come at the cost of not being parallel to React's event handlers anymore. So if there is more benefit to maintaining that parallel—from reusable community utils for instance—then it wouldn't be worth it."}}
{"actor":{"display_login":"ianstormtaylor"}}