Skip to content

Commit

Permalink
Merge pull request #486 from FluxNotes/fix-context-setting-for-placeh…
Browse files Browse the repository at this point in the history
…olders

set context based on location of placeholder instead of cursor
  • Loading branch information
nicoleng12 authored Oct 17, 2018
2 parents 00aa4c2 + 64913b8 commit 8d288c8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
14 changes: 14 additions & 0 deletions src/context/ContextManager.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ class ContextManager {
});
}

getContextsBeforeShortcut(shortcut, contextType = undefined) {
if (!shortcut.getKey()) return; // if the key hasn't been set for a shortcut yet then it shouldn't be added
let contextsBeforeShortcut = [];
this.contexts.forEach((context) => {
// check if provided shortcut is after the context. If yes, we have a valid context for the shortcut
if (!this.isBlock1BeforeBlock2(shortcut.getKey(), 0, context.getKey(), 0)) {
if (Lang.isUndefined(contextType) || Lang.isNull(contextType) || context.getShortcutType() === contextType) {
contextsBeforeShortcut.push(context);
}
}
});
return contextsBeforeShortcut;
}

addShortcutToContext(shortcut) {
if (!shortcut.getKey()) return; // if the key hasn't been set for a shortcut yet then it shouldn't be added
//console.log("adding shortcut to context: " + shortcut.getShortcutType());
Expand Down
19 changes: 14 additions & 5 deletions src/shortcuts/EntryShortcut.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,23 @@ export default class EntryShortcut extends Shortcut {
return !Lang.isUndefined(this.parentContext) && !Lang.isNull(this.parentContext);
}

establishParentContext(contextManager) {
establishParentContext(contextManager, relativeToShortcut = undefined) {
super.initialize(contextManager);
const knownParent = this.metadata["knownParentContexts"];

if (knownParent) {
this.parentContext = contextManager.getActiveContextOfType(knownParent);
} else {
this.parentContext = contextManager.getCurrentContext();
if (Lang.isUndefined(relativeToShortcut)) {
if (knownParent) {
this.parentContext = contextManager.getActiveContextOfType(knownParent);
} else {
this.parentContext = contextManager.getCurrentContext();
}
} else {
const potentialParents = contextManager.getContextsBeforeShortcut(this, knownParent);
if (potentialParents && potentialParents.length > 0) {
this.parentContext = potentialParents[0];
} else {
this.parentContext = undefined;
}
}

if (!Lang.isUndefined(this.parentContext)) {
Expand Down
2 changes: 1 addition & 1 deletion src/shortcuts/Placeholder.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Placeholder {

setAttributeValue(name, value, index = 0, source) {
if (!this._entryShortcuts[index].hasParentContext()) {
this._entryShortcuts[index].establishParentContext(this._contextManager);
this._entryShortcuts[index].establishParentContext(this._contextManager, this);
}

if (!this._entryShortcuts[index].hasParentContext()) {
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2976,9 +2976,9 @@ flexboxgrid@^6.3.0:
version "6.3.1"
resolved "https://registry.yarnpkg.com/flexboxgrid/-/flexboxgrid-6.3.1.tgz#e99898afc07b7047722bb81a958a5fba4d4e20fd"

flux_notes_treatment_options_rest_client@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/flux_notes_treatment_options_rest_client/-/flux_notes_treatment_options_rest_client-1.1.1.tgz#4b9497ccc7b9a9cd859661e147f4f3caf12ea20b"
flux_notes_treatment_options_rest_client@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/flux_notes_treatment_options_rest_client/-/flux_notes_treatment_options_rest_client-1.1.2.tgz#1cd2ddd8372116fef194c793e8e05e445f8a3495"
dependencies:
superagent "3.5.2"

Expand Down

0 comments on commit 8d288c8

Please sign in to comment.