-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuipEditor.py
432 lines (352 loc) · 12.9 KB
/
QuipEditor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import re
import time
import sublime
import sublime_plugin
import os
from sublime import Region
from .src.editor import HTMLEditor
from .src.providers import QuipProvider
from .src.managers import TREE_VIEW_TAB_ID, TabsManager, ChatView, Preview
from .src.deps.markdownify import markdownify as md
COMMAND_OPEN_DOCUMENT = "open_document"
COMMAND_OPEN_CHAT = "open_chat"
COMMAND_OPEN_PREVIEW = "open_preview"
COMMAND_SHOW_FILE_TREE = "show_file_tree"
COMMAND_PRINT_QUIP_FILE_TREE = "print_quip_file_tree"
COMMAND_INSERT_SELECTED_DOCUMENT = "insert_selected_document"
COMMAND_INSERT_CHAT_MESSAGES = "insert_chat_messages"
COMMAND_INSERT_CONTACTS = "insert_contacts"
COMMAND_INSERT_PREVIEW = "insert_preview"
COMMAND_DELETE_DOCUMENT = "delete_document"
COMMAND_CREATE_DOCUMENT = "create_document"
KEY_THREAD_ID = "thread_id"
KEY_FILE_TREE_PHANTOM_SET = "file_tree_phantom_set"
KEY_CONTACTS_PHANTOM_SET = "contacts_phantom_set"
KEY_MESSAGES_PHANTOM_SET = "messages_phantom_set"
KEY_PREVIEW_PHANTOM_SET = "preview_phantom_set"
def plugin_loaded():
global quip, manager, CACHE_DIRECTORY
quip = QuipProvider()
manager = TabsManager()
CACHE_DIRECTORY = sublime.cache_path() + "/QuipEditor"
if not os.path.exists(CACHE_DIRECTORY):
os.makedirs(CACHE_DIRECTORY)
class OpenDocumentCommand(sublime_plugin.WindowCommand):
def run(self, thread_id, markdown=False, chat=True):
self.window.run_command("set_layout", {
"cols": [0, 1.0],
"rows": [0.0, 1.0],
"cells": [[0, 0, 1, 1]]
})
view = manager.get_tab(thread_id)
if not view:
view = self.window.new_file()
manager.event_propagation = False
view.retarget(CACHE_DIRECTORY + "/" + thread_id + ".html")
view.run_command(COMMAND_INSERT_SELECTED_DOCUMENT, {"thread_id": thread_id, "markdown": markdown, "chat": chat})
view.run_command("save")
manager.add(thread_id, view)
class ShowFileTreeCommand(sublime_plugin.WindowCommand):
def run(self):
view = manager.get_tab(TREE_VIEW_TAB_ID)
if not view:
view = self.window.new_file()
manager.add(TREE_VIEW_TAB_ID, view)
self.window.focus_view(view)
view.run_command(COMMAND_PRINT_QUIP_FILE_TREE)
class InsertSelectedDocumentCommand(sublime_plugin.TextCommand):
def run(self, edit, thread_id, markdown=False, chat=True):
html = quip.get_document_content(thread_id)
self.view.replace(edit, Region(0, self.view.size()), md(html) if markdown else html)
manager.reset_debounced(thread_id)
if chat:
manager.comments[thread_id] = quip.get_comments(thread_id)
self.view.window().run_command(
COMMAND_OPEN_CHAT,
{"thread": thread_id,
"name": "Comments",
"is_document": True}
)
self.view.window().run_command(
COMMAND_OPEN_PREVIEW,
{"content": html}
)
else:
manager.preview.view.run_command(
COMMAND_INSERT_PREVIEW,
{"content": html}
)
class PrintQuipFileTree(sublime_plugin.TextCommand):
def run(self, edit):
self.view.erase_phantoms(KEY_FILE_TREE_PHANTOM_SET)
self.view.set_read_only(True)
self.view.set_name("Folders")
file_tree = quip.get_thread_tree()
string_tree = self._print_tree(file_tree, "", "")
phantom = sublime.Phantom(
region=Region(0, self.view.size()),
content=string_tree,
layout=sublime.LAYOUT_INLINE,
on_navigate=self._on_click_doc_link
)
sublime.PhantomSet(self.view, KEY_FILE_TREE_PHANTOM_SET) \
.update([phantom, phantom]) # TODO разобраться почему только так работает
def _on_click_doc_link(self, command):
args = command.split(":")
if len(args) == 2:
if args[0] == "open":
self.view.window().run_command(COMMAND_OPEN_DOCUMENT, {"thread_id": args[1], "markdown": False})
if args[0] == "delete":
self.view.window().run_command(COMMAND_DELETE_DOCUMENT, {"thread_id": args[1]})
if args[0] == "create":
self.view.window().run_command(COMMAND_CREATE_DOCUMENT, {"thread_id": args[1]})
def _print_tree(self, node, prefix, postfix):
if node is None:
return ""
thread_name = node.name
if node.thread_type == "document":
thread_name = "<a href=\"open:{0}\">{1}</a>".format(node.thread_id, node.name)
str_result = "{0} Name: {1} | Type: {2}{3} (<a href=\"delete:{4}\">Delete</a>)".format(
prefix,
thread_name,
node.thread_type,
postfix,
node.thread_id
)
elif node.thread_type != "root":
str_result = "{0} Name: {1} | Type: {2}{3} (<a href=\"create:{4}\">New document</a>)".format(
prefix,
thread_name,
node.thread_type,
postfix,
node.thread_id
)
else:
str_result = "{0} Name: {1} | Type: {2}{3}".format(
prefix,
thread_name,
node.thread_type,
postfix
)
if node.children is None:
return str_result
for child in node.children:
str_result += "<ul>" + self._print_tree(child, "<li>", "</li>") + "</ul>"
return str_result
class CreateDocumentCommand(sublime_plugin.WindowCommand):
def run(self, thread_id):
manager.temp_folder_id_for_create = thread_id
self.window.show_input_panel("Enter file name:", "", self._create_file, None, None)
def _create_file(self, file_name):
folder_id = manager.temp_folder_id_for_create
manager.temp_folder_id_for_create = None
quip.create_document("", content=file_name, content_type="markdown", folder_id=folder_id)
self.window.run_command(COMMAND_SHOW_FILE_TREE)
class DeleteDocumentCommand(sublime_plugin.TextCommand):
def run(self, edit, thread_id, markdown=False, chat=False):
isOk = sublime.ok_cancel_dialog("Удалить документ?", "Удалить")
if isOk:
response = quip.delete_document(thread_id)
if len(response) == 0:
self.view.window().run_command(COMMAND_SHOW_FILE_TREE)
class ShowContactsCommand(sublime_plugin.WindowCommand):
def run(self):
view = self.window.new_file()
view.run_command(COMMAND_INSERT_CONTACTS)
class InsertContactsCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.erase_phantoms(KEY_CONTACTS_PHANTOM_SET)
self.view.set_read_only(True)
self.view.set_name("Contacts")
user, friends = quip.get_contacts()
tree = str(user) if friends else "Нет контактов"
tree += "<ul>"
tree += "\n".join(["<li>%s</li>" % str(friend) for friend in friends if friend])
tree += "</ul>"
phantom = sublime.Phantom(
region=Region(0, self.view.size()),
content=tree,
layout=sublime.LAYOUT_INLINE,
on_navigate=lambda thread: self.view.window().run_command(
COMMAND_OPEN_CHAT,
{"thread": thread}
)
)
sublime.PhantomSet(self.view, KEY_CONTACTS_PHANTOM_SET).update([phantom, phantom])
class OpenChatCommand(sublime_plugin.WindowCommand):
def run(self, thread=None, name="Private Chat", is_document=False):
self.window.run_command("close_chat")
self._open_chat(thread, name, is_document)
def _open_chat(self, thread, name, is_document):
if not thread:
return
self.window.run_command("set_layout", {
"cols": [0, 0.6, 1.0],
"rows": [0.0, 1.0],
"cells": [[0, 0, 1, 1], [1, 0, 2, 1]]
})
manager.set_chat(
ChatView(thread, sublime.active_window().new_file(), name, is_document)
)
manager.chat.view.run_command(
COMMAND_INSERT_CHAT_MESSAGES,
{"messages": [str(m) for m in quip.get_messages(thread)]}
)
class OpenPreviewCommand(sublime_plugin.WindowCommand):
def run(self, content, name="Document Preview"):
self.window.run_command("close_preview")
self._open_preview(content, name)
def _open_preview(self, content, name):
manager.set_preview(
Preview(content, sublime.active_window().new_file(), name)
)
manager.preview.view.run_command(
COMMAND_INSERT_PREVIEW,
{"content": content}
)
class InsertPreviewCommand(sublime_plugin.TextCommand):
def run(self, edit, content):
self.view.erase_phantoms(KEY_PREVIEW_PHANTOM_SET)
manager.preview.view.set_scratch(False)
manager.preview.view.set_read_only(False)
phantom = sublime.Phantom(
region=Region(0, self.view.size()),
content=content.replace("<br/>", ""),
layout=sublime.LAYOUT_INLINE
)
manager.preview.phantom = [phantom, phantom]
sublime.PhantomSet(self.view, KEY_PREVIEW_PHANTOM_SET).update(manager.preview.phantom)
manager.preview.view.set_read_only(True)
manager.preview.view.set_scratch(True)
class CloseChatCommand(sublime_plugin.WindowCommand):
def run(self):
if not manager.chat or not manager.chat.view:
return
manager.chat.view.close()
manager.reset_chat()
if manager.preview and manager.preview.view:
return
self.window.run_command("set_layout", {
"cols": [0, 1.0],
"rows": [0.0, 1.0],
"cells": [[0, 0, 1, 1]]
})
class ClosePreviewCommand(sublime_plugin.WindowCommand):
def run(self):
if not manager.preview or not manager.preview.view:
return
manager.preview.view.close()
manager.reset_preview()
if not manager.chat or not manager.chat.view:
self.window.run_command("set_layout", {
"cols": [0, 1.0],
"rows": [0.0, 1.0],
"cells": [[0, 0, 1, 1]]
})
class InsertChatMessagesCommand(sublime_plugin.TextCommand):
def run(self, edit, messages):
self.view.erase_phantoms(KEY_MESSAGES_PHANTOM_SET)
result = "".join([self._convert_to_html(m) for m in messages])
manager.chat.view.set_scratch(False)
manager.chat.view.set_read_only(False)
phantom = sublime.Phantom(
region=Region(0, self.view.size()),
content=result,
layout=sublime.LAYOUT_INLINE
)
manager.chat.add_phantom(phantom)
sublime.PhantomSet(self.view, KEY_MESSAGES_PHANTOM_SET).update(manager.chat.phantoms)
manager.chat.view.set_read_only(True)
manager.chat.view.set_scratch(True)
def _convert_to_html(self, message):
return "<div>%s</div>" % message
class SendChatMessageCommand(sublime_plugin.TextCommand):
def run(self, edit):
current_window = sublime.active_window()
if manager.chat and manager.chat.view:
current_window.show_input_panel("Enter chat message:", "", self._send_message, None, None)
def _send_message(self, text: str):
if not manager.chat or not manager.chat.id:
return
message = quip.send_message(manager.chat.id, text)
manager.chat.view.run_command(COMMAND_INSERT_CHAT_MESSAGES, {"messages": [str(message)]})
class UploadChangesOnSave(sublime_plugin.EventListener):
def on_pre_save(self, view):
if not manager.contains(view) or not manager.event_propagation:
return
editor = HTMLEditor(view)
thread = manager.get_thread(view)
for line, section in editor.edited:
quip.edit_document(
thread_id=thread, content=line,
operation=4, section_id=section
)
for line, section in editor.new:
quip.edit_document(
thread_id=thread, content=line,
operation=2 if section else 0,
section_id=section, content_type="markdown"
)
for line, section in editor.deleted:
quip.edit_document(
thread_id=thread, content=line,
operation=5, section_id=section
)
view.run_command(COMMAND_INSERT_SELECTED_DOCUMENT, {"thread_id": thread, "chat": False})
manager.add(thread, view)
def on_post_save(self, view):
sublime.active_window().focus_view(view)
view.sel().clear()
manager.event_propagation = True
def on_close(self, view):
if manager.chat:
if manager.chat.view == view:
sublime.active_window().run_command("close_chat")
if (manager.get_thread(view) and manager.chat.is_document):
sublime.active_window().run_command("close_chat")
sublime.active_window().run_command("close_preview")
manager.remove_tab(view=view)
def on_activated(self, view):
thread = manager.get_thread(view)
if thread is None or thread == TREE_VIEW_TAB_ID:
return
if manager.update_debounced(thread):
view.run_command(COMMAND_INSERT_SELECTED_DOCUMENT, {"thread_id": thread, "chat": False})
manager.event_propagation = False
view.run_command("save")
# def on_activated_async(self, view):
# thread = manager.get_thread(view)
# if thread is None or thread == TREE_VIEW_TAB_ID or not manager.event_propagation:
# return
# manager.comments[thread] = quip.get_comments(thread)
# view.window().run_command(
# COMMAND_OPEN_CHAT,
# {"thread": thread,
# "name": "Comments",
# "is_document": True}
# )
# view.window().run_command(
# COMMAND_OPEN_PREVIEW,
# {"content": view.substr(sublime.Region(0, view.size()))}
# )
# def on_deactivated_async(self, view):
# thread = manager.get_thread(view)
# if thread is None or thread == TREE_VIEW_TAB_ID or not manager.event_propagation:
# return
# # manager.chat.view.close()
# # manager.preview.view.close()
# view.window().run_command("set_layout", {
# "cols": [0, 1.0],
# "rows": [0.0, 1.0],
# "cells": [[0, 0, 1, 1]]
# })
class ShowCommentsOnHover(sublime_plugin.EventListener):
def on_hover(self, view, point, hover_zone):
thread = manager.get_thread(view)
messages = manager.comments.get(thread)
if not messages or view.is_popup_visible():
return
word_region = view.word(point)
word = view.substr(word_region)
comments = ["<div>" + str(comment) + "</div>" for comment in messages if word in comment.sections]
view.show_popup("\n".join(comments), flags=sublime.HIDE_ON_MOUSE_MOVE_AWAY, location=point, max_width=600, max_height=1500)