معرف الموضوع كمتغير؟

الكلمة الشائعة هي عكس ما أريده. لا أريد أن تحتوي نتائج البحث الخاصة بي على كل تذكرة، بل أريد طريقة للبحث عن تذكرة معينة. لهذا السبب سيكون معرف الموضوع مثاليًا. يتم إنشاؤها تلقائيًا وهي فريدة.

لا، أعتقد أن الكود الذي أستخدمه كان أيضًا من awesomerobot في الأصل، لكن لا يمكنني العثور على مصدره بعد الآن.
أفضل مكان ما أستخدمه حاليًا على تلك الكتلة تحت النص.

حسنًا، لا يمكنني فعل ذلك. ربما يمكنك.
أنا أمتلك مجموعة معينة من المهارات. البرمجة ليست من بينها.

إذًا ربما يجب عليك نقل هذا الموضوع إلى Marketplace أو Feature.

إعجاب واحد (1)

أنت تفهمني بشكل خاطئ. كنت أعني وجود كلمة شائعة بما في ذلك المعلمة topic:id، بحيث تظهر النتائج من هذا الموضوع فقط. الكلمة الشائعة تزيل الحاجة إلى تغيير كلمات مثل “مرحباً” و “أهلاً”.

@tknospdr @pfaffman لقد قمت بتجميع مكون سريع يسمح لك بإدخال معرف الموضوع والانتقال إليه.

قم بإنشاء مكون جديد، وأضف هذا إلى علامة التبويب JS تحت زر Edit CSS/HTML[1]:

import { apiInitializer } from "discourse/lib/api";
import Component from '@glimmer/component';
import { action } from "@ember/object";
import Form from "discourse/components/form";
import DiscourseURL from "discourse/lib/url";

export default apiInitializer((api) => {
    api.renderBeforeWrapperOutlet("full-page-search-filters",
        class GoToTopic extends Component {
            @action
            handleSubmit(data) {
                DiscourseURL.routeTo(`/t/${data.id}`);
            }

            <template>
                <div class="topic-id-go-to" style="margin-top: 1em;">
                    <Form @onSubmit={{this.handleSubmit}} as |form|>

                      <form.Field @name="id" @title="Topic id" as |field|>
                        <field.Input @type="number" @validation="required" />
                      </form.Field>

                      <form.Submit />
                    </Form>
                </div>
            </template>
        }
    );
});

يضيف هذا إدخالًا إلى صفحة البحث:


زر Submit مخصص للانتقال إلى الموضوع[2]؛ فهو لا يؤثر على نتائج البحث.

آمل أن يكون هذا مفيدًا!


  1. أول مرة أستخدم فيها FormKit، إنها رائعة حقًا! ↩︎

  2. لم أتمكن من تغيير نص الزر، لأن ذلك سيتطلب لغات… وهو ما قد يتطلب مستودع TC كامل، وهو ما قد يكون مبالغًا فيه :person_shrugging:. ↩︎

إعجاب واحد (1)

شكراً @NateDhaliwal، أقدر هذا الجهد.
لقد أنشأت المكون، ولصقت الكود تحت علامة تبويب JS. تأكدت من أنه نشط في السمة الخاصة بي، ولكن لم يحدث شيء.
هذا ما تبدو عليه صفحة البحث الخاصة بي:

إعجاب واحد (1)

لقد قمت بإعداد هذا على موقعي للتجربة ويبدو أنه يعمل، ومع ذلك، أحصل على هذا التحذير الذي يجعلني أتوقف قليلاً:

هل هناك أي أخطاء في وحدة تحكم المتصفح؟

هذا هو الخطأ الوحيد:

وأعتقد أن هذا بسبب JavaScript لعرض معرف الموضوع، وليس البحث. لا يظهر إلا في الصفحات التي خصصتها لهذا العنصر.

لقد قمت بتعطيل، ثم إعادة تمكين مكون لافتة البحث المتقدم، والآن يعمل.
:man_shrugging:

إعجاب واحد (1)

