UNPKG

delta-component

Version:

embeddable react component

148 lines (123 loc) 4.02 kB
'use strict'; import { socket, documents } from './helpers'; import { text } from '../../resources/text'; import { DEFAULT_CLIENTS_AWAIT_MSEC } from '../../../../server/logic/rebalance/constants'; export function rebalance() { const websocket = socket(); const awaitTimeout = parseInt( prompt( "Сколько msec выделить на работу клиентов?", DEFAULT_CLIENTS_AWAIT_MSEC.toString() ) || DEFAULT_CLIENTS_AWAIT_MSEC ); for(let document of documents()) { console.log(`Rebalance ${document}`); websocket.emit('Debug', document.documentString, 'rebalance', awaitTimeout); } } rebalance.keyToBind = 'R'; rebalance.description = 'Перебалансировать документы на странице'; let okToInputText = true; function* _words(txt) { let index = -1; while (true) { const nextSpace = txt.indexOf(' ', index + 1); if(nextSpace === -1) { break } const word = txt.slice(index, nextSpace).trim(); index = nextSpace; if(!word) continue; yield word; } } function _wait(timeout) { return new Promise(function (resolve) { setTimeout(resolve, timeout); }); } function emitter(document) { function emitToUI () { document.textChanged(document.getText()); document.emit('localSelectionChanged', document.getSelection()); } return { move: (left, right) => { document.moveSelection(left, right); emitToUI(); }, insert: (text) => { document.insert(text); const selection = document.getSelection(); document.moveSelection( selection.left + text.length, selection.left + text.length ); emitToUI(); }, delete: () => { document.delete(); const selection = document.getSelection(); document.moveSelection( selection.left, selection.left ); emitToUI(); } } } export async function inputText() { const document = documents()[0]; const awaitTimeout = parseInt(prompt("С какой частотой (msec) вставлять слова?", '2000') || 0); if (awaitTimeout === 0) { return; } const emit = emitter(document); emit.move(0, 0); let textLength; for(let word of _words(text)) { if(!okToInputText) { okToInputText = true; break; } textLength = document.getText().length; word = ' ' + word + ' '; if (Math.random() < 0.10) { word += '\n'; } const index = Math.max(Math.min( Math.round(Math.random() * textLength), textLength - 2 ), 1); await document.getUnlockedDocument(); emit.move(index, index); await _wait(awaitTimeout); for(const char of word) { await document.getUnlockedDocument(); emit.insert(char); await _wait(10); } await _wait(awaitTimeout); if(Math.random() < 0.40) { const selection = document.getSelection(); const [left, right] = [ selection.right - word.length, selection.right ]; await document.getUnlockedDocument(); emit.move(left, right); await _wait(awaitTimeout); await document.getUnlockedDocument(); emit.delete(); await _wait(awaitTimeout); } } console.log('Words finished'); } inputText.keyToBind = 'T'; inputText.description = 'Автоматически править документ'; export async function stopInputText() { okToInputText = false; } stopInputText.keyToBind = 'S'; stopInputText.description = 'Остановить правки документа';