forked from Tiny-Giant/myuserscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInlineEditingEverywhere.user.js
176 lines (143 loc) · 6.6 KB
/
InlineEditingEverywhere.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// ==UserScript==
// @name Inline Editing Everywhere
// @namespace https://github.com/Tiny-Giant/
// @version 1.0.0.3
// @description Enables inline editing on any site in the Stack Exchange network.
// @author @TinyGiant
// @grant none
// @include /^https?://\w*.?(stackoverflow|stackexchange|serverfault|superuser|askubuntu|stackapps)\.com/(questions|posts|review)/(?!tagged|new).*/
// ==/UserScript==
function crossFade(outElem, inElem, duration, callback) {
inElem.fadeIn(duration, callback);
outElem.hide();
return;
}
// Enable inline editing everywhere, even if you don't have the privilege.
$('.post-menu .suggest-edit-post').live("click", function (evt) {
if (evt.ctrlKey || evt.altKey || evt.metaKey || evt.shiftKey) return; // allow open new window etc
var e = $(this), root = e.closest(".question, .answer");
if (e.data('handling-event') == 1) return false;
e.data('handling-event', 1);
if (root.find("#edit-tags-cancel").length) { // inline tag editor is active
StackExchange.inlineTagEditing.restoreCurrentTags(true);
}
var showEditorButton = $("#show-editor-button");
var showEditorButtonWasHidden = showEditorButton.is(':visible');
if (showEditorButtonWasHidden) {
showEditorButton.hide();
}
e.addSpinner();
var answer = "";
var isAnswer = root.hasClass("answer");
if (isAnswer) {
answer = " class='answer'";
}
var existing = root.find('.postcell,.answercell');
var toBeHidden = existing.find('> *');
var div = $('<div class="inline-editor"/>').hide().appendTo(existing);
// PostsController.InlineEdit: posts/{id:INT}/edit-inline
div.load(e.attr('href') + '-inline', function (r, status, xhr) {
var originalTitle;
div.find("[tabindex]").each(function () {
$(this).attr("tabindex", parseInt($(this).attr("tabindex") - 20));
});
StackExchange.helpers.removeSpinner();
if (root.offset().top < $(window).scrollTop()) // don't scroll if the top of the post is visible
$('html, body').animate({ scrollTop: $(root).offset().top - 55 }, 200);
if (status == 'error') {
StackExchange.helpers.showErrorMessage(existing, "The post could not be loaded");
div.remove();
e.data('handling-event', 0);
} else {
crossFade(toBeHidden, div, 300);
var targetID = e.attr('href').match(/\d+/)[0];
var cancelEdit = function (elem) {
if (StackExchange.navPrevention) {
if (!StackExchange.navPrevention.confirm("You have started editing this post. Abandon this edit?"))
return false;
StackExchange.navPrevention.stop();
}
if (showEditorButtonWasHidden) {
showEditorButton.show();
}
e.data('handling-event', 0);
crossFade(div, elem, 300, function () { div.remove(); });
if (elem.offset().top < $(window).scrollTop()) // don't scroll if the top of the post is visible
$('html, body').animate({ scrollTop: $(root).offset().top - 55 }, 200);
return false;
};
var updateTitleEvent = "input keyup";
var resetTitle = function () {
title.unbind(updateTitleEvent, updateTitle);
setTitleDelayed.trigger(originalTitle);
};
var updateTitle = function (evt) {
setTitleDelayed.trigger(title[0].value);
return true;
};
div.find('.cancel-edit').click(function () { resetTitle(); cancelEdit(toBeHidden); });
var saveNewDefaultDelayed = StackExchange.helpers.DelayedReaction(function (hide) {
$.ajax({
type: "POST",
data: { hide: hide },
url: "/user/save-pref/hide-preview-for-inline-editing"
});
}, 1000, { sliding: true });
var hidePreview = div.find('.hide-preview');
hidePreview.click(function () {
var preview = div.find(".wmd-preview");
var visible = preview.is(":visible");
if (visible) {
preview.slideUp();
hidePreview.text("show preview");
} else {
preview.slideDown();
hidePreview.text("hide preview");
}
saveNewDefaultDelayed.trigger(visible);
return false;
});
var btn = div.find('input[type="submit"]');
var postfix = btn.attr('id').replace('submit-button', '');
var form = div.find('form');
var title = div.find('#title');
var header = $("#question-header a");
var originalTitleSuffix = title.data('question-state-suffix');
originalTitleSuffix = originalTitleSuffix ? ' ' + originalTitleSuffix : '';
originalTitle = title.val();
var setTitleDelayed = StackExchange.helpers.DelayedReaction(function (text) {
if (typeof text != 'undefined') {
header.text(text + originalTitleSuffix);
if (typeof MathJax != 'undefined')
MathJax.Hub.Queue(["Typeset", MathJax.Hub, header[0]]);
}
}, { sliding: true });
title.bind(updateTitleEvent, updateTitle);
div.find('#title,.wmd-input,#tagnames,.edit-comment').keydown(function (evt) {
if (evt.ctrlKey === true && evt.keyCode == 13) {
form.submit();
return false;
}
if (evt.keyCode == 27) {
resetTitle();
cancelEdit(toBeHidden);
return false;
}
});
StackExchange.using("postValidation", function () {
StackExchange.postValidation.initOnBlurAndSubmit(form, isAnswer ? 2 : 1, 'edit', false, function (json) {
var html = json.html;
var elem = $(html).hide();
existing.replaceWith(elem);
if (!isAnswer) {
$("#question-header a").text(json.title);
}
$('html').trigger('inline-edit-complete', [elem, json.title]);
styleCode();
cancelEdit(elem); // reverts the UI
});
});
}
});
return false;
});