حسنًا، إليك ملخص للوضع الحالي:

  1. لدينا حل يعمل. على الرغم من أنه غير أنيق قليلاً، إلا أنه سيفي باحتياجاتي حتى نتمكن من البحث عن معرف الموضوع مباشرة في مربع البحث القياسي.
  2. الكود الحالي الذي اقترحه @awesomerobot يبدو أنه يعمل، ولكنه يؤدي إلى ظهور التحذير الذي نشرته أعلاه.
    2أ. عندما قمت بإزالة الكود الأصلي الذي كنت أستخدمه لعرض معرف الموضوع، اختفى هذا التحذير…
  3. عندما تختار حلاً، يظهر الكود لعرض معرف الموضوع في كل من المنشور الأصلي وفي الحل، هل هناك طريقة لمعالجة ذلك ومنعه من الظهور هناك؟
  4. قمت بتزيين CSS قليلاً، الآن لدي حبوب على كل رد في كل سلسلة في الفئات المحددة التي أستخدمها فيها. أفترض أن الإجابة ستكون إلغاء التزيين، ولكن إذا عرف أي شخص طريقة أخرى، أفضل الاحتفاظ بها، أعتقد أنها تبدو لطيفة كما هي.

@awesomerobot
يعمل الكود الخاص بك بشكل رائع، ولكن يبدو أن أي زخارف CSS بخلاف النص تظهر في كل مشاركة بعد الأولى كمساحة فارغة منسقة بالطريقة التي تقولها CSS.

قم بإيقاف تشغيل الكتلة المضمنة وستحصل على شريط ألوان بعرض كامل.
قم بتغيير لون الخلفية وسيتغير لون الحبة
إلخ…

بناءً على ما كتبته، يبدو أنك أخبرته أن يظهر فقط في المنشور الأول، ولكن هذا يؤثر فقط على النص. هل هناك أي شيء آخر يمكننا فعله لقتل العنصر في جميع المشاركات الإضافية؟

إعجاب واحد (1)

حسنًا، كما قلت، أنا لست مبرمجًا، ولكني بارع جدًا في طرح الأسئلة على Grok. إليك رمز يعالج كلا المشكلتين.

  1. لا مزيد من فقاعات CSS الفارغة في الردود من 2 إلى X.
  2. لا مزيد من تكرار معرف الموضوع في الحلول المقتبسة.

ربما لا يهتم أي شخص آخر على وجه الأرض، ولكن هذا يبدو طريقة جيدة للمساعدة في تتبع الموضوعات لأولئك منا الذين يستخدمون Discourse كنظام تذاكر.

import { apiInitializer } from "discourse/lib/api";
import Component from "@glimmer/component";

class TopicIdentifier extends Component {
  get topicId() {
    return this.args.post?.topic?.id;
  }

  get shouldShow() {
    const firstPost = this.args.post?.post_number === 1; // first post in topic
    const desiredCategories = [9, 23]; // a comma separated list of category IDs you want this to appear in
    const isInCategory = desiredCategories.includes(
      this.args.post?.topic.category.id
    );

    return firstPost && isInCategory;
  }

