This is a major change to draft internals. Previously there were quite a few cases where the draft system would say "draft saved", when in fact we just skipped saving. This commit ensures the draft system deals with draft ownership handover in a predictable way. For example: - Window 1 editing draft - Window 2 editing same draft at the same time Previously we would allow window 1 and 2 to just fight on the same draft each window overwriting the same draft over an over. This commit introduces an ownership concept where either window 1 or 2 win and user is prompted on the loser window to reload screen to correct the issue This also corrects edge cases where a user could have multiple browser windows open and posts in 1 window, later to post in the second window. Previously drafts would break in the second window, this corrects it.
34 lines
698 B
JavaScript
34 lines
698 B
JavaScript
import { ajax } from "discourse/lib/ajax";
|
|
const Draft = Discourse.Model.extend();
|
|
|
|
Draft.reopenClass({
|
|
clear(key, sequence) {
|
|
return ajax("/draft.json", {
|
|
type: "DELETE",
|
|
data: { draft_key: key, sequence }
|
|
});
|
|
},
|
|
|
|
get(key) {
|
|
return ajax("/draft.json", {
|
|
data: { draft_key: key },
|
|
dataType: "json"
|
|
});
|
|
},
|
|
|
|
getLocal(key, current) {
|
|
// TODO: implement this
|
|
return current;
|
|
},
|
|
|
|
save(key, sequence, data, clientId) {
|
|
data = typeof data === "string" ? data : JSON.stringify(data);
|
|
return ajax("/draft.json", {
|
|
type: "POST",
|
|
data: { draft_key: key, sequence, data, owner: clientId }
|
|
});
|
|
}
|
|
});
|
|
|
|
export default Draft;
|