forked from Mirrors/apostrophe
Add format inserts to the undo stack as single actions
Also fixes undoing list items.github/fork/yochananmarqos/patch-1
parent
0b6e84bf8c
commit
bd2d78b86a
|
@ -1,5 +1,7 @@
|
||||||
from gettext import gettext as _
|
from gettext import gettext as _
|
||||||
|
|
||||||
|
from uberwriter.helpers import user_action
|
||||||
|
|
||||||
|
|
||||||
class FormatInserter:
|
class FormatInserter:
|
||||||
"""Manages insertion of formatting.
|
"""Manages insertion of formatting.
|
||||||
|
@ -25,7 +27,8 @@ class FormatInserter:
|
||||||
"""Insert horizontal rule"""
|
"""Insert horizontal rule"""
|
||||||
|
|
||||||
text_buffer = text_view.get_buffer()
|
text_buffer = text_view.get_buffer()
|
||||||
text_buffer.insert_at_cursor("\n\n---\n")
|
with user_action(text_buffer):
|
||||||
|
text_buffer.insert_at_cursor("\n\n---\n")
|
||||||
text_view.scroll_mark_onscreen(text_buffer.get_insert())
|
text_view.scroll_mark_onscreen(text_buffer.get_insert())
|
||||||
|
|
||||||
def insert_list_item(self, text_view, _data=None):
|
def insert_list_item(self, text_view, _data=None):
|
||||||
|
@ -35,12 +38,14 @@ class FormatInserter:
|
||||||
if text_buffer.get_has_selection():
|
if text_buffer.get_has_selection():
|
||||||
(start, end) = text_buffer.get_selection_bounds()
|
(start, end) = text_buffer.get_selection_bounds()
|
||||||
if start.starts_line():
|
if start.starts_line():
|
||||||
text = text_buffer.get_text(start, end, False)
|
with user_action(text_buffer):
|
||||||
if text.startswith(("- ", "* ", "+ ")):
|
text = text_buffer.get_text(start, end, False)
|
||||||
delete_end = start.forward_chars(2)
|
if text.startswith(("- ", "* ", "+ ")):
|
||||||
text_buffer.delete(start, delete_end)
|
delete_end = start.copy()
|
||||||
else:
|
delete_end.forward_chars(2)
|
||||||
text_buffer.insert(start, "- ")
|
text_buffer.delete(start, delete_end)
|
||||||
|
else:
|
||||||
|
text_buffer.insert(start, "- ")
|
||||||
else:
|
else:
|
||||||
helptext = _("Item")
|
helptext = _("Item")
|
||||||
text_length = len(helptext)
|
text_length = len(helptext)
|
||||||
|
@ -53,25 +58,25 @@ class FormatInserter:
|
||||||
text = text_buffer.get_text(cursor_iter, start_ext, False)
|
text = text_buffer.get_text(cursor_iter, start_ext, False)
|
||||||
lines = text.splitlines()
|
lines = text.splitlines()
|
||||||
|
|
||||||
for line in reversed(lines):
|
with user_action(text_buffer):
|
||||||
if line and line.startswith(("- ", "* ", "+ ")):
|
for line in reversed(lines):
|
||||||
if cursor_iter.starts_line():
|
if line and line.startswith(("- ", "* ", "+ ")):
|
||||||
text_buffer.insert_at_cursor(line[:2] + helptext)
|
|
||||||
else:
|
|
||||||
text_buffer.insert_at_cursor(
|
|
||||||
"\n" + line[:2] + helptext)
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
if not lines[-1] and not lines[-2]:
|
|
||||||
text_buffer.insert_at_cursor("- " + helptext)
|
|
||||||
elif not lines[-1]:
|
|
||||||
if cursor_iter.starts_line():
|
if cursor_iter.starts_line():
|
||||||
text_buffer.insert_at_cursor("- " + helptext)
|
text_buffer.insert_at_cursor(line[:2] + helptext)
|
||||||
else:
|
else:
|
||||||
text_buffer.insert_at_cursor("\n- " + helptext)
|
text_buffer.insert_at_cursor("\n" + line[:2] + helptext)
|
||||||
|
break
|
||||||
else:
|
else:
|
||||||
text_buffer.insert_at_cursor("\n\n- " + helptext)
|
if not lines[-1] and not lines[-2]:
|
||||||
break
|
text_buffer.insert_at_cursor("- " + helptext)
|
||||||
|
elif not lines[-1]:
|
||||||
|
if cursor_iter.starts_line():
|
||||||
|
text_buffer.insert_at_cursor("- " + helptext)
|
||||||
|
else:
|
||||||
|
text_buffer.insert_at_cursor("\n- " + helptext)
|
||||||
|
else:
|
||||||
|
text_buffer.insert_at_cursor("\n\n- " + helptext)
|
||||||
|
break
|
||||||
|
|
||||||
self.__select_text(text_view, 0, text_length)
|
self.__select_text(text_view, 0, text_length)
|
||||||
|
|
||||||
|
@ -83,57 +88,60 @@ class FormatInserter:
|
||||||
"""Insert header or mark a selection as a list header"""
|
"""Insert header or mark a selection as a list header"""
|
||||||
|
|
||||||
text_buffer = text_view.get_buffer()
|
text_buffer = text_view.get_buffer()
|
||||||
if text_buffer.get_has_selection():
|
with user_action(text_buffer):
|
||||||
(start, end) = text_buffer.get_selection_bounds()
|
if text_buffer.get_has_selection():
|
||||||
text = text_buffer.get_text(start, end, False)
|
(start, end) = text_buffer.get_selection_bounds()
|
||||||
text_buffer.delete(start, end)
|
text = text_buffer.get_text(start, end, False)
|
||||||
else:
|
text_buffer.delete(start, end)
|
||||||
text = _("Header")
|
else:
|
||||||
|
text = _("Header")
|
||||||
|
|
||||||
|
text_buffer.insert_at_cursor("#" + " " + text)
|
||||||
|
|
||||||
text_buffer.insert_at_cursor("#" + " " + text)
|
|
||||||
self.__select_text(text_view, 0, len(text))
|
self.__select_text(text_view, 0, len(text))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def __wrap(text_view, wrap, helptext=""):
|
def __wrap(text_view, wrap, helptext=""):
|
||||||
"""Inserts wrap format to the selected text (helper text when nothing selected)"""
|
"""Inserts wrap format to the selected text (helper text when nothing selected)"""
|
||||||
text_buffer = text_view.get_buffer()
|
text_buffer = text_view.get_buffer()
|
||||||
if text_buffer.get_has_selection():
|
with user_action(text_buffer):
|
||||||
# Find current highlighting
|
if text_buffer.get_has_selection():
|
||||||
(start, end) = text_buffer.get_selection_bounds()
|
# Find current highlighting
|
||||||
moved = False
|
(start, end) = text_buffer.get_selection_bounds()
|
||||||
if (start.get_offset() >= len(wrap) and
|
moved = False
|
||||||
end.get_offset() <= text_buffer.get_char_count() - len(wrap)):
|
if (start.get_offset() >= len(wrap) and
|
||||||
moved = True
|
end.get_offset() <= text_buffer.get_char_count() - len(wrap)):
|
||||||
ext_start = start.copy()
|
moved = True
|
||||||
ext_start.backward_chars(len(wrap))
|
ext_start = start.copy()
|
||||||
ext_end = end.copy()
|
ext_start.backward_chars(len(wrap))
|
||||||
ext_end.forward_chars(len(wrap))
|
ext_end = end.copy()
|
||||||
text = text_buffer.get_text(ext_start, ext_end, True)
|
ext_end.forward_chars(len(wrap))
|
||||||
else:
|
text = text_buffer.get_text(ext_start, ext_end, True)
|
||||||
text = text_buffer.get_text(start, end, True)
|
else:
|
||||||
|
text = text_buffer.get_text(start, end, True)
|
||||||
|
|
||||||
if moved and text.startswith(wrap) and text.endswith(wrap):
|
if moved and text.startswith(wrap) and text.endswith(wrap):
|
||||||
text = text[len(wrap):-len(wrap)]
|
|
||||||
new_text = text
|
|
||||||
text_buffer.delete(ext_start, ext_end)
|
|
||||||
move_back = 0
|
|
||||||
else:
|
|
||||||
if moved:
|
|
||||||
text = text[len(wrap):-len(wrap)]
|
text = text[len(wrap):-len(wrap)]
|
||||||
new_text = text.lstrip().rstrip()
|
new_text = text
|
||||||
text = text.replace(new_text, wrap + new_text + wrap)
|
text_buffer.delete(ext_start, ext_end)
|
||||||
|
move_back = 0
|
||||||
|
else:
|
||||||
|
if moved:
|
||||||
|
text = text[len(wrap):-len(wrap)]
|
||||||
|
new_text = text.lstrip().rstrip()
|
||||||
|
text = text.replace(new_text, wrap + new_text + wrap)
|
||||||
|
|
||||||
text_buffer.delete(start, end)
|
text_buffer.delete(start, end)
|
||||||
|
move_back = len(wrap)
|
||||||
|
|
||||||
|
text_buffer.insert_at_cursor(text)
|
||||||
|
text_length = len(new_text)
|
||||||
|
|
||||||
|
else:
|
||||||
|
text_buffer.insert_at_cursor(wrap + helptext + wrap)
|
||||||
|
text_length = len(helptext)
|
||||||
move_back = len(wrap)
|
move_back = len(wrap)
|
||||||
|
|
||||||
text_buffer.insert_at_cursor(text)
|
|
||||||
text_length = len(new_text)
|
|
||||||
|
|
||||||
else:
|
|
||||||
text_buffer.insert_at_cursor(wrap + helptext + wrap)
|
|
||||||
text_length = len(helptext)
|
|
||||||
move_back = len(wrap)
|
|
||||||
|
|
||||||
cursor_mark = text_buffer.get_insert()
|
cursor_mark = text_buffer.get_insert()
|
||||||
cursor_iter = text_buffer.get_iter_at_mark(cursor_mark)
|
cursor_iter = text_buffer.get_iter_at_mark(cursor_mark)
|
||||||
cursor_iter.backward_chars(move_back)
|
cursor_iter.backward_chars(move_back)
|
||||||
|
|
Loading…
Reference in New Issue