Skip to content

Commit

Permalink
more direct & readable stack of method calls concerning editing actions
Browse files Browse the repository at this point in the history
related to #4

the contextual menu has been restructured too, to include more actions
  • Loading branch information
maoschanz committed Dec 31, 2021
1 parent ad66d62 commit dfe0ff1
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 121 deletions.
75 changes: 30 additions & 45 deletions markdown_preview/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,28 +144,30 @@ def connect_actions(self):
action_remove = Gio.SimpleAction(name='md-prev-remove-all')
action_remove.connect('activate', lambda i, j: self.view_method('remove_all'))

self.add_format_action('md-prev-format-title-upper', 'format_title_upper')
self.add_format_action('md-prev-format-title-lower', 'format_title_lower')

self.add_format_action('md-prev-format-bold', 'format_bold')
self.add_format_action('md-prev-format-italic', 'format_italic')
# self.add_format_action('md-prev-format-underline', 'format_underline')
# self.add_format_action('md-prev-format-stroke', 'format_stroke')
self.add_format_action('md-prev-format-monospace', 'format_monospace')
self.add_format_action('md-prev-format-quote', 'format_quote')

self.add_format_action('md-prev-list-unordered', 'list_unordered')
self.add_format_action('md-prev-list-ordered', 'list_ordered')
self.add_format_action('md-prev-format-title-upper', 'remove_block', '#')
self.add_format_action('md-prev-format-title-lower', 'format_block', '#')

self.add_format_action('md-prev-format-bold', 'format_inline', '**')
self.add_format_action('md-prev-format-italic', 'format_inline', '*')
self.add_format_action('md-prev-format-underline', 'format_inline', '__')
self.add_format_action('md-prev-format-stroke', 'format_inline', '~~')
self.add_format_action('md-prev-format-monospace', 'format_inline', '`')

self.add_format_action('md-prev-block-code', 'format_block2', '\n```\n')
self.add_format_action('md-prev-block-quote', 'format_block', '>')
self.add_format_action('md-prev-list-unordered', 'format_block', '-')
self.add_format_action('md-prev-list-ordered', 'format_block', '1.')

self.add_format_action('md-prev-insert-picture', 'insert_picture')
self.add_format_action('md-prev-insert-link', 'insert_link')
self.add_format_action('md-prev-insert-table', 'insert_table')

def add_format_action(self, action_name, method_name):
def add_format_action(self, action_name, method, arg=None):
action = Gio.SimpleAction(name=action_name)
action.connect('activate', lambda i, j: self.view_method(method_name))
action.connect('activate', lambda i, j: self.view_method(method, arg))
self.window.add_action(action)

def view_method(self, name):
def view_method(self, method_name, argument=None):
if self.preview.recognize_format() != 'md':
return

Expand All @@ -175,39 +177,22 @@ def view_method(self, name):
else:
return

# TODO gérer ça de manière statique avec une méthode hors-classe dans le
# fichier tags_manager.py
# ou juste ajouter les actions depuis le tags_manager si possible ??

# print('action : ' + name) # TODO terminer ça mdr

if name == 'insert_table':
v.insert_table()
elif name == 'insert_picture':
if method_name == 'insert_table':
v.insert_table(argument)
elif method_name == 'insert_picture':
v.insert_picture(self.window)
elif name == 'insert_link':
elif method_name == 'insert_link':
v.insert_link(self.window)

elif name == 'format_bold':
v.format_bold()
elif name == 'format_italic':
v.format_italic()
elif name == 'format_monospace':
v.format_monospace()
elif name == 'format_quote':
v.format_quote()
elif name == 'format_underline':
v.format_underline()

elif name == 'list_ordered':
v.list_ordered()
elif name == 'list_unordered':
v.list_unordered()

elif name == 'format_title_upper':
v.format_title_upper()
elif name == 'format_title_lower':
v.format_title_lower()
elif method_name == 'format_inline':
v.add_word_tags(argument)

elif method_name == 'format_block':
v.add_line_tags(argument)
elif method_name == 'format_block2':
v.add_block_tags(argument, argument)
elif method_name == 'remove_block':
v.remove_line_tags(argument)

def on_change_panel_from_popover(self, *args):
self._auto_position = False
Expand Down
37 changes: 1 addition & 36 deletions markdown_preview/tags_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,41 +61,6 @@ def add_word_tags(self, tag_both):

