{"action":"created","comment":{"url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/pulls/comments/44714641","id":44714641,"diff_hunk":"@@ -0,0 +1,194 @@\n+// Copyright (c) Jupyter Development Team.\n+// Distributed under the terms of the Modified BSD License.\n+'use strict';\n+\n+import * as CodeMirror from 'codemirror';\n+\n+import 'codemirror/lib/codemirror.css';\n+\n+import 'codemirror/mode/meta';\n+\n+import * as dmp from 'diff-match-patch';\n+\n+import {\n+  Message\n+} from 'phosphor-messaging';\n+\n+import {\n+  IChangedArgs\n+} from 'phosphor-properties';\n+\n+import {\n+  ISignal, Signal\n+} from 'phosphor-signaling';\n+\n+import {\n+  ResizeMessage, Widget\n+} from 'phosphor-widget';\n+\n+import {\n+  IInputAreaViewModel, ITextEditorViewModel\n+} from './InputAreaViewModel';\n+\n+\n+let diffMatchPatch = new dmp.diff_match_patch()\n+\n+\n+/**\n+ * A widget which hosts a CodeMirror editor.\n+ */\n+export\n+class CodeMirrorWidget extends Widget {\n+\n+  /**\n+   * Construct a CodeMirror widget.\n+   */\n+  constructor(model: ITextEditorViewModel) {\n+    super();\n+    this.addClass('CodeMirrorWidget');\n+    this._model = model;\n+    this._editor = CodeMirror(this.node);\n+    this.updateMimetype(model.mimetype);\n+    this.updateLineNumbers(model.lineNumbers);\n+    this.updateFixedHeight(model.fixedHeight);\n+    this.updateText(model.text);\n+\n+    this._editor.on('change', (instance, change) => {\n+      this._model.text = this._editor.getDoc().getValue();\n+    });\n+    model.stateChanged.connect(this._modelUpdate, this);\n+  }\n+\n+  /**\n+   * Update the fixedHeight.\n+   */\n+  updateFixedHeight(fixedHeight: boolean) {\n+    if (fixedHeight) {\n+      this.addClass('CodeMirrorWidget-fixedHeight');\n+    } else {\n+      this.removeClass('CodeMirrorWidget-fixedHeight');\n+    }\n+  }\n+\n+  /**\n+   * Update the text in the widget.\n+   *\n+   * #### Notes\n+   * This function attempts to restore the cursor to the correct\n+   * place by using the bitap algorithm to find the corresponding\n+   * position of the cursor in the new text.\n+   */\n+  updateText(text: string) {\n+    if (this.text !== text) {\n+      // TODO: do something smart with all the selections\n+\n+      let doc = this._editor.getDoc();\n+      let oldText = doc.getValue();\n+      let oldCursor = doc.indexFromPos(doc.getCursor());\n+      let cursor = 0;\n+      if (oldCursor === oldText.length) {\n+        // if the cursor was at the end, keep it at the end\n+        cursor = text.length;\n+      } else {\n+        let fragment = oldText.substr(oldCursor, 10);\n+        cursor = diffMatchPatch.match_main(text, fragment, oldCursor);\n+      }\n+\n+      doc.setValue(text);\n+      doc.setCursor(doc.posFromIndex(cursor));\n+    }\n+  }\n+\n+  /**\n+   * Set the mode by giving the mimetype.\n+   *\n+   * #### Notes\n+   * Valid mimetypes are listed in https://github.com/codemirror/CodeMirror/blob/master/mode/meta.js.\n+   */\n+  updateMimetype(mimetype: string) {\n+    if (CodeMirror.mimeModes.hasOwnProperty(mimetype)) {\n+      this._editor.setOption('mode', mimetype);\n+    } else {\n+      let info = CodeMirror.findModeByMIME(mimetype);\n+      if (info) {\n+        this._loadCodeMirrorMode(info.mode).then(() => {\n+          this._editor.setOption('mode', mimetype);\n+        })\n+      }\n+    }\n+  }\n+\n+  /**\n+   * Update the line numbers in the editing widget.\n+   */\n+  updateLineNumbers(lineNumbers: boolean) {\n+    this._editor.setOption('lineNumbers', lineNumbers);\n+  }\n+\n+  /**\n+   * Get the text in the widget.\n+   */\n+  get text(): string {\n+    return this._editor.getDoc().getValue();\n+  }\n+\n+  /**\n+   * Set the text in the widget.\n+   */\n+  set text(text: string) {\n+    this.updateText(text)\n+  }\n+\n+  /**\n+   * Handle afterAttach messages.\n+   */\n+  protected onAfterAttach(msg: Message): void {\n+    this._editor.refresh();\n+  }\n+\n+  /**\n+   * Handle resize messages.\n+   */\n+  protected onResize(msg: ResizeMessage): void {\n+    if (msg.width < 0 || msg.height < 0) {\n+      this._editor.refresh();\n+    } else {\n+      this._editor.setSize(msg.width, msg.height);\n+    }\n+  }\n+\n+  /**\n+   * Change handler for model updates.\n+   */\n+  private _modelUpdate(sender: ITextEditorViewModel, args: IChangedArgs<any>) {\n+    switch(args.name) {\n+      case 'fixedHeight':","path":"src/InputAreaWidget.ts","position":165,"original_position":165,"commit_id":"edb63b7c8499a54a00e84b67d4cb090f2501ff01","original_commit_id":"cc910595e0d8916ef1cf54d3c386953b1b48ac1a","user":{"login":"blink1073","id":2096628,"avatar_url":"https://avatars.githubusercontent.com/u/2096628?v=3","gravatar_id":"","url":"https://api.github.com/users/blink1073","html_url":"https://github.com/blink1073","followers_url":"https://api.github.com/users/blink1073/followers","following_url":"https://api.github.com/users/blink1073/following{/other_user}","gists_url":"https://api.github.com/users/blink1073/gists{/gist_id}","starred_url":"https://api.github.com/users/blink1073/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/blink1073/subscriptions","organizations_url":"https://api.github.com/users/blink1073/orgs","repos_url":"https://api.github.com/users/blink1073/repos","events_url":"https://api.github.com/users/blink1073/events{/privacy}","received_events_url":"https://api.github.com/users/blink1073/received_events","type":"User","site_admin":false},"body":"Fair enough, this was a Phosphor convention.","created_at":"2015-11-12T21:07:59Z","updated_at":"2015-11-12T21:07:59Z","html_url":"https://github.com/jupyter/jupyter-js-input-area/pull/4#discussion_r44714641","pull_request_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/pulls/4","_links":{"self":{"href":"https://api.github.com/repos/jupyter/jupyter-js-input-area/pulls/comments/44714641"},"html":{"href":"https://github.com/jupyter/jupyter-js-input-area/pull/4#discussion_r44714641"},"pull_request":{"href":"https://api.github.com/repos/jupyter/jupyter-js-input-area/pulls/4"}}},"pull_request":{"url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/pulls/4","id":50028960,"html_url":"https://github.com/jupyter/jupyter-js-input-area/pull/4","diff_url":"https://github.com/jupyter/jupyter-js-input-area/pull/4.diff","patch_url":"https://github.com/jupyter/jupyter-js-input-area/pull/4.patch","issue_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/issues/4","number":4,"state":"open","locked":false,"title":"WIP Viewmodel phosphor widget implementation","user":{"login":"jasongrout","id":192614,"avatar_url":"https://avatars.githubusercontent.com/u/192614?v=3","gravatar_id":"","url":"https://api.github.com/users/jasongrout","html_url":"https://github.com/jasongrout","followers_url":"https://api.github.com/users/jasongrout/followers","following_url":"https://api.github.com/users/jasongrout/following{/other_user}","gists_url":"https://api.github.com/users/jasongrout/gists{/gist_id}","starred_url":"https://api.github.com/users/jasongrout/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jasongrout/subscriptions","organizations_url":"https://api.github.com/users/jasongrout/orgs","repos_url":"https://api.github.com/users/jasongrout/repos","events_url":"https://api.github.com/users/jasongrout/events{/privacy}","received_events_url":"https://api.github.com/users/jasongrout/received_events","type":"User","site_admin":false},"body":"This implementation follows design discussions about designing the models.","created_at":"2015-11-07T05:26:33Z","updated_at":"2015-11-12T21:07:59Z","closed_at":null,"merged_at":null,"merge_commit_sha":"caedf06e4052bcd78d4d14bd82c9ee1b6cab5f2f","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/pulls/4/commits","review_comments_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/pulls/4/comments","review_comment_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/pulls/comments{/number}","comments_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/issues/4/comments","statuses_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/statuses/edb63b7c8499a54a00e84b67d4cb090f2501ff01","head":{"label":"jasongrout:viewmodel","ref":"viewmodel","sha":"edb63b7c8499a54a00e84b67d4cb090f2501ff01","user":{"login":"jasongrout","id":192614,"avatar_url":"https://avatars.githubusercontent.com/u/192614?v=3","gravatar_id":"","url":"https://api.github.com/users/jasongrout","html_url":"https://github.com/jasongrout","followers_url":"https://api.github.com/users/jasongrout/followers","following_url":"https://api.github.com/users/jasongrout/following{/other_user}","gists_url":"https://api.github.com/users/jasongrout/gists{/gist_id}","starred_url":"https://api.github.com/users/jasongrout/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jasongrout/subscriptions","organizations_url":"https://api.github.com/users/jasongrout/orgs","repos_url":"https://api.github.com/users/jasongrout/repos","events_url":"https://api.github.com/users/jasongrout/events{/privacy}","received_events_url":"https://api.github.com/users/jasongrout/received_events","type":"User","site_admin":false},"repo":{"id":45723791,"name":"jupyter-js-input-area","full_name":"jasongrout/jupyter-js-input-area","owner":{"login":"jasongrout","id":192614,"avatar_url":"https://avatars.githubusercontent.com/u/192614?v=3","gravatar_id":"","url":"https://api.github.com/users/jasongrout","html_url":"https://github.com/jasongrout","followers_url":"https://api.github.com/users/jasongrout/followers","following_url":"https://api.github.com/users/jasongrout/following{/other_user}","gists_url":"https://api.github.com/users/jasongrout/gists{/gist_id}","starred_url":"https://api.github.com/users/jasongrout/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jasongrout/subscriptions","organizations_url":"https://api.github.com/users/jasongrout/orgs","repos_url":"https://api.github.com/users/jasongrout/repos","events_url":"https://api.github.com/users/jasongrout/events{/privacy}","received_events_url":"https://api.github.com/users/jasongrout/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jasongrout/jupyter-js-input-area","description":"Javascript APIs for Jupyter input areas","fork":true,"url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area","forks_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/forks","keys_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/teams","hooks_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/hooks","issue_events_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/issues/events{/number}","events_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/events","assignees_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/assignees{/user}","branches_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/branches{/branch}","tags_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/tags","blobs_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/git/refs{/sha}","trees_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/statuses/{sha}","languages_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/languages","stargazers_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/stargazers","contributors_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/contributors","subscribers_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/subscribers","subscription_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/subscription","commits_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/commits{/sha}","git_commits_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/git/commits{/sha}","comments_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/comments{/number}","issue_comment_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/issues/comments{/number}","contents_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/contents/{+path}","compare_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/merges","archive_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/downloads","issues_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/issues{/number}","pulls_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/pulls{/number}","milestones_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/milestones{/number}","notifications_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/labels{/name}","releases_url":"https://api.github.com/repos/jasongrout/jupyter-js-input-area/releases{/id}","created_at":"2015-11-07T05:24:59Z","updated_at":"2015-11-07T05:25:00Z","pushed_at":"2015-11-12T20:17:50Z","git_url":"git://github.com/jasongrout/jupyter-js-input-area.git","ssh_url":"git@github.com:jasongrout/jupyter-js-input-area.git","clone_url":"https://github.com/jasongrout/jupyter-js-input-area.git","svn_url":"https://github.com/jasongrout/jupyter-js-input-area","homepage":null,"size":111,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"jupyter:master","ref":"master","sha":"ed650075352aa18d9c8f582486147feb139a22cd","user":{"login":"jupyter","id":7388996,"avatar_url":"https://avatars.githubusercontent.com/u/7388996?v=3","gravatar_id":"","url":"https://api.github.com/users/jupyter","html_url":"https://github.com/jupyter","followers_url":"https://api.github.com/users/jupyter/followers","following_url":"https://api.github.com/users/jupyter/following{/other_user}","gists_url":"https://api.github.com/users/jupyter/gists{/gist_id}","starred_url":"https://api.github.com/users/jupyter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jupyter/subscriptions","organizations_url":"https://api.github.com/users/jupyter/orgs","repos_url":"https://api.github.com/users/jupyter/repos","events_url":"https://api.github.com/users/jupyter/events{/privacy}","received_events_url":"https://api.github.com/users/jupyter/received_events","type":"Organization","site_admin":false},"repo":{"id":39586574,"name":"jupyter-js-input-area","full_name":"jupyter/jupyter-js-input-area","owner":{"login":"jupyter","id":7388996,"avatar_url":"https://avatars.githubusercontent.com/u/7388996?v=3","gravatar_id":"","url":"https://api.github.com/users/jupyter","html_url":"https://github.com/jupyter","followers_url":"https://api.github.com/users/jupyter/followers","following_url":"https://api.github.com/users/jupyter/following{/other_user}","gists_url":"https://api.github.com/users/jupyter/gists{/gist_id}","starred_url":"https://api.github.com/users/jupyter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jupyter/subscriptions","organizations_url":"https://api.github.com/users/jupyter/orgs","repos_url":"https://api.github.com/users/jupyter/repos","events_url":"https://api.github.com/users/jupyter/events{/privacy}","received_events_url":"https://api.github.com/users/jupyter/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/jupyter/jupyter-js-input-area","description":"Javascript APIs for Jupyter input areas","fork":false,"url":"https://api.github.com/repos/jupyter/jupyter-js-input-area","forks_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/forks","keys_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/teams","hooks_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/hooks","issue_events_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/issues/events{/number}","events_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/events","assignees_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/assignees{/user}","branches_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/branches{/branch}","tags_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/tags","blobs_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/git/refs{/sha}","trees_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/statuses/{sha}","languages_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/languages","stargazers_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/stargazers","contributors_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/contributors","subscribers_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/subscribers","subscription_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/subscription","commits_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/commits{/sha}","git_commits_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/git/commits{/sha}","comments_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/comments{/number}","issue_comment_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/issues/comments{/number}","contents_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/contents/{+path}","compare_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/merges","archive_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/downloads","issues_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/issues{/number}","pulls_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/pulls{/number}","milestones_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/milestones{/number}","notifications_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/labels{/name}","releases_url":"https://api.github.com/repos/jupyter/jupyter-js-input-area/releases{/id}","created_at":"2015-07-23T19:06:55Z","updated_at":"2015-10-12T17:06:55Z","pushed_at":"2015-11-12T20:17:51Z","git_url":"git://github.com/jupyter/jupyter-js-input-area.git","ssh_url":"git@github.com:jupyter/jupyter-js-input-area.git","clone_url":"https://github.com/jupyter/jupyter-js-input-area.git","svn_url":"https://github.com/jupyter/jupyter-js-input-area","homepage":null,"size":248,"stargazers_count":1,"watchers_count":1,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":1,"forks":4,"open_issues":1,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/jupyter/jupyter-js-input-area/pulls/4"},"html":{"href":"https://github.com/jupyter/jupyter-js-input-area/pull/4"},"issue":{"href":"https://api.github.com/repos/jupyter/jupyter-js-input-area/issues/4"},"comments":{"href":"https://api.github.com/repos/jupyter/jupyter-js-input-area/issues/4/comments"},"review_comments":{"href":"https://api.github.com/repos/jupyter/jupyter-js-input-area/pulls/4/comments"},"review_comment":{"href":"https://api.github.com/repos/jupyter/jupyter-js-input-area/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/jupyter/jupyter-js-input-area/pulls/4/commits"},"statuses":{"href":"https://api.github.com/repos/jupyter/jupyter-js-input-area/statuses/edb63b7c8499a54a00e84b67d4cb090f2501ff01"}}}}