  <template>
    {{#if this.shouldShow}}
      <!-- you can edit the content below, {{this.topicId}} is where the topic ID will be filled -->
      Issue Tracking # is
      {{this.topicId}}
      <!-- you can edit the content above -->
    {{/if}}
  </template>
}

export default apiInitializer("0.8.40", (api) => {
  api.decorateCookedElement((element, helper) => {
```gjs
import { apiInitializer } from "discourse/lib/api";
import Component from "@glimmer/component";

class TopicIdentifier extends Component {
  get topicId() {
    return this.args.post?.topic?.id;
  }

  get shouldShow() {
    const firstPost = this.args.post?.post_number === 1; // first post in topic
    const desiredCategories = [9, 23]; // a comma separated list of category IDs you want this to appear in
    const isInCategory = desiredCategories.includes(
      this.args.post?.topic.category.id
    );

    return firstPost && isInCategory;
  }

  <template>
    {{#if this.shouldShow}}
      <!-- you can edit the content below, {{this.topicId}} is where the topic ID will be filled -->
      Issue Tracking # is
      {{this.topicId}}
      <!-- you can edit the content above -->
    {{/if}}
  </template>
}

export default apiInitializer("0.8.40", (api) => {
  api.decorateCookedElement((element, helper) => {
    // Only proceed if this is the first post and not inside a solution quote
    // Only proceed if this is the first post, not in a quote, and in the desired category
    const post = helper?.model;
    const desiredCategories = [9, 23]; // Match the categories from TopicIdentifier
    if (
    if (
      helper?.model?.post_number !== 1 ||
      !post ||
      post.post_number !== 1 ||
      !desiredCategories.includes(post.topic?.category?.id) || // Check category
      element.classList.contains("post__contents-cooked-quote") || // Check if the element itself is the quote content
      element.closest("aside.accepted-answer") || // Check for solution/quote wrapper
      element.closest(".quote") // Additional check for generic quotes
    ) {
      return;
    }

    const wrapper = document.createElement("div");
    wrapper.className = "tracking-id";

    helper?.renderGlimmer(
      wrapper,
      <template><TopicIdentifier @post={{helper.model}} /></template>
    );

    element.appendChild(wrapper);
  });
});
3 إعجابات

لم أتمكن من الوصول إلى هذا في وقت سابق، ولكني سعيد لأنك توصلت إلى حل! يبدو هذا حلاً معقولاً.

إعجاب واحد (1)

أعلم أنه يمكنني اختباره بسهولة، ولكن هل تعرف على الفور ما إذا كان هذا سيعمل إذا وضعت هذا الـ JS والـ CSS في مكون خاص به، أم يجب أن يكون في السمة نفسها؟

لقد وجدت شذوذًا صغيرًا آخر. كنت أرى فقاعات الفئة الفارغة في المنشور الأول للمواضيع خارج الفئتين اللتين أردتهما فيهما، لذلك اضطررت إلى تحديث الكود قليلاً. إليك المنتج النهائي.

import { apiInitializer } from "discourse/lib/api";
import Component from "@glimmer/component";

class TopicIdentifier extends Component {
  get topicId() {
    return this.args.post?.topic?.id;
  }

  get shouldShow() {
    const firstPost = this.args.post?.post_number === 1; // first post in topic
    const desiredCategories = [9, 23]; // a comma separated list of category IDs you want this to appear in 
    const isInCategory = desiredCategories.includes(
      this.args.post?.topic.category.id
    );

    return firstPost && isInCategory;
  }

  <template>
    {{#if this.shouldShow}}
      <!-- you can edit the content below, {{this.topicId}} is where the topic ID will be filled -->
      Issue Tracking # is
      {{this.topicId}} 
      <!-- you can edit the content above -->
    {{/if}}
  </template>
}

export default apiInitializer("0.8.40", (api) => {
  api.decorateCookedElement((element, helper) => {
    // Only proceed if this is the first post and not inside a solution quote
    // Only proceed if this is the first post, not in a quote, and in the desired category
    const post = helper?.model;
    const desiredCategories = [9, 23]; // Match the categories from TopicIdentifier
    if (
    if (
      helper?.model?.post_number !== 1 ||
      !post ||
      post.post_number !== 1 ||
      !desiredCategories.includes(post.topic?.category?.id) || // Check category
      element.classList.contains("post__contents-cooked-quote") || // Check if the element itself is the quote content
      element.closest("aside.accepted-answer") || // Check for solution/quote wrapper
      element.closest(".quote") // Additional check for generic quotes
    ) {
      return;
    }

    const wrapper = document.createElement("div");
    wrapper.className = "tracking-id";

    helper?.renderGlimmer(
      wrapper,
      <template><TopicIdentifier @post={{helper.model}} /></template>
    );

    element.appendChild(wrapper);
  });
});
      element.classList.contains("post__contents-cooked-quote") || // Check if the element itself is the quote content
      element.closest("aside.accepted-answer") || // Check for solution/quote wrapper
      element.closest(".quote") // Additional check for generic quotes
    ) {
      return;
    }

    const wrapper = document.createElement("div");
    wrapper.className = "tracking-id";

    helper?.renderGlimmer(
      wrapper,
      <template><TopicIdentifier @post={{helper.model}} /></template>
    );

    element.appendChild(wrapper);
  });
});

وللإجابة على سؤالي بنفسي، قمت بنقل كل الكود إلى مكون سمة جديد وكل شيء لا يزال يعمل بشكل جيد.