@atlaskit/editor-plugin-placeholder
Version:
Placeholder plugin for @atlaskit/editor-core.
58 lines (57 loc) • 2.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.cycleThroughPlaceholderPrompts = void 0;
var _constants = require("./constants");
var cycleThroughPlaceholderPrompts = exports.cycleThroughPlaceholderPrompts = function cycleThroughPlaceholderPrompts(placeholderPrompts, activeTypewriterTimeouts, placeholderNodeWithText) {
var initialDelayWhenUserTypedAndDeleted = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
var currentPromptIndex = 0;
var displayedText = '';
var animationTimeouts = [];
var clearAllTimeouts = function clearAllTimeouts() {
animationTimeouts.forEach(function (timeoutId) {
return clearTimeout(timeoutId);
});
animationTimeouts = [];
};
var scheduleTimeout = function scheduleTimeout(callback, delay) {
var timeoutId = setTimeout(callback, delay);
animationTimeouts.push(timeoutId);
return timeoutId;
};
var _startAnimationCycle = function startAnimationCycle() {
var currentPrompt = placeholderPrompts[currentPromptIndex];
var characterIndex = 0;
var _typeNextCharacter = function typeNextCharacter() {
if (characterIndex < currentPrompt.length) {
displayedText = currentPrompt.substring(0, characterIndex + 1);
placeholderNodeWithText.textContent = displayedText;
characterIndex++;
scheduleTimeout(_typeNextCharacter, _constants.TYPEWRITER_TYPE_DELAY);
} else {
scheduleTimeout(_eraseLastCharacter, _constants.TYPEWRITER_PAUSE_BEFORE_ERASE);
}
};
var _eraseLastCharacter = function eraseLastCharacter() {
if (displayedText.length > 1) {
displayedText = displayedText.substring(0, displayedText.length - 1);
placeholderNodeWithText.textContent = displayedText;
scheduleTimeout(_eraseLastCharacter, _constants.TYPEWRITER_ERASE_DELAY);
} else {
displayedText = ' ';
placeholderNodeWithText.textContent = displayedText;
currentPromptIndex = (currentPromptIndex + 1) % placeholderPrompts.length;
scheduleTimeout(_startAnimationCycle, _constants.TYPEWRITER_CYCLE_DELAY);
}
};
_typeNextCharacter();
};
activeTypewriterTimeouts === null || activeTypewriterTimeouts === void 0 || activeTypewriterTimeouts.push(clearAllTimeouts);
if (initialDelayWhenUserTypedAndDeleted > 0) {
placeholderNodeWithText.textContent = ' ';
scheduleTimeout(_startAnimationCycle, initialDelayWhenUserTypedAndDeleted);
} else {
_startAnimationCycle();
}
};