1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-07-30 04:23:11 +03:00

Ran eslint fix on existing codebase

Had to do some manual fixing of the app.js file due to misplaced
comments
This commit is contained in:
Dan Brown
2023-04-18 22:20:02 +01:00
parent 752ee664c2
commit e711290d8b
106 changed files with 905 additions and 869 deletions

View File

@ -1,6 +1,7 @@
import DrawIO from "../services/drawio";
import DrawIO from '../services/drawio';
export class Actions {
/**
* @param {MarkdownEditor} editor
*/
@ -29,13 +30,13 @@ export class Actions {
}
showImageInsert() {
/** @type {ImageManager} **/
/** @type {ImageManager} * */
const imageManager = window.$components.first('image-manager');
imageManager.show(image => {
const imageUrl = image.thumbs.display || image.url;
const selectedText = this.#getSelectionText();
const newText = "[![" + (selectedText || image.name) + "](" + imageUrl + ")](" + image.url + ")";
const newText = `[![${selectedText || image.name}](${imageUrl})](${image.url})`;
this.#replaceSelection(newText, newText.length);
}, 'gallery');
}
@ -49,12 +50,12 @@ export class Actions {
const selectedText = this.#getSelectionText();
const newText = `[${selectedText}]()`;
const cursorPosDiff = (selectedText === '') ? -3 : -1;
this.#replaceSelection(newText, newText.length+cursorPosDiff);
this.#replaceSelection(newText, newText.length + cursorPosDiff);
}
showImageManager() {
const selectionRange = this.#getSelectionRange();
/** @type {ImageManager} **/
/** @type {ImageManager} * */
const imageManager = window.$components.first('image-manager');
imageManager.show(image => {
this.#insertDrawing(image, selectionRange);
@ -65,7 +66,7 @@ export class Actions {
showLinkSelector() {
const selectionRange = this.#getSelectionRange();
/** @type {EntitySelectorPopup} **/
/** @type {EntitySelectorPopup} * */
const selector = window.$components.first('entity-selector-popup');
selector.show(entity => {
const selectedText = this.#getSelectionText(selectionRange) || entity.name;
@ -81,16 +82,13 @@ export class Actions {
const selectionRange = this.#getSelectionRange();
DrawIO.show(url,() => {
return Promise.resolve('');
}, (pngData) => {
DrawIO.show(url, () => Promise.resolve(''), pngData => {
const data = {
image: pngData,
uploaded_to: Number(this.editor.config.pageId),
};
window.$http.post("/images/drawio", data).then(resp => {
window.$http.post('/images/drawio', data).then(resp => {
this.#insertDrawing(resp.data, selectionRange);
DrawIO.close();
}).catch(err => {
@ -106,7 +104,7 @@ export class Actions {
// Show draw.io if enabled and handle save.
editDrawing(imgContainer) {
const drawioUrl = this.editor.config.drawioUrl;
const {drawioUrl} = this.editor.config;
if (!drawioUrl) {
return;
}
@ -114,16 +112,13 @@ export class Actions {
const selectionRange = this.#getSelectionRange();
const drawingId = imgContainer.getAttribute('drawio-diagram');
DrawIO.show(drawioUrl, () => {
return DrawIO.load(drawingId);
}, (pngData) => {
DrawIO.show(drawioUrl, () => DrawIO.load(drawingId), pngData => {
const data = {
image: pngData,
uploaded_to: Number(this.editor.config.pageId),
};
window.$http.post("/images/drawio", data).then(resp => {
window.$http.post('/images/drawio', data).then(resp => {
const newText = `<div drawio-diagram="${resp.data.id}"><img src="${resp.data.url}"></div>`;
const newContent = this.#getText().split('\n').map(line => {
if (line.indexOf(`drawio-diagram="${drawingId}"`) !== -1) {
@ -150,7 +145,7 @@ export class Actions {
// Make the editor full screen
fullScreen() {
const container = this.editor.config.container;
const {container} = this.editor.config;
const alreadyFullscreen = container.classList.contains('fullscreen');
container.classList.toggle('fullscreen', !alreadyFullscreen);
document.body.classList.toggle('markdown-fullscreen', !alreadyFullscreen);
@ -204,7 +199,7 @@ export class Actions {
content = this.#cleanTextForEditor(content);
const selectionRange = this.#getSelectionRange();
const selectFrom = selectionRange.from + content.length + 1;
this.#dispatchChange(0, 0, content + '\n', selectFrom);
this.#dispatchChange(0, 0, `${content}\n`, selectFrom);
this.focus();
}
@ -214,7 +209,7 @@ export class Actions {
*/
appendContent(content) {
content = this.#cleanTextForEditor(content);
this.#dispatchChange(this.editor.cm.state.doc.length, '\n' + content);
this.#dispatchChange(this.editor.cm.state.doc.length, `\n${content}`);
this.focus();
}
@ -223,7 +218,7 @@ export class Actions {
* @param {String} content
*/
replaceContent(content) {
this.#setText(content)
this.#setText(content);
}
/**
@ -250,7 +245,7 @@ export class Actions {
if (alreadySymbol) {
newLineContent = lineContent.replace(lineStart, newStart).trim();
} else if (newStart !== '') {
newLineContent = newStart + ' ' + lineContent;
newLineContent = `${newStart} ${lineContent}`;
}
const selectFrom = selectionRange.from + (newLineContent.length - lineContent.length);
@ -290,7 +285,7 @@ export class Actions {
const number = (Number(listMatch[2]) || 0) + 1;
const whiteSpace = listMatch[1] || '';
const listMark = listMatch[3] || '.'
const listMark = listMatch[3] || '.';
const prefix = `${whiteSpace}${number}${listMark}`;
return this.replaceLineStart(prefix);
@ -373,7 +368,7 @@ export class Actions {
* @param {File} file
* @param {?Number} position
*/
async uploadImage(file, position= null) {
async uploadImage(file, position = null) {
if (file === null || file.type.indexOf('image') !== 0) return;
let ext = 'png';
@ -382,17 +377,17 @@ export class Actions {
}
if (file.name) {
let fileNameMatches = file.name.match(/\.(.+)$/);
const fileNameMatches = file.name.match(/\.(.+)$/);
if (fileNameMatches.length > 1) ext = fileNameMatches[1];
}
// Insert image into markdown
const id = "image-" + Math.random().toString(16).slice(2);
const id = `image-${Math.random().toString(16).slice(2)}`;
const placeholderImage = window.baseUrl(`/loading.gif#upload${id}`);
const placeHolderText = `![](${placeholderImage})`;
this.#dispatchChange(position, position, placeHolderText, position);
const remoteFilename = "image-" + Date.now() + "." + ext;
const remoteFilename = `image-${Date.now()}.${ext}`;
const formData = new FormData();
formData.append('file', file, remoteFilename);
formData.append('uploaded_to', this.editor.config.pageId);
@ -466,7 +461,7 @@ export class Actions {
* @return {String}
*/
#cleanTextForEditor(text) {
return text.replace(/\r\n|\r/g, "\n");
return text.replace(/\r\n|\r/g, '\n');
}
/**
@ -511,7 +506,7 @@ export class Actions {
* @param {?Number} selectTo
*/
#dispatchChange(from, to = null, text = null, selectFrom = null, selectTo = null) {
const tr = {changes: {from, to: to, insert: text}};
const tr = {changes: {from, to, insert: text}};
if (selectFrom) {
tr.selection = {anchor: selectFrom};
@ -533,4 +528,5 @@ export class Actions {
scrollIntoView,
});
}
}
}

View File

@ -1,6 +1,6 @@
import {provideKeyBindings} from "./shortcuts";
import {debounce} from "../services/util";
import Clipboard from "../services/clipboard";
import {provideKeyBindings} from './shortcuts';
import {debounce} from '../services/util';
import Clipboard from '../services/clipboard';
/**
* Initiate the codemirror instance for the markdown editor.
@ -25,9 +25,9 @@ export async function init(editor) {
const domEventHandlers = {
// Handle scroll to sync display view
scroll: (event) => syncActive && onScrollDebounced(event),
scroll: event => syncActive && onScrollDebounced(event),
// Handle image & content drag n drop
drop: (event) => {
drop: event => {
const templateId = event.dataTransfer.getData('bookstack/template');
if (templateId) {
event.preventDefault();
@ -43,7 +43,7 @@ export async function init(editor) {
}
},
// Handle image paste
paste: (event) => {
paste: event => {
const clipboard = new Clipboard(event.clipboardData || event.dataTransfer);
// Don't handle the event ourselves if no items exist of contains table-looking data
@ -55,8 +55,8 @@ export async function init(editor) {
for (const image of images) {
editor.actions.uploadImage(image);
}
}
}
},
};
const cm = Code.markdownEditor(
editor.config.inputEl,
@ -70,4 +70,4 @@ export async function init(editor) {
window.mdEditorView = cm;
return cm;
}
}

View File

@ -6,23 +6,22 @@ function getContentToInsert({html, markdown}) {
* @param {MarkdownEditor} editor
*/
export function listen(editor) {
window.$events.listen('editor::replace', (eventContent) => {
window.$events.listen('editor::replace', eventContent => {
const markdown = getContentToInsert(eventContent);
editor.actions.replaceContent(markdown);
});
window.$events.listen('editor::append', (eventContent) => {
window.$events.listen('editor::append', eventContent => {
const markdown = getContentToInsert(eventContent);
editor.actions.appendContent(markdown);
});
window.$events.listen('editor::prepend', (eventContent) => {
window.$events.listen('editor::prepend', eventContent => {
const markdown = getContentToInsert(eventContent);
editor.actions.prependContent(markdown);
});
window.$events.listen('editor::insert', (eventContent) => {
window.$events.listen('editor::insert', eventContent => {
const markdown = getContentToInsert(eventContent);
editor.actions.insertContent(markdown);
});
@ -30,4 +29,4 @@ export function listen(editor) {
window.$events.listen('editor::focus', () => {
editor.actions.focus();
});
}
}

View File

@ -1,4 +1,4 @@
import {patchDomFromHtmlString} from "../services/vdom";
import {patchDomFromHtmlString} from '../services/vdom';
export class Display {
@ -81,7 +81,7 @@ export class Display {
* @param {String} html
*/
patchWithHtml(html) {
const body = this.doc.body;
const {body} = this.doc;
if (body.children.length === 0) {
const wrap = document.createElement('div');
@ -102,8 +102,8 @@ export class Display {
const elems = this.doc.body?.children[0]?.children;
if (elems && elems.length <= index) return;
const topElem = (index === -1) ? elems[elems.length-1] : elems[index];
topElem.scrollIntoView({ block: 'start', inline: 'nearest', behavior: 'smooth'});
const topElem = (index === -1) ? elems[elems.length - 1] : elems[index];
topElem.scrollIntoView({block: 'start', inline: 'nearest', behavior: 'smooth'});
}
}
}

View File

@ -1,10 +1,9 @@
import {Markdown} from "./markdown";
import {Display} from "./display";
import {Actions} from "./actions";
import {Settings} from "./settings";
import {listen} from "./common-events";
import {init as initCodemirror} from "./codemirror";
import {Markdown} from './markdown';
import {Display} from './display';
import {Actions} from './actions';
import {Settings} from './settings';
import {listen} from './common-events';
import {init as initCodemirror} from './codemirror';
/**
* Initiate a new markdown editor instance.
@ -12,7 +11,6 @@ import {init as initCodemirror} from "./codemirror";
* @returns {Promise<MarkdownEditor>}
*/
export async function init(config) {
/**
* @type {MarkdownEditor}
*/
@ -31,7 +29,6 @@ export async function init(config) {
return editor;
}
/**
* @typedef MarkdownEditorConfig
* @property {String} pageId
@ -51,4 +48,4 @@ export async function init(config) {
* @property {Actions} actions
* @property {EditorView} cm
* @property {Settings} settings
*/
*/

View File

@ -1,4 +1,4 @@
import MarkdownIt from "markdown-it";
import MarkdownIt from 'markdown-it';
import mdTasksLists from 'markdown-it-task-lists';
export class Markdown {
@ -24,7 +24,5 @@ export class Markdown {
render(markdown) {
return this.renderer.render(markdown);
}
}

View File

@ -59,4 +59,5 @@ export class Settings {
listeners.push(callback);
this.changeListeners[key] = listeners;
}
}
}

View File

@ -34,8 +34,8 @@ function provide(editor) {
shortcuts['Mod-8'] = cm => editor.actions.wrapSelection('`', '`');
shortcuts['Shift-Mod-e'] = cm => editor.actions.wrapSelection('`', '`');
shortcuts['Mod-9'] = cm => editor.actions.cycleCalloutTypeAtSelection();
shortcuts['Mod-p'] = cm => editor.actions.replaceLineStart('-')
shortcuts['Mod-o'] = cm => editor.actions.replaceLineStartForOrderedList()
shortcuts['Mod-p'] = cm => editor.actions.replaceLineStart('-');
shortcuts['Mod-o'] = cm => editor.actions.replaceLineStartForOrderedList();
return shortcuts;
}
@ -46,14 +46,12 @@ function provide(editor) {
* @return {{key: String, run: function, preventDefault: boolean}[]}
*/
export function provideKeyBindings(editor) {
const shortcuts= provide(editor);
const shortcuts = provide(editor);
const keyBindings = [];
const wrapAction = (action) => {
return () => {
action();
return true;
};
const wrapAction = action => () => {
action();
return true;
};
for (const [shortcut, action] of Object.entries(shortcuts)) {
@ -61,4 +59,4 @@ export function provideKeyBindings(editor) {
}
return keyBindings;
}
}