UNPKG

electron-text-substitutions

Version:

Substitute text in an input field based on OS X System Preferences

88 lines (72 loc) 3.31 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.readSystemTextPreferences = readSystemTextPreferences; exports.readSystemTextPreferencesJSON = readSystemTextPreferencesJSON; exports.onPreferenceChanged = onPreferenceChanged; var _electron = require('electron'); var _electron2 = _interopRequireDefault(_electron); var _Observable = require('rxjs/Observable'); var _Subscription = require('rxjs/Subscription'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var systemPreferences = process.type === 'browser' ? _electron2.default.systemPreferences : _electron2.default.remote.systemPreferences; var userDefaultsTextSubstitutionsKey = 'NSUserDictionaryReplacementItems'; var userDefaultsSmartQuotesKey = 'NSAutomaticQuoteSubstitutionEnabled'; var userDefaultsSmartDashesKey = 'NSAutomaticDashSubstitutionEnabled'; var textPreferenceChangedKeys = ['IMKTextReplacementDidChangeNotification', 'NSAutomaticQuoteSubstitutionEnabledChanged', 'NSAutomaticDashSubstitutionEnabledChanged']; /** * Returns the current text substitution preferences. * * @return {Object} * @return {Object}.substitutions An array of text substitutions * @return {Object}.useSmartQuotes True if smart quotes are enabled * @return {Object}.useSmartDashes True if smart dashes are enabled */ function readSystemTextPreferences() { var substitutions = systemPreferences.getUserDefault(userDefaultsTextSubstitutionsKey, 'array') || []; var useSmartQuotes = systemPreferences.getUserDefault(userDefaultsSmartQuotesKey, 'boolean'); var useSmartDashes = systemPreferences.getUserDefault(userDefaultsSmartDashesKey, 'boolean'); return { substitutions: substitutions, useSmartQuotes: useSmartQuotes, useSmartDashes: useSmartDashes }; } /** * If we've required this module remotely, we want to serialize the text * preferences first, to avoid any remote objects in the result. */ function readSystemTextPreferencesJSON() { return JSON.stringify(readSystemTextPreferences()); } /** * Calls the provided method whenever text substitutions change. * * @param {Function} callback The method to call * @return {Subscription} Manages the event listener */ function onPreferenceChanged(callback) { if (!process || process.type !== 'browser') throw new Error('Not in an Electron browser context'); if (process.platform !== 'darwin') throw new Error('Only supported on macOS'); return _Observable.Observable.from(textPreferenceChangedKeys).mergeMap(function (key) { return observableForPreferenceChanged(key); }).debounceTime(100).subscribe(callback); } /** * Create an Observable that will emit when the given macOS system * notification fires. * * @param {String} preferenceChangedKey The key to listen for * @return {Observable} The cold Observable */ function observableForPreferenceChanged(preferenceChangedKey) { return _Observable.Observable.create(function (subj) { var subscriberId = systemPreferences.subscribeNotification(preferenceChangedKey, function () { subj.next(preferenceChangedKey); }); return new _Subscription.Subscription(function () { return systemPreferences.unsubscribeNotification(subscriberId); }); }); }