############################################################################

def format_title_lower(self):
self.add_line_tags('#')

def format_title_upper(self):
self.remove_line_tags('#')

def format_title(self, level):
self.add_line_tags('#'*level)

def format_bold(self):
self.add_word_tags('**')

def format_italic(self):
self.add_word_tags('*')

def format_monospace(self):
self.add_word_tags('`')

def format_underline(self):
self.add_word_tags('__')

def format_stroke(self):
self.add_word_tags('~~')

def list_unordered(self):
self.add_line_tags('-')

def list_ordered(self):
self.add_line_tags('1.')

def format_quote(self):
self.add_line_tags('>')

############################################################################

def insert_link(self, window):
pass

Expand Down Expand Up @@ -135,7 +100,7 @@ def insert_picture(self, window):
# Actually insert
self.add_tags_characters(document, start_tag, end_tag, start, end)

def insert_table(self):
def insert_table(self, nb_columns):
doc = self._view_plugin.view.get_buffer()
table = '|||\n|--|--|\n|||'
iterator = doc.get_iter_at_mark(doc.get_insert())
Expand Down
91 changes: 51 additions & 40 deletions markdown_preview/view-menu.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,71 @@
<item>
<attribute name="label" translatable="yes">Remove all tags</attribute>
<attribute name="action">win.md-prev-remove-all</attribute>
<attribute name="action">win.todo</attribute>
</item>
</section>
<section>

<section>
<submenu>
<attribute name="label" translatable="yes">Title</attribute>
<attribute name="label" translatable="yes">Inline formatting</attribute>

<section>
<item>
<attribute name="label" translatable="yes">Upper</attribute>
<attribute name="label" translatable="yes">Upper title level</attribute>
<attribute name="action">win.md-prev-format-title-upper</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Lower</attribute>
<attribute name="label" translatable="yes">Lower title level</attribute>
<attribute name="action">win.md-prev-format-title-lower</attribute>
</item>
</section>
</submenu>

<item>
<attribute name="label" translatable="yes">Bold</attribute>
<attribute name="action">win.md-prev-format-bold</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Italic</attribute>
<attribute name="action">win.md-prev-format-italic</attribute>
</item>
<!-- <item>-->
<!-- <attribute name="label" translatable="yes">Underline</attribute>-->
<!-- <attribute name="action">win.md-prev-format-underline</attribute>-->
<!-- </item>-->
<!-- <item>-->
<!-- <attribute name="label" translatable="yes">Stoke</attribute>-->
<!-- <attribute name="action">win.md-prev-format-stroke</attribute>-->
<!-- </item>-->
<item>
<attribute name="label" translatable="yes">Monospace</attribute>
<attribute name="action">win.md-prev-format-monospace</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Quote</attribute>
<attribute name="action">win.md-prev-format-quote</attribute>
</item>
</section>
<section>
<item>
<attribute name="label" translatable="yes">Bold</attribute>
<attribute name="action">win.md-prev-format-bold</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Italic</attribute>
<attribute name="action">win.md-prev-format-italic</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Code</attribute>
<attribute name="action">win.md-prev-format-monospace</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Underline</attribute>
<attribute name="action">win.md-prev-format-underline</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Stroke</attribute>
<attribute name="action">win.md-prev-format-stroke</attribute>
</item>
</section>
</submenu>
<submenu>
<attribute name="label" translatable="yes">Block formatting</attribute>
<section>
<item>
<attribute name="label" translatable="yes">Code</attribute>
<attribute name="action">win.md-prev-block-code</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Quote</attribute>
<attribute name="action">win.md-prev-block-quote</attribute>
</item>
</section>

<section>
<item>
<attribute name="label" translatable="yes">Unordered list</attribute>
<attribute name="action">win.md-prev-list-unordered</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Ordered list</attribute>
<attribute name="action">win.md-prev-list-ordered</attribute>
</item>
<section>
<item>
<attribute name="label" translatable="yes">Unordered list</attribute>
<attribute name="action">win.md-prev-list-unordered</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Ordered list</attribute>
<attribute name="action">win.md-prev-list-ordered</attribute>
</item>
</section>
</submenu>
</section>

<section>
Expand Down

0 comments on commit dfe0ff1

Please sign in to comment.