Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
DEV: Apply prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaylorhq committed Oct 22, 2018
1 parent bce9f31 commit 0cfc82a
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 75 deletions.
85 changes: 42 additions & 43 deletions assets/javascripts/discourse/initializers/checklist.js.es6
Original file line number Diff line number Diff line change
@@ -1,73 +1,72 @@
import { withPluginApi } from 'discourse/lib/plugin-api';
import AjaxLib from 'discourse/lib/ajax';
import TextLib from 'discourse/lib/text';
import { withPluginApi } from "discourse/lib/plugin-api";
import AjaxLib from "discourse/lib/ajax";
import TextLib from "discourse/lib/text";

function initializePlugin(api)
{
const siteSettings = api.container.lookup('site-settings:main');
function initializePlugin(api) {
const siteSettings = api.container.lookup("site-settings:main");

if (siteSettings.checklist_enabled)
{
if (siteSettings.checklist_enabled) {
api.decorateCooked(checklistSyntax);
}
}

export function checklistSyntax($elem, post)
{
if (!post) { return; }
export function checklistSyntax($elem, post) {
if (!post) {
return;
}

var boxes = $elem.find(".chcklst-box"),
viewPost = post.getModel();

if (!viewPost.can_edit) { return; }
if (!viewPost.can_edit) {
return;
}

boxes.each(function(idx, val)
{
$(val).click(function(ev)
{
boxes.each(function(idx, val) {
$(val).click(function(ev) {
var elem = $(ev.currentTarget),
new_value = elem.hasClass("checked") ? "[ ]": "[\\*]";
new_value = elem.hasClass("checked") ? "[ ]" : "[\\*]";

elem.after('<i class="fa fa-spinner fa-spin fa-fw"></i>');
elem.hide();

var postId = viewPost.id;
AjaxLib.ajax("/posts/" + postId, { type: 'GET', cache: false }).then(function(result)
{
var nth = -1, // make the first run go to index = 0
new_raw = result.raw.replace(/\[(\s|\_|\-|\x|\\?\*)?\]/ig, function(match)
{
nth += 1;
return nth === idx ? new_value : match;
});
AjaxLib.ajax("/posts/" + postId, { type: "GET", cache: false }).then(
function(result) {
var nth = -1, // make the first run go to index = 0
new_raw = result.raw.replace(/\[(\s|\_|\-|\x|\\?\*)?\]/gi, function(
match
) {
nth += 1;
return nth === idx ? new_value : match;
});

var props = {
raw: new_raw,
edit_reason: I18n.t("checklist.edit_reason"),
};
var props = {
raw: new_raw,
edit_reason: I18n.t("checklist.edit_reason")
};

if (TextLib.cookAsync) {
TextLib.cookAsync(new_raw).then(cooked => {
props.cooked = cooked.string;
if (TextLib.cookAsync) {
TextLib.cookAsync(new_raw).then(cooked => {
props.cooked = cooked.string;
viewPost.save(props);
});
} else {
props.cooked = TextLib.cook(new_raw).string;
viewPost.save(props);
});
} else {
props.cooked = TextLib.cook(new_raw).string;
viewPost.save(props);
}
}

});
);
});
});

// confirm the feature is enabled by showing the click-ability
boxes.css({"cursor": "pointer"});
boxes.css({ cursor: "pointer" });
}

export default {
name: 'checklist',
initialize: function()
{
withPluginApi('0.1', api => initializePlugin(api));
name: "checklist",
initialize: function() {
withPluginApi("0.1", api => initializePlugin(api));
}
};
71 changes: 40 additions & 31 deletions assets/javascripts/lib/discourse-markdown/checklist.js.es6
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { registerOption } from 'pretty-text/pretty-text';
import { registerOption } from "pretty-text/pretty-text";

const REGEX = /\[(\s?|_|-|x|\*)\]/ig;
const REGEX = /\[(\s?|_|-|x|\*)\]/gi;

function getClasses(str) {
switch(str.toLowerCase()) {
switch (str.toLowerCase()) {
case "x":
return "checked fa fa-check-square fa-fw";
case "*":
Expand All @@ -19,25 +19,23 @@ function getClasses(str) {

function addCheckbox(result, content, match, state) {
let classes = getClasses(match[1]);
let token = new state.Token('check_open', 'span', 1);
token.attrs = [['class', `chcklst-box ${classes}`]];
let token = new state.Token("check_open", "span", 1);
token.attrs = [["class", `chcklst-box ${classes}`]];
result.push(token);

token = new state.Token('check_close', 'span', -1);
token = new state.Token("check_close", "span", -1);
result.push(token);
}

function applyCheckboxes(content, state) {

let result = null,
pos = 0,
match;

while (match = REGEX.exec(content)) {
pos = 0,
match;

while ((match = REGEX.exec(content))) {
if (match.index > pos) {
result = result || [];
let token = new state.Token('text', '', 0);
let token = new state.Token("text", "", 0);
token.content = content.slice(pos, match.index);
result.push(token);
}
Expand All @@ -49,7 +47,7 @@ function applyCheckboxes(content, state) {
}

if (result && pos < content.length) {
let token = new state.Token('text', '', 0);
let token = new state.Token("text", "", 0);
token.content = content.slice(pos);
result.push(token);
}
Expand All @@ -58,12 +56,18 @@ function applyCheckboxes(content, state) {
}

function processChecklist(state) {
var i, j, l, tokens, token,
blockTokens = state.tokens,
nesting = 0;
var i,
j,
l,
tokens,
token,
blockTokens = state.tokens,
nesting = 0;

for (j = 0, l = blockTokens.length; j < l; j++) {
if (blockTokens[j].type !== 'inline') { continue; }
if (blockTokens[j].type !== "inline") {
continue;
}
tokens = blockTokens[j].children;

// We scan from the end, to keep position when new tags are added.
Expand All @@ -73,30 +77,35 @@ function processChecklist(state) {

nesting += token.nesting;

if (token.type === 'text' && nesting === 0) {
if (token.type === "text" && nesting === 0) {
let processed = applyCheckboxes(token.content, state);
if (processed) {
blockTokens[j].children = tokens = state.md.utils.arrayReplaceAt(tokens, i, processed);
blockTokens[j].children = tokens = state.md.utils.arrayReplaceAt(
tokens,
i,
processed
);
}
}
}
}

}

export function setup(helper) {
helper.registerOptions((opts, siteSettings)=>{
opts.features['checklist'] = !!siteSettings.checklist_enabled;
helper.registerOptions((opts, siteSettings) => {
opts.features["checklist"] = !!siteSettings.checklist_enabled;
});

helper.whiteList([ 'span.chcklst-stroked',
'span.chcklst-box fa fa-square-o fa-fw',
'span.chcklst-box fa fa-square fa-fw',
'span.chcklst-box fa fa-minus-square-o fa-fw',
'span.chcklst-box checked fa fa-check-square fa-fw',
'span.chcklst-box checked fa fa-check-square-o fa-fw' ]);

helper.registerPlugin(md =>{
md.core.ruler.push('checklist', processChecklist);
helper.whiteList([
"span.chcklst-stroked",
"span.chcklst-box fa fa-square-o fa-fw",
"span.chcklst-box fa fa-square fa-fw",
"span.chcklst-box fa fa-minus-square-o fa-fw",
"span.chcklst-box checked fa fa-check-square fa-fw",
"span.chcklst-box checked fa fa-check-square-o fa-fw"
]);

helper.registerPlugin(md => {
md.core.ruler.push("checklist", processChecklist);
});
}
2 changes: 1 addition & 1 deletion assets/stylesheets/checklist.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
span.chcklst-stroked {
text-decoration: line-through;
}
}

0 comments on commit 0cfc82a

Please sign in to comment.