Jump to content

InputfieldSimpleMDE


Macrura
 Share

Recommended Posts

Basic implementation of the Simple MDE as an Inputfield.

https://simplemde.com/

Module developed in reply to request from @OrganizedFellow (https://processwire.com/talk/topic/13474-found-a-handy-js-based-markdown-editor/)

Modules Directory:

http://modules.processwire.com/modules/inputfield-simple-mde/

Github:

https://github.com/outflux3/InputfieldSimpleMDE

Editor example:

post-136-0-45492600-1465258417_thumb.png

Preview mode:

post-136-0-49079000-1465258416_thumb.png

Frontend output (using Markdown/Parsedown textformatter and Image Tags)

post-136-0-70950400-1465258415_thumb.png

Limitations etc:

This has been tested with multiple instances on 1 page and seems to work fine.

Toolbar is not configurable, but you can edit the JS file;

In the spirit of keeping this simple, there are no module settings.

If you want the spellchecker, you can enable it in the JS file.

If is seems that there is a need for configurable instances, it could be added, but so far this works fine and can't see any reason to complicate it further.

  • Like 20
Link to comment
Share on other sites

Well done. Thanks. I like the lines and words counter. :) But it shows up only if I start to edit. Same to the included spellcheck. Is there an option to disable spellcheck?

In generally I like Markdown much more than CKEditor, because its more save to prevent Layoutkilling by Customers. Great compromise between both!

Link to comment
Share on other sites

seems the init state does not show the lines/words counter - this is a documented issue with the plugin, see https://simplemde.com/

you can force it to update on load by enabling autosave;

(adding these to the init call)

autosave: {
	enabled: true,
	uniqueId: thisID,
	delay: 1000,
},
spellChecker: false

the plan is to make each instance individually configurable, but for now this is a very bare-bones implementation.

i'm going to add those options to the js init, but maybe leave them commented out, and let users uncomment to disable spellcheck, and enable autosave;

BTW - the autosave is interesting and works, not sure how useful it is or even if it makes sense in the context of a CMS.

if it were to be enabled, additional configuration would be required to change the way it works so as to have a unique id for the page and the field though.

  • Like 2
Link to comment
Share on other sites

Where do it autosave the data? I think, not into the DB-field, but into some local storage, or I'm wrong? ( Haven't looked into it, just want to ask a dump question ;-) )

Link to comment
Share on other sites

yes - it autosaves it in local storage, which is why the module init would need to ID each field with it's page id and instance id... so by default the autosave is off (and probably not necessary)...

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

The module should show up in the directory in a few days. In the meantime, some small fixes and optimizations were made, such as fixed height editor window, and testing on both admin themes. At this time, the fullscreen, side-by-side editing only seems to work on the default theme; in Reno the editor goes behind the left menubar. If anyone has a quick CSS fix for this feel free to post, else i'll have a look at some point when i need to use that mode on reno

Link to comment
Share on other sites

  • 2 months later...

I tried out the module in a website I'm just getting ready locally. When editing a blog entry, I noticed that SimpleMDE would always truncate my text. 

The article had ~1400 words, with about 12000 characters. After saving I ended up with < 1200 words. 

 

Link to comment
Share on other sites

ok sorry about that limitation; so far i haven't noticed any limitations in terms of length of content, and i have some really long paginated articles. Will have to see if i do in fact have anything longer and 1200 words

Link to comment
Share on other sites

12 hours ago, Macrura said:

ok sorry about that limitation; so far i haven't noticed any limitations in terms of length of content, and i have some really long paginated articles. Will have to see if i do in fact have anything longer and 1200 words

Hi,

The issue has resolved itself by now. It is actually a limitation of the textarea field in PW 2.7.

Sorry for the hassle ^^°

Link to comment
Share on other sites

  • 1 month later...

I just wrote this as a comment on the Module itself, but two issues I've found with this (otherwise perfect and awesome) Module:

 

1. SimpleMDE has been updated from 1.10.1 to 1.11.2

2. If a textarea field in the admin is using InputfieldSimpleMDE, but is closed upon load, or on a separate content tab created with `FieldsetTabOpen`, its value is blank. I've been trying to debug why all morning, but it's over my head.

Link to comment
Share on other sites

are you sure the value is blank? Mine works on the closed field, you just can't see the content until you click into the field; The field is still initialized when you open it; not sure if the fact that you can't see the content in the field is due to some initialization issue, or is something specific to how this component works.

I'm not sure this can be fixed, so for now i think your options are to either tell the editors to click into the field to see the content, or don't use collapsed fields.

