Skip to content

Commit

Permalink
fix: Auto-saved issue description keeps displaying for new issue even…
Browse files Browse the repository at this point in the history
… if it is saved (OD-2028)
  • Loading branch information
robinshine committed Aug 14, 2024
1 parent d623834 commit 6889eb3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import javax.servlet.http.HttpServletResponse;

import io.onedev.server.validation.validator.ProjectKeyValidator;
import io.onedev.server.web.page.base.BasePage;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.wicket.Component;
Expand Down Expand Up @@ -78,7 +79,7 @@

@SuppressWarnings("serial")
public class MarkdownEditor extends FormComponentPanel<String> {

protected static final int ATWHO_LIMIT = 10;

private static final Logger logger = LoggerFactory.getLogger(MarkdownEditor.class);
Expand Down Expand Up @@ -128,10 +129,8 @@ public MarkdownEditor(String id, IModel<String> model, boolean compactMode,
protected void onModelChanged() {
super.onModelChanged();
input.setModelObject(getModelObject());
var target = RequestCycle.get().find(AjaxRequestTarget.class);
var form = findParent(Form.class);
if (target != null && form != null)
target.prependJavaScript(String.format("onedev.server.form.clearAutosavings($('#%s'));", form.getMarkupId()));
if (getAutosaveKey() != null)
((BasePage) getPage()).removeAutosaveKey(getAutosaveKey());
}

public void clearMarkdown() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ onedev.server.markdown = {
var content = $input.val();
if (content.trim().length != 0)
localStorage.setItem(autosaveKey, content);
else
localStorage.removeItem(autosaveKey);
}

/*
Expand Down Expand Up @@ -963,7 +965,6 @@ onedev.server.markdown = {
}
var autosaveKey = $container.data("autosaveKey");
if (autosaveKey) {
onedev.server.form.registerAutosaveKey($container.closest("form.leave-confirm"), autosaveKey);
var autosaveValue = localStorage.getItem(autosaveKey);
if (autosaveValue && $input.val() != autosaveValue) {
$input.val(autosaveValue);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.onedev.server.web.page.base;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Sets;
Expand Down Expand Up @@ -29,7 +31,9 @@
import org.apache.commons.lang3.SerializationUtils;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.wicket.Component;
import org.apache.wicket.MetaDataKey;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.wicket.Session;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.AjaxSelfUpdatingTimerBehavior;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
Expand Down Expand Up @@ -68,6 +72,7 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static io.onedev.server.web.behavior.ChangeObserver.filterObservables;
import static io.onedev.server.web.page.admin.ssosetting.SsoProcessPage.MOUNT_PATH;
Expand All @@ -77,6 +82,9 @@
@SuppressWarnings("serial")
public abstract class BasePage extends WebPage {

private static final MetaDataKey<HashSet<String>> REMOVE_AUTOSAVE_KEYS = new MetaDataKey<>() {
};

private static final String COOKIE_DARK_MODE = "darkMode";

private boolean darkMode;
Expand Down Expand Up @@ -170,11 +178,19 @@ public void renderHead(IHeaderResponse response) {

response.render(JavaScriptHeaderItem.forReference(new BaseResourceReference()));

String jsonOfRemoveAutosaveKeys;
try {
jsonOfRemoveAutosaveKeys = OneDev.getInstance(ObjectMapper.class).writeValueAsString(getRemoveAutosaveKeys());
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
getSession().setMetaData(REMOVE_AUTOSAVE_KEYS, null);
response.render(OnDomReadyHeaderItem.forScript(
String.format("onedev.server.onDomReady('%s', '%s', %s);",
String.format("onedev.server.onDomReady('%s', '%s', %s, %s);",
String.valueOf(OneDev.getInstance().getBootDate().getTime()),
SpriteImage.getVersionedHref(IconScope.class, null),
popStateBehavior.getCallbackFunction(explicit("data")).toString())));
popStateBehavior.getCallbackFunction(explicit("data")).toString(),
jsonOfRemoveAutosaveKeys)));
response.render(OnLoadHeaderItem.forScript("onedev.server.onWindowLoad();"));
}

Expand Down Expand Up @@ -395,4 +411,22 @@ public boolean isSubscriptionActive() {
return WicketUtils.isSubscriptionActive();
}

public void removeAutosaveKey(String autosaveKey) {
var target = RequestCycle.get().find(AjaxRequestTarget.class);
if (target != null) {
target.prependJavaScript(String.format("localStorage.removeItem('%s');", autosaveKey));
} else {
var removeAutosaveKeys = getRemoveAutosaveKeys();
removeAutosaveKeys.add(autosaveKey);
getSession().setMetaData(REMOVE_AUTOSAVE_KEYS, removeAutosaveKeys);
}
}

public HashSet<String> getRemoveAutosaveKeys() {
var removeAutosaveKeys = getSession().getMetaData(REMOVE_AUTOSAVE_KEYS);
if (removeAutosaveKeys == null)
removeAutosaveKeys = new HashSet<>();
return removeAutosaveKeys;
}

}
29 changes: 8 additions & 21 deletions server-core/src/main/java/io/onedev/server/web/page/base/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ onedev.server = {
$form.submit(function(e) {
$form.removeClass("dirty");
setTimeout(function() {
$form.removeClass("dirty");
$form.removeClass("dirty");
}, 0);
});
},
Expand Down Expand Up @@ -169,26 +169,10 @@ onedev.server = {
$container = $(document);
var selector = "form.leave-confirm.dirty";
var $dirtyForms = $container.find(selector).addBack(selector);
if ($dirtyForms.length != 0) {
if (confirm("There are unsaved changes, do you want to discard and continue?")) {
onedev.server.form.clearAutosavings($dirtyForms);
return true;
} else {
return false;
}
} else {
if ($dirtyForms.length != 0)
return confirm("There are unsaved changes, do you want to discard and continue?");
else
return true;
}
},
clearAutosavings: function($dirtyForms) {
$dirtyForms.each(function() {
var autosaveKey = $(this).data("autosaveKey");
if (autosaveKey)
localStorage.removeItem(autosaveKey);
});
},
registerAutosaveKey: function($form, autosaveKey) {
$form.data("autosaveKey", autosaveKey);
}
},
isDarkMode: function() {
Expand Down Expand Up @@ -873,9 +857,12 @@ onedev.server = {
else
return "0s";
},
onDomReady: function(bootTimestamp, icons, popStateCallback) {
onDomReady: function(bootTimestamp, icons, popStateCallback, removeAutosaveKeys) {
onedev.server.bootTimestamp = bootTimestamp;
onedev.server.icons = icons;

for (var i in removeAutosaveKeys)
localStorage.removeItem(removeAutosaveKeys[i]);

// fix issue #1640
if (onedev.server.util.isMac() && onedev.server.util.isSafari()) {
Expand Down

0 comments on commit 6889eb3

Please sign in to comment.