Skip to content

Commit

Permalink
Add saving, editing, deleting for documents
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarin1029 committed Dec 27, 2020
1 parent 2eac26b commit 7b61fbe
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 22 deletions.
62 changes: 40 additions & 22 deletions QuipEditor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import re

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
from .src.deps.markdownify import markdownify as md
Expand Down Expand Up @@ -31,13 +35,13 @@ def plugin_loaded():

class OpenDocumentCommand(sublime_plugin.WindowCommand):

def run(self, thread_id, markdown=True):
def run(self, thread_id, markdown=False, chat=True):
view = manager.get_tab(thread_id)
if not view:
view = self.window.new_file()
self.window.focus_view(view)
view.retarget(CACHE_DIRECTORY + "/" + thread_id + ".html")
view.run_command(COMMAND_INSERT_SELECTED_DOCUMENT, {"thread_id": thread_id, 'markdown': markdown})
view.run_command(COMMAND_INSERT_SELECTED_DOCUMENT, {"thread_id": thread_id, 'markdown': markdown, "chat": chat})
view.run_command('save')
manager.add(thread_id, view)

Expand All @@ -55,15 +59,16 @@ def run(self):

class InsertSelectedDocumentCommand(sublime_plugin.TextCommand):

def run(self, edit, thread_id, markdown):
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()), html) #md(html) if markdown else html)
manager.comments[thread_id] = quip.get_comments(thread_id)
self.view.window().run_command(
COMMAND_OPEN_CHAT,
{"thread": thread_id,
'name': 'Comments'}
)
self.view.replace(edit, Region(0, self.view.size()), md(html) if markdown else html)
if chat:
manager.comments[thread_id] = quip.get_comments(thread_id)
self.view.window().run_command(
COMMAND_OPEN_CHAT,
{"thread": thread_id,
'name': 'Comments'}
)


class PrintQuipFileTree(sublime_plugin.TextCommand):
Expand All @@ -84,7 +89,7 @@ def run(self, edit):
.update([phantom, phantom]) # TODO разобраться почему только так работает

def _open_doc(self, doc_id):
self.view.window().run_command(COMMAND_OPEN_DOCUMENT, {"thread_id": doc_id})
self.view.window().run_command(COMMAND_OPEN_DOCUMENT, {"thread_id": doc_id, "markdown": False})

def _print_tree(self, node, prefix, postfix):
if node is None:
Expand Down Expand Up @@ -217,13 +222,29 @@ class UploadChangesOnSave(sublime_plugin.EventListener):
def on_pre_save(self, view):
if not manager.contains(view):
return
line = view.substr(view.full_line(view.sel()[0]))
if line.startswith('<') and line.endswith('>\n'):
html = line
else:
html = "<p>" + line + "</p>"

quip.edit_document(thread_id=manager.get_thread(view), content=html)
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
)
for section in editor.deleted:
quip.edit_document(
thread_id=thread, content=None,
operation=5, section_id=section
)

view.run_command(COMMAND_INSERT_SELECTED_DOCUMENT, {"thread_id": thread, "chat": False})
manager.add(thread, view)

def on_close(self, view):
if manager.chat and manager.chat.view == view:
Expand All @@ -245,8 +266,5 @@ def on_hover(self, view, point, hover_zone):
return
word_region = view.word(point)
word = view.substr(word_region)
comments = [str(comment) for comment in messages if word in comment.sections]
view.sel().clear()
view.sel().add(Region(point))
view.show_popup_menu(comments, None)
#view.show_popup('\n'.join(comments), flags=sublime.HIDE_ON_MOUSE_MOVE_AWAY, location=point)
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)
71 changes: 71 additions & 0 deletions src/editor/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import re
from sublime import Region


class HTMLEditor:

def __init__(self, view):
self.new, self.edited, self.deleted = self._calculate(
old=self._readlines(view),
new=self._readregions(view)
)

def _calculate(self, old, new):
new_lines = []
edited = []
deleted = []

old_copy = [line.replace('\n', '') for line in old]
new_copy = [line.replace('\n', '') for line in new]

for i, line in enumerate(new_copy):
if line not in old_copy:
section = self._parse_id(line)
if section:
edited.append((self._to_html(line), section))
continue
if i <= len(old_copy):
section = self._parse_id(old_copy[i-1])
new_lines.append((self._to_html(new[i]), section))

edited_sections = [section for line,section in edited]
for i, copy in enumerate(old_copy):
if copy not in new_copy:
section = self._parse_id(old[i])
if section and section not in edited_sections:
deleted.append(section)
old.remove(old[i])

return new_lines, edited, deleted

def _readlines(self, view):
try:
with open(view.file_name()) as file:
return [line for line in file.readlines() if line and line != '\n']
except Exception as ex:
print(ex)
return []

def _readregions(self, view):
lines = []
for region in view.lines(Region(0, view.size())):
text = view.substr(region)
if text and text != '\n':
lines.append(text)
return lines

def _get_sections(self, lines):
return set([self._parse_id(line) for line in lines])

def _parse_id(self, line):
if line:
result = re.search(r"id='\w{11}'", line)
if result:
id = result.group(0).split("'")[-2]
if len(id) == 11:
return id

def _to_html(self, line):
if line.startswith('<') and (line.endswith('>') or line.endswith('>\n')):
return line
return "<p>" + line + "</p>"

0 comments on commit 7b61fbe

Please sign in to comment.