In some future iteration of this module, the eventual aim is to have it operate more like CK editor, so that the config takes place from the admin, and each instance can be individually configured; also support for some sort of image input. But i think it will take a while till it reaches that stage, and also would depend on demand...

  • Like 3
Link to comment
Share on other sites

Sorry - your explanation of the problem is more accurate. The value of the field definitely exists, it's just not visible until the field is given focus.

My idea for how to fix this was something like `imagesLoaded()` for Masonry layouts. I was going to see if there was a way to "retrigger" the SimpleMDE field after a tab was opened. I just didn't know if opening a tab triggers a Javascript event. If it does, I think that would fix this problem.

 

Link to comment
Share on other sites

@Macrura I fixed it!

See the code attached.

var initSimpleMDE = function() {
	var thisID = $(this).attr('id');
    var visible = $(this).is(":visible");

    if(visible) {
        var simplemde = new SimpleMDE({
            element: document.getElementById(thisID),
            toolbar: ["bold", "italic", "heading", "|",
                      "quote", "unordered-list", "ordered-list", "|",
                      "link", "image", "|",
                      "preview", "side-by-side", "fullscreen", "|",
                      "table", "horizontal-rule", "code", "|",
                      "guide"
                      ],
            spellChecker: false,
            promptURLs: true,
            // forceSync: true,
        });
    }
}

$(window).load(function(){
	$('.InputfieldSimpleMDEField').each(initSimpleMDE);
});

// Fix for things hidden on pageload, by @Ethan Beyer
$(document).on('wiretabclick', function() {
    $('.InputfieldSimpleMDEField').each(initSimpleMDE);
});

 

Link to comment
Share on other sites

"wiretabclick" fires on each tab click so I would suggest to add a data-attribute to the SimpeMDE field and check for its existence before calling initSimpleMDE (eg. add "data-loaded").

Link to comment
Share on other sites

The biggest reason I made sure to include "wiretabclick" was because I have multiple tabs with different SimpleMDE fields on them - they all showed up blank when clicking on their tab until you clicked in the SimpleMDE field itself. This was the only way I could get the values to show up on tabs.

Link to comment
Share on other sites

I see, but there's no sense initializing the same SimpleMDE field several times (clicking on tabs back and forth). I would add a "data-mce-loaded" attribute on first load and return from the initSimpleMDE() function on second (and consequent) runs if this tag exists on the field in question.

  • Like 2
Link to comment
Share on other sites

actually looks like this is related to Codemirror refresh, so the fix is all about being able to call the refresh() method on the codemirror instance based on a js event, such as the wiretab click, or the inputfield collapsed. Other option which is out there is to have an interval refresh running on the page, which is an addon for codemirror which helps in situations where you have a lot of instances where the codemirror does not init; @ethfun your fix doesn't work actually because then any non-visible fields on the content tab (like if they are collapsed) don't get initialized till you click a tab... to be continued

Link to comment
Share on other sites

I've done some more digging. The field with SimpleMDE definitely needs to be refreshed, but it is possible to just "init" that field if we hook into the correct triggered events.

I've tested this with Textarea fields using SimpleMDE as their Inputfield:

  • If the field is closed;
  • or if the field is within a Fieldset that is closed;
  • or if the field is on a tab that is not visible on page load...

The following code fixes all of those cases:

/**
 * Init for Processwire
 * These items could be added if fullscreen or sidebyside would work on PW
 * Note that fullscreen mode is not working right on Reno theme
 */
var initSimpleMDE = function() {
    var thisID = $(this).attr('id');
    var visible = $(this).is(":visible");

    if(visible && $(this).data('simplemde-made') !== 'true') {
        $(this).attr('data-simpleMDE-made', 'true');
        // console.log($(this).data());
        var simplemde = new SimpleMDE({
            element: document.getElementById(thisID),
            toolbar: ["bold", "italic", "heading", "|",
                      "quote", "unordered-list", "ordered-list", "|",
                      "link", "image", "|",
                      "preview", "side-by-side", "fullscreen", "|",
                      "table", "horizontal-rule", "code", "|",
                      "guide"
                      ],
            spellChecker: false,
            promptURLs: true,
            // forceSync: true,
        });
    }
}

/**
 * Init the field on page load
 */
$(window).load(function(){
    $('.InputfieldSimpleMDEField').each(initSimpleMDE);
});

/**
 * Re-init for fields that are now visible due to:
 *     - opening a tab with a hidden field, making it visible
 *     - opening an accordion with a hidden field, making it visible
 *     - opening a field that was closed
 */
$(document).on('wiretabclick reloaded opened', function() {
    // console.log("got here");
    $('.InputfieldSimpleMDEField').each(initSimpleMDE);
});

 

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...