Skip to content

Commit

Permalink
Merge pull request #267 from weseek/rc/2.3.9
Browse files Browse the repository at this point in the history
release v2.3.9
  • Loading branch information
yuki-takei authored Feb 15, 2018
2 parents de2ce2e + 89606fb commit dfcb1b8
Show file tree
Hide file tree
Showing 14 changed files with 266 additions and 157 deletions.
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
CHANGES
========

## 2.3.9-RC

* Fix: `Ctrl-/` doesn't work on Chrome
* Fix: Close Shortcuts help with `Ctrl-/`, ESC key
* Fix: Jump to last line wrongly when `.revision-head-edit-button` clicked
* Support: Upgrade libs
* googleapis

## 2.3.8

* Feature: Suggest page path when creating pages
Expand Down
34 changes: 12 additions & 22 deletions lib/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,34 +436,24 @@ module.exports = function(crowi) {


userSchema.statics.findUserByUsername = function(username) {
var User = this;
return new Promise(function(resolve, reject) {
User.findOne({username: username}, function (err, userData) {
if (err) {
return reject(err);
}

return resolve(userData);
});
});
if (username == null) {
return Promise.resolve(null);
}
return this.findOne({username});
};

userSchema.statics.findUserByApiToken = function(apiToken) {
var self = this;

return new Promise(function(resolve, reject) {
self.findOne({apiToken: apiToken}, function (err, userData) {
if (err) {
return reject(err);
} else {
return resolve(userData);
}
});
});
if (apiToken == null) {
return Promise.resolve(null);
}
return this.findOne({apiToken});
};

userSchema.statics.findUserByGoogleId = function(googleId, callback) {
this.findOne({googleId: googleId}, function (err, userData) {
if (googleId == null) {
callback(null, null);
}
this.findOne({googleId}, function (err, userData) {
callback(err, userData);
});
};
Expand Down
1 change: 1 addition & 0 deletions lib/routes/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ module.exports = function(crowi, app) {
User.findUserByGoogleId(googleId, function(err, userData) {
debug('findUserByGoogleId', err, userData);
if (!userData) {
clearGoogleSession(req);
return loginFailure(req, res);
}
return loginSuccess(req, res, userData);
Expand Down
8 changes: 5 additions & 3 deletions lib/util/googleAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
module.exports = function(config) {
'use strict';

var google = require('googleapis')
const { GoogleApis } = require('googleapis');
var google = new GoogleApis()
, debug = require('debug')('crowi:lib:googleAuth')
, lib = {}
;
Expand Down Expand Up @@ -58,8 +59,9 @@ module.exports = function(config) {
return callback(new Error('[googleAuth.handleCallback] Error while proceccing userinfo.get.'), null);
}

response.user_id = response.id; // This is for B.C. (tokeninfo をつかっている前提のコードに対してのもの)
return callback(null, response);
const data = response.data;
data.user_id = data.id; // This is for B.C. (tokeninfo をつかっている前提のコードに対してのもの)
return callback(null, data);
});
});
};
Expand Down
24 changes: 21 additions & 3 deletions lib/util/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,33 @@ SearchClient.prototype.addAllPages = function()
var Page = this.crowi.model('Page');
var cursor = Page.getStreamOfFindAll();
var body = [];
var sent = 0;
var skipped = 0;

return new Promise(function(resolve, reject) {
cursor.on('data', function (doc) {
if (!doc.creator || !doc.revision || !self.shouldIndexed(doc)) {
debug('Skipped', doc.path);
//debug('Skipped', doc.path);
skipped++;
return ;
}

self.prepareBodyForCreate(body, doc);
//debug(body.length);
if (body.length > 2000) {
sent++;
debug('Sending request (seq, skipped)', sent, skipped);
self.client.bulk({
body: body,
requestTimeout: Infinity,
}).then(res => {
debug('addAllPages add anyway (items, errors, took): ', (res.items || []).length, res.errors, res.took)
}).catch(err => {
debug('addAllPages error on add anyway: ', err)
});

body = [];
}
}).on('error', function (err) {
// TODO: handle err
debug('Error cursor:', err);
Expand All @@ -231,13 +249,13 @@ SearchClient.prototype.addAllPages = function()
return resolve();
}

// 最後に送信
// 最後にすべてを送信
self.client.bulk({
body: body,
requestTimeout: Infinity,
})
.then(function(res) {
debug('Reponse from es:', res);
debug('Reponse from es (item length, errros, took):', (res.items || []).length, res.errors, res.took);
return resolve(res);
}).catch(function(err) {
debug('Err from es:', err);
Expand Down
2 changes: 1 addition & 1 deletion lib/views/modal/shortcuts.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="modal" id="shortcuts-modal">
<div class="modal" id="shortcuts-modal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "crowi-plus",
"version": "2.3.8-RC",
"version": "2.3.9-RC",
"description": "Enhanced Crowi",
"tags": [
"wiki",
Expand Down Expand Up @@ -83,7 +83,7 @@
"express-webpack-assets": "^0.1.0",
"file-loader": "^1.1.0",
"get-line-from-pos": "^1.0.0",
"googleapis": "^25.0.0",
"googleapis": "^26.0.0",
"graceful-fs": "^4.1.11",
"highlight.js": "^9.10.0",
"i18next": "^10.0.1",
Expand Down
4 changes: 3 additions & 1 deletion resource/js/components/NewPageNameInputter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from 'react';
import { FormGroup, Button, InputGroup } from 'react-bootstrap';
import { FormGroup } from 'react-bootstrap/es/FormGroup';
import { Button } from 'react-bootstrap/es/Button';
import { InputGroup } from 'react-bootstrap/es/InputGroup';

import UserPicture from './User/UserPicture';
import PageListMeta from './PageList/PageListMeta';
Expand Down
7 changes: 4 additions & 3 deletions resource/js/components/PageEditor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require('codemirror/theme/twilight.css');
import Dropzone from 'react-dropzone';

import pasteHelper from './PasteHelper';
import markdownListHelper from './MarkdownListHelper';
import emojiAutoCompleteHelper from './EmojiAutoCompleteHelper';


Expand Down Expand Up @@ -97,8 +98,8 @@ export default class Editor extends React.Component {
const editor = this.getCodeMirror();

// scroll to the bottom for a moment
const eol = editor.getDoc().lineCount() - 1;
editor.scrollIntoView(eol);
const lastLine = editor.getDoc().lastLine();
editor.scrollIntoView(lastLine);

const linePosition = Math.max(0, line - 1);
editor.scrollIntoView(linePosition);
Expand Down Expand Up @@ -318,7 +319,7 @@ export default class Editor extends React.Component {
highlightFormatting: true,
// continuelist, indentlist
extraKeys: {
"Enter": "newlineAndIndentContinueMarkdownList",
"Enter": markdownListHelper.newlineAndIndentContinueMarkdownList,
"Tab": "indentMore",
"Shift-Tab": "indentLess",
"Ctrl-Q": (cm) => { cm.foldCode(cm.getCursor()) },
Expand Down
167 changes: 167 additions & 0 deletions resource/js/components/PageEditor/MarkdownListHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import * as codemirror from 'codemirror';

class MarkdownListHelper {

constructor() {
// https://github.com/codemirror/CodeMirror/blob/c7853a989c77bb9f520c9c530cbe1497856e96fc/addon/edit/continuelist.js#L14
// https://regex101.com/r/7BN2fR/5
this.indentAndMarkRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]\s|[*+-]\s|(\d+)([.)]))(\s*)/;
this.indentAndMarkOnlyRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]|[*+-]|(\d+)[.)])(\s*)$/;

this.newlineAndIndentContinueMarkdownList = this.newlineAndIndentContinueMarkdownList.bind(this);
this.pasteText = this.pasteText.bind(this);

this.getBol = this.getBol.bind(this);
this.getEol = this.getEol.bind(this);
this.getStrFromBol = this.getStrFromBol.bind(this);
this.getStrToEol = this.getStrToEol.bind(this);
}

/**
* wrap codemirror.commands.newlineAndIndentContinueMarkdownList
* @param {any} editor An editor instance of CodeMirror
*/
newlineAndIndentContinueMarkdownList(editor) {
// get strings from current position to EOL(end of line) before break the line
const strToEol = this.getStrToEol(editor);

if (this.indentAndMarkRE.test(strToEol)) {
codemirror.commands.newlineAndIndent(editor);
// replace the line with strToEol (abort auto indent)
editor.getDoc().replaceRange(strToEol, this.getBol(editor), this.getEol(editor));
}
else {
codemirror.commands.newlineAndIndentContinueMarkdownList(editor);
}
}

/**
* paste text
* @param {any} editor An editor instance of CodeMirror
* @param {any} event
* @param {string} text
*/
pasteText(editor, event, text) {
// get strings from BOL(beginning of line) to current position
const strFromBol = this.getStrFromBol(editor);

const matched = strFromBol.match(this.indentAndMarkRE);
// when match indentAndMarkOnlyRE
// (this means the current position is the beginning of the list item)
if (this.indentAndMarkOnlyRE.test(strFromBol)) {
const adjusted = this.adjustPastedData(strFromBol, text);

// replace
if (adjusted != null) {
event.preventDefault();
editor.getDoc().replaceRange(adjusted, this.getBol(editor), editor.getCursor());
}
}
}

/**
* return adjusted pasted data by indentAndMark
*
* @param {string} indentAndMark
* @param {string} text
* @returns adjusted pasted data
* returns null when adjustment is not necessary
*/
adjustPastedData(indentAndMark, text) {
let adjusted = null;

// list data (starts with indent and mark)
if (text.match(this.indentAndMarkRE)) {
const indent = indentAndMark.match(this.indentAndMarkRE)[1];

// splice to an array of line
const lines = text.match(/[^\r\n]+/g);
// indent
const replacedLines = lines.map((line) => {
return indent + line;
})

adjusted = replacedLines.join('\n');
}
// listful data
else if (this.isListfulData(text)) {
// do nothing (return null)
}
// not listful data
else {
// append `indentAndMark` at the beginning of all lines (except the first line)
const replacedText = text.replace(/(\r\n|\r|\n)/g, "$1" + indentAndMark);
// append `indentAndMark` to the first line
adjusted = indentAndMark + replacedText;
}

return adjusted;
}

/**
* evaluate whether `text` is list like data or not
* @param {string} text
*/
isListfulData(text) {
// return false if includes at least one blank line
// see https://stackoverflow.com/a/16369725
if (text.match(/^\s*[\r\n]/m) != null) {
return false;
}

const lines = text.match(/[^\r\n]+/g);
// count lines that starts with indent and mark
let isListful = false;
let count = 0;
lines.forEach((line) => {
if (line.match(this.indentAndMarkRE)) {
count++;
}
// ensure to be true if it is 50% or more
if (count >= lines.length / 2) {
isListful = true;
return;
}
});

return isListful;
}

/**
* return the postion of the BOL(beginning of line)
*/
getBol(editor) {
const curPos = editor.getCursor();
return { line: curPos.line, ch: 0 };
}

/**
* return the postion of the EOL(end of line)
*/
getEol(editor) {
const curPos = editor.getCursor();
const lineLength = editor.getDoc().getLine(curPos.line).length;
return { line: curPos.line, ch: lineLength };
}

/**
* return strings from BOL(beginning of line) to current position
*/
getStrFromBol(editor) {
const curPos = editor.getCursor();
return editor.getDoc().getRange(this.getBol(editor), curPos);
}

/**
* return strings from current position to EOL(end of line)
*/
getStrToEol(editor) {
const curPos = editor.getCursor();
return editor.getDoc().getRange(curPos, this.getEol(editor));
}
}

// singleton pattern
const instance = new MarkdownListHelper();
Object.freeze(instance);
export default instance;
Loading

0 comments on commit dfcb1b8

Please sign in